How do I get the inverse of a matrix in python? I've implemented it myself, but it's pure python, and I suspect there are faster modules out there to do it.
I have a two dimensional array that I need to rotate 90 degrees clockwise, however I keep getting arrayindexoutofbounds...
public int[][] rorateArray(int[][] arr){
//first change the dimensions vertical length for horizontal length
//and viceversa
int[][] newArray = new int[arr[0].length][arr.length];
//invert values 90 degrees clockwise by starting from button of
//array to top and from left to right
int ii = 0;
int jj = 0;
for(int i=0; i<arr[0].length; i++){
for(int j=arr.length-1; j>=0; j--){
newArray[ii][jj] = arr[i][j];
jj++;
}
ii++;
}
return newArray;
}
Okay, so this question here asked which language is most like executable pseudocode, so why not find out by actually writing some code! Here we have a competition where I will award a 100 point bounty (I know its not much, but I am poor after the recalc) to the code which most resembles this pseudocode. I've read through this a few times so I'm pretty sure that this pseudocode below is correct and about as unambiguous as pseudocode can be.
Personally, I'm going to have a go in Python and probably Haskell as well, but I'm just learning the later so my attempt will probably be pretty poor.
Note: Obviously to implement anything looking like this you'll have to define quite a few library functions.
define DirectedGraph G with:
Vertices as V, Edges as E
define Vertex A, Z
declare each e in E as having properties:
Boolean fixed with:
initial=false
Real minSoFar with:
initial=0 for A else infinity
define PriorityQueue pq with:
objects=V
initial=A
priority v=v.minSoFar
create triggers for v in V:
when v.minSoFar event reduced then pq.addOrUpdate v
when v.fixed event becomesTrue then pq.remove v
Repeat until Z.fixed==True:
define Vertex U=pq.pop()
U.fixed=True
for Edge E adjacentTo U with other Vertex V:
V.minSoFar=U.minSoFar+length(E) if reducesValue
return Z.name, Z.minSoFar
Numbers whose only prime factors are 2, 3 or 5 are called ugly numbers.
Example:
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
1 can be considered as 2^0.
I am working on finding nth ugly number. Note that these numbers are extremely sparsely distributed as n gets large.
I wrote a trivial program that computes if a given number is ugly or not. For n 500 - it became super slow. I tried using memoization - observation: ugly_number * 2, ugly_number * 3, ugly_number * 5 are all ugly. Even with that it is slow. I tried using some properties of log - since that will reduce this problem from multiplication to addition - but, not much luck yet. Thought of sharing this with you all. Any interesting ideas?
Using a concept similar to "Sieve of Eratosthenes" (thanks Anon)
for (int i(2), uglyCount(0); ; i++) {
if (i % 2 == 0)
continue;
if (i % 3 == 0)
continue;
if (i % 5 == 0)
continue;
uglyCount++;
if (uglyCount == n - 1)
break;
}
i is the nth ugly number.
Even this is pretty slow. I am trying to find 1500th ugly number.
Hi
I have this array A = <3,2,9,0,7,5,4,8,6,1> and I want to write all its worst partitions are these correct?thanks
a1 = <0,2,9,3,7,5,4,8,6,1>
a2 = <1,9,3,7,5,4,8,6,2>
a3 = <2,3,7,5,4,8,6,9>
a4 = <3,7,5,4,8,6,9>
a5 = <4,5,7,8,6,9>
a6 = <5,7,8,6,9>
a7 = <6,8,7,9>
a8 = <7,8,9>
a9 = <8,9>
a10 = <9>
I'm trying to pass a txt file to som_read_data from a GUI......i created a function that takes a txt file from the GUI and then passes it to som_read_data..but i'm getting some errors...here are a list of some of the errors.....any one with ideas?
??? Error using ==> ftell
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> som_read_data at 169
fpos1 = ftell(fid); c1 = 0; % read first non-comment line
Error in ==> prog_som at 3
sD = som_read_data(m);
Let's say we have 0.33, we need to output "1/3".
If we have "0.4", we need to output "2/5".
The idea is to make it human-readable to make the user understand "x parts out of y" as a better way of understanding data.
I know that percentages is a good substitute but I was wondering if there was a simple way to do this?
I'm learning Weka and I'm trying to figure out how to do fuzzy clustering. I found an old site that listed 2 fuzzy clusterers (by Frank Weber & Robin Senge), but I could not add their .jar file to the existing .jar file to use the algorithms.
Does anyone know where I can find fuzzy clustering algorithms for Weka? If not, is there another tool that I can use for this?
Thanks, Jon
Hi all
I am trying to do bit reversal in a byte. I use the code below
static int BitReversal(int n)
{
int u0 = 0x55555555; // 01010101010101010101010101010101
int u1 = 0x33333333; // 00110011001100110011001100110011
int u2 = 0x0F0F0F0F; // 00001111000011110000111100001111
int u3 = 0x00FF00FF; // 00000000111111110000000011111111
int x, y, z;
x = n;
y = (x >> 1) & u0;
z = (x & u0) << 1;
x = y | z;
y = (x >> 2) & u1;
z = (x & u1) << 2;
x = y | z;
y = (x >> 4) & u2;
z = (x & u2) << 4;
x = y | z;
y = (x >> 8) & u3;
z = (x & u3) << 8;
x = y | z;
y = (x >> 16) & u4;
z = (x & u4) << 16;
x = y | z;
return x;
}
It can reverser the bit (on a 32-bit machine), but there is a problem,
For example, the input is 10001111101, I want to get 10111110001, but this method would reverse the whole byte including the heading 0s. The output is 10111110001000000000000000000000.
Is there any method to only reverse the actual number? I do not want to convert it to string and reverser, then convert again. Is there any pure math method or bit operation method?
Best Regards,
I'm sitting here writing a function that I'm positive has been written before, somewhere on earth. It's just too common to have not been attempted, and I'm wondering why I can't just go to a website and search for a function that I can then copy and paste into my project in 2 seconds, instead of wasting my day reinventing the wheel.
Sure there are certain libraries you can use, but where do you find these libraries and when they are absent, is there a site like I'm describing?
Possibly a wiki of some type that contains free code that anybody can edit and improve?
I have an idea for encryption that I could program fairly easily to encrypt some local text file.
Given that my approach is novel, and does not use any of the industry standard encryption techniques, would I be able to test the strength of my encryption using 'cracker' apps or suchlike?
Or do all those tools rely on advanced knowledge of the encryption process (or intercepted 'keys'), meaning I'd have to build my own cracker for testing?
Today I had an interview where I was asked to write a program which takes a Binary Tree and returns true if it is also a Binary Search Tree otherwise false.
My Approach1: Perform an inroder traversal and store the elements in O(n) time. Now scan through the array/list of elements and check if element at ith index is greater than element at (i+1)th index. If such a condition is encountered, return false and break out of the loop. (This takes O(n) time). At the end return true.
But this gentleman wanted me to provide an efficient solution. I tried but I was unsuccessfult, because to find if it is a BST I have to check each node.
Moreover he was pointing me to think over recusrion.
My Approach 2: A BT is a BST if for any node N N-left is < N and N-right N , and the INorder successor of left node of N is less than N and the inorder successor of right node of N is greater than N and the left and right subtrees are BSTs.
But this is going to be complicated and running time doesn't seem to be good. Please help if you know any optimal solution.
Thanks.
Hi guys,
I am planning to implement spam filter using Naive Bayesian classification model.
Online I see a lot of info on Naive Bayesian classification, but the problem is its a lot of mathematical stuff, than clearly stating how its done. And the problem is I am more of a programmer than a mathematician (yes I had learnt Probability and Bayesian theorem back in school, but out of touch for a long long time, and I don't have luxury of learning it now (Have nearly 3 weeks to come-up with a working prototype)).
So if someone can explain or point me to location where its explained for programmers than a mathematician, it would be a great help.
PS: By the way I have to implement it in C, if you want to know. :(
Regards,
Microkernel
I have a stored procedure that uses Levenshtein Distance to determine the result closest to what the user typed. The only thing really affecting the speed is the function that calculates the Levenshtein Distance for all the records before selecting the record with the lowest distance (I've verified this by putting a 0 in place of the call to the Levenshtein function). The table has 1.5 million records, so even the slightest adjustment may shave off a few seconds. Right now the entire thing runs over 10 minutes. Here's the method I'm using:
ALTER function dbo.Levenshtein
(
@Source nvarchar(200),
@Target nvarchar(200)
)
RETURNS int
AS
BEGIN
DECLARE @Source_len int, @Target_len int, @i int, @j int, @Source_char nchar, @Dist int, @Dist_temp int, @Distv0 varbinary(8000), @Distv1 varbinary(8000)
SELECT @Source_len = LEN(@Source), @Target_len = LEN(@Target), @Distv1 = 0x0000, @j = 1, @i = 1, @Dist = 0
WHILE @j <= @Target_len
BEGIN
SELECT @Distv1 = @Distv1 + CAST(@j AS binary(2)), @j = @j + 1
END
WHILE @i <= @Source_len
BEGIN
SELECT @Source_char = SUBSTRING(@Source, @i, 1), @Dist = @i, @Distv0 = CAST(@i AS binary(2)), @j = 1
WHILE @j <= @Target_len
BEGIN
SET @Dist = @Dist + 1
SET @Dist_temp = CAST(SUBSTRING(@Distv1, @j+@j-1, 2) AS int) +
CASE WHEN @Source_char = SUBSTRING(@Target, @j, 1) THEN 0 ELSE 1 END
IF @Dist > @Dist_temp
BEGIN
SET @Dist = @Dist_temp
END
SET @Dist_temp = CAST(SUBSTRING(@Distv1, @j+@j+1, 2) AS int)+1
IF @Dist > @Dist_temp SET @Dist = @Dist_temp
BEGIN
SELECT @Distv0 = @Distv0 + CAST(@Dist AS binary(2)), @j = @j + 1
END
END
SELECT @Distv1 = @Distv0, @i = @i + 1
END
RETURN @Dist
END
Anyone have any ideas? Any input is appreciated.
Thanks,
Matt
I'm working on a project, written in Java, which requires that I build a very large 2-D sparse array. Very sparse, if that makes a difference. Anyway: the most crucial aspect for this application is efficency in terms of time (assume loads of memory, though not nearly so unlimited as to allow me to use a standard 2-D array -- the key range is in the billions in both dimensions).
Out of the kajillion cells in the array, there will be several hundred thousand cells which contain an object. I need to be able to modify cell contents VERY quickly.
Anyway: Does anyone know a particularly good library for this purpose? It would have to be Berkeley, LGPL or similar license (no GPL, as the product can't be entirely open-sourced). Or if there's just a very simple way to make a homebrew sparse array object, that'd be fine too.
I'm considering MTJ, but haven't heard any opinions on its quality.
Thanks!!
-Dan
I have a arbitrarily large string of text from the user that needs to be split into 10k chunks (potentially adjustable value) and sent off to another system for processing.
Chunks cannot be longer than 10k (or other arbitrary value)
Text should be broken with natural language context in mind
split on punctuation when possible
split on spaces if no punction exists
break a word as a last resort
I'm trying not to re-invent the wheel with this, any suggestions before I roll this from scratch?
Using C#.
i have got a latitude and longitude of a point... i want to get all the readings of latitude and longitude with in a particular radius... is there any methods to calculate all laltitudes and longtiudes with in a radius with respect to a given point?...can u help me
Hi I have the following UK postcode dy8 3xt and know that the latitude and longitude is:-
52.190108
-2.517266
I also know that the Eastings, Northings for the postcode is:-
389490
283880
However I am struggling to find the equation that converts lat/long to northings and Eastings, I would prefer to have the equation in both in jScript and c# (I am being greedy)!
Can anyone help?
Hi,
I am creating a custom social network for one of my clients.
In this I am storing the friends of a user in the form of CSV as shown below in the user table
uid user_name friends
1 John 2
2 Jack 3,1
3 Gary 2,4
4 Joey 3
In the above scenario if the logged in user is John and if he visits the profile page of Joey, the connection between them should appear as
John-Jack-Gary-Joey
I am able to establish the connection at level 1 i.e
If Jack visits Joey's profile I am able to establish the following :
Jack-Gary-Joey
But for the 2nd level I need to get into the same routine of for loops which I know is not the right solution + I am not able to implement that as well.
So, can someone please help me with this?
Thanks in Advance,
Akash
P:S I am not in a position to change the db architecture :(
I recently came across the data structure known as a Skip list. They seem to have very similar behavior to a binary search tree... my question is - why would you ever want to use a skip list over a binary search tree?
Basically what I want to do is illustrated here:
I start with A and B, then B is conformed to A to create C.
The idea is, given TLBR rectangles A, B, make C
I also need to know if it produces an empty rectangle (B outside of A case).
I tried this but it just isn't doing what I want:
if(clipRect.getLeft() > rect.getLeft())
L = clipRect.getLeft();
else
L = rect.getLeft();
if(clipRect.getRight() < rect.getRight())
R = clipRect.getRight();
else
R = rect.getRight();
if(clipRect.getBottom() > rect.getBottom())
B = clipRect.getBottom();
else
B = rect.getBottom();
if(clipRect.getTop() < rect.getTop())
T = clipRect.getTop();
else
T = rect.getTop();
if(L < R && B < T)
{
clipRect = AguiRectangle(0,0,0,0);
}
else
{
clipRect = AguiRectangle::fromTLBR(T,L,B,R);
}
Thanks
Hi,
I have an application which is using 24 bit fixed point calculation.I am porting it to a hardware which does support floating point, so for speed optimization I need to convert all fixed point based calculation to floating point based calculation.
For this code snippet, It is calculating mantissa
for(i=0;i<8207;i++)
{
// Do n^8/7 calculation and store
// it in mantissa and exponent, scaled to
// fixed point precision.
}
So since this calculation, does convert an integer to mantissa and exponent scaled to fixed point precision(23 bit). When I tried converting it to float, by dividing the mantissa part by precision bits and subtracting the exponent part by precision bit, it really does'
t work.
Please help suggesting a better way of doing it.
I have an adjacency list of objects (rows loaded from SQL database with the key and it's parent key) that I need to use to build an unordered tree. It's guaranteed to not have cycles.
This is taking wayyy too long (processed only ~3K out of 870K nodes in about 5 minutes). Running on my workstation Core 2 Duo with plenty of RAM.
Any ideas on how to make this faster?
public class StampHierarchy {
private StampNode _root;
private SortedList<int, StampNode> _keyNodeIndex;
// takes a list of nodes and builds a tree
// starting at _root
private void BuildHierarchy(List<StampNode> nodes)
{
Stack<StampNode> processor = new Stack<StampNode>();
_keyNodeIndex = new SortedList<int, StampNode>(nodes.Count);
// find the root
_root = nodes.Find(n => n.Parent == 0);
// find children...
processor.Push(_root);
while (processor.Count != 0)
{
StampNode current = processor.Pop();
// keep a direct link to the node via the key
_keyNodeIndex.Add(current.Key, current);
// add children
current.Children.AddRange(nodes.Where(n => n.Parent == current.Key));
// queue the children
foreach (StampNode child in current.Children)
{
processor.Push(child);
nodes.Remove(child); // thought this might help the Where above
}
}
}
}
public class StampNode {
// properties: int Key, int Parent, string Name, List<StampNode> Children
}