473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help with to row echelon form algorithm

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

Jul 23 '05 #1
9 4374
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
Jul 23 '05 #2
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[i] )? t : x[i];
}
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!
[...]
--
Rob
Jul 23 '05 #3
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?

Jul 23 '05 #4
ok. I got it to work using for loops, etc. If you want I can post it...

Jul 23 '05 #5
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>

Jul 23 '05 #6
greenflame wrote:
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.

--
Rob
Jul 23 '05 #7
> ...
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. :-(

Jul 25 '05 #8
OK..... I found the problem... I had written:

tocontinue == false;

when I should have written:

tocontinue = false;

Man I feel stupid!

Jul 25 '05 #9
greenflame wrote:
... [...]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.
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?


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).

<URL:http://documents.wolfram.com/teachersedition/index151.html>
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. :-(


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.

--
Rob
Jul 26 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
8
by: point | last post by:
Hello there.... I hope there's someone who can help with one algorithm...I've been trying the whole day.... I'm building my own FORM class and have a function select(&$options, $name = "",...
2
by: prash | last post by:
Hi everybody, I would like to request all the members to help me find the source code for the following : 1.C program for error detecting code: CRC-CCITT and CRC -32. 2.c Program for...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
1
by: kiran | last post by:
Hi , I need some helpon Symmetric Crptography algorithm. My friend has given a encrypted message ( encrypted using Symmetric cryptogrraphy algorithm and he does 't mention on which plat form is...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
2
by: zaheer181 | last post by:
Hai everybody, Iam assigned a task of implementing fuzzy c means algorithm in matlab to C language using graphics.h and my question is can we implement the same intensity of graphics in matlab...
31
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed...
4
by: knuxus | last post by:
Hey everyone.... I would appreciate any type of help if anyone could explain me how to translate this algorithm to Visual Basic, im still learning and i would appreciate this algorithm to my prime...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.