greenflame wrote:
I am working on an algorithm to put a 'matrix' in row echelon form, and
eventually reduced row echelon form. My script only seems to work if
there arent to many ones, zeros, or row(s) of zeros. I get a different
error message in each of the aforementioned cases. Both are like null
or length is undefined or not an object. Once at, line 245 other times
103, other times something else.... I'm thinking it may have something
to do with it going past the 'matrix'. the script is rather long so
here is the link to the script. It is a text file containing the code.
Here it is: http://ali.freezope.org/ref%20test
Some ideas:
In randommatrix() it seems you want some values -ve and some +ve. You
could replace:
if (Math.round(Math.random()) == 0) {
output[i][j] = Math.round(Math.random()*max);
} else {
output[i][j] = -1*Math.round(Math.random()*max);
}
with:
output[i][j] = Math.round( (Math.random()-0.5)*max*2 );
Subtracting 0.5 then *2 should ensure the number is roughly evenly +ve
and +ve, though the *2 may weight results away from 0 or 1.
In findmax(), you copy the array, then troll through it looking for
the max value. Since you don't modify the array, you don't need to
copy it. If you want to copy it, then use sort() and get the last
value - it will be the highest (and the first will be the lowest)
so:
var Temp = copy1darr(input);
for (var i=0;i<Temp.length;i++) {
for (var j=0;j<Temp.length-1;j++) {
Temp[j] = Math.max(Temp[j],Temp[j+1]);
}
}
return Temp[0]; //return the max number
could be replaced with:
var Temp = copy1darr(input);
Temp.sort();
return Temp[Temp.length-1];
In your function tostr(), there is no need to explicitly create each
element of the array as an undefined value using a function.
var output = [];
will create an empty array, then you just assign values (note where
input.length is used not output.length):
for (var i=0;i<input.length;i++) {
for (var j=0;j<input[0].length;j++) {
// This is the line that was giving an error:
// if (input[i][j].length == undefined) {
// Replace it with:
if ( 'number' == typeof input[i][j].length ) {
output[i][j] = input[i][j].toString();
} else {
// Not sure what's happening here
output[i][j] = input[i][j].join("/");
}
}
}
The line that I have replaced with 'number' == typeof ...
is where your error was. Trying to get the length of a number will
cause an error and the evaluation never occurs. If you are looking
for a number, use typeof. You have used a similar test in many other
places, you need to replace them all.
There are likely many other optimisations...
--
Rob