help with to row echelon form algorithm

G

greenflame

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 test
 
R

RobG

greenflame said:
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 test


Some ideas:

In randommatrix() it seems you want some values -ve and some +ve. You
could replace:

if (Math.round(Math.random()) == 0) {
output[j] = Math.round(Math.random()*max);
} else {
output[j] = -1*Math.round(Math.random()*max);
}

with:

output[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[j].length == undefined) {
// Replace it with:
if ( 'number' == typeof input[j].length ) {

output[j] = input[j].toString();
} else {

// Not sure what's happening here
output[j] = input[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...
 
R

RobG

RobG wrote:
[..]
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];

Here's a better one (it assumes you pass an array):

function findmax( x ){
var i = x.length;
var t = x[0];
while ( --i ) {
t = ( t>x )? t : x;
}
return t;
}

It assigns the first value of x to 't', then tests all the other
values to see if they are higher. The pre-decrement --i means that
the 0th value isn't tested (it doesn't need to be as t is x[0] to
start with and will be replaced if a higher value is found).

Probably saves half a clock tick!


[...]
 
G

greenflame

wow that is alot of stuff... Ok Thanks I will be busy lately. Also
would it be helpful to post algorithm I am trying to implement?
 
G

greenflame

Well... after more extensive testing I found some problems... :( Well
then I worked on it and now I have totally new problem. I seem to get
stuck in an infinite loop and I cannot seem to see how or where. I know
I feel stupid. :/

The page is at <URL: http://ali.freezope.org/reftest>
 
R

RobG

greenflame said:
Well... after more extensive testing I found some problems... :( Well
then I worked on it and now I have totally new problem. I seem to get
stuck in an infinite loop and I cannot seem to see how or where. I know
I feel stupid. :/

The page is at <URL: http://ali.freezope.org/reftest>

If you have a specific question, start a new thread. You were lucky I
found you way down here (well, maybe you don't consider it lucky...) :)

As a tip, insert a confirm before your alert in rref(). If you click
'cancel', your function will stop the looping at your convenience and
the output appears on the page:

//...
} else if (fnzcind == -2) { // if workingrow,workingcol
tocontinue == false;

// Put this line in
if ( ! confirm( 'here: ' + fnzcind ) ) return;

alert("-2");
}
document.write("End Of Column: "+(workingcol-1)+"<br><br>");
//...


As another tip, create a couple of small matrices that you know the
solution for, then make sure your algorithm is working OK - ones that
result in all integer intermediate results are best.

Then work through them manually and note the results at each step.
You have an output function that shows you the results, but you get
different stuff every time, making debugging tougher than it needs to be.
 
G

greenflame

...
You were lucky I found you way down here
(well, maybe you don't consider it lucky...) :)

Oh... sorry... I do not have that problem because I star all the
threads that I want to be able to find easily. Also this has to do with
this same topic so intead of creating a new thread (which I know will
make some people angry) I just made a reply to this one. :-/
As another tip, create a couple of small matrices that you know the
solution for, then make sure your algorithm is working OK - ones that
result in all integer intermediate results are best.

Well... I can only do this if the script stops running (atleast long
enough) for me to see the output. Since this did not happen, I could
not check. But now I will try this confirm thing. Also I do not
understand what you mean by 'ones that result in all integer
intermediate results are best.' Especially the 'integer intermediate
results'.
Then work through them manually and note the results at each step.

I do not quite understand this... :-( Do you mean for me to work out
the results my self and compare with the script's output?
You have an output function that shows you the results, but you get
different stuff every time, making debugging tougher than it needs to be.

Are you refering to the fact that my script generates a random matrix
every time the page loads? If not then I do not understand this as
well. :-(
 
G

greenflame

OK..... I found the problem... I had written:

tocontinue == false;

when I should have written:

tocontinue = false;

Man I feel stupid!
 
R

RobG

greenflame said:
... [...]
As another tip, create a couple of small matrices that you know the
solution for, then make sure your algorithm is working OK - ones that
result in all integer intermediate results are best.


Well... I can only do this if the script stops running (atleast long
enough) for me to see the output. Since this did not happen, I could
not check. But now I will try this confirm thing. Also I do not
understand what you mean by 'ones that result in all integer
intermediate results are best.' Especially the 'integer intermediate
results'.

Using a confirm as suggested will cause the script to stop and only
continue if you want it to.
I do not quite understand this... :-( Do you mean for me to work out
the results my self and compare with the script's output?

Yes. Otherwise you only know that 'magic happens', you don't know if
your algorithm really works. Design of suitable test cases that test
every function is fundamental to testing any program. And you need to
know all the intermediate results too. And you need to have suitable
exception handling.

As a side note, there are formulae that will generate a matrix inverse
that may be simpler than using row-reduction (you will need to be able
to calculate a determinant, see previous threads).

Are you refering to the fact that my script generates a random matrix
every time the page loads? If not then I do not understand this as
well. :-(

Yes. Having a random generator is fine, but you never see the same
matrix twice (at least not often enough for my feeble mind to remember).
Once you've debugged the program and have it running nicely, then use
random matrices.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top