Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > why does it take forever?

Reply
Thread Tools

why does it take forever?

 
 
greenflame
Guest
Posts: n/a
 
      06-12-2005
I have the following function. It is supposed to multiply all the
elements of a row of a matrix my a certain factor. I make a matrix with
a list of lists. with each of the inner lists represents a row of a
matrix. So the matrix:

[1 2 3]
[4 5 6]
[7 8 9]

is coded as:

A = [
[1,2,3],
[4,5,6],
[7,8,9]
];

OK now heres the function:

function create2darr(rows,cols) {
output = new Array(rows);
for (i=0;i<rows;i++) {
output[i] = new Array(cols);
}
return output;
}
function copy2darr(input) {
output = create2darr(input.length,input[0].length);
for (i=0;i<input.length;i++) {
for (j=0;j<input[0].length;j++) {
output[i][j] = input[i][j];
}
}
return output;
}
function xrow(input,row,factor) {
output = copy2darr(input);
for (i=0;output[row-1].length;i++) {
output[row-1][i] *= factor;
}
return output;
}

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-12-2005
greenflame wrote:
> I have the following function. It is supposed to multiply all the
> elements of a row of a matrix my a certain factor. I make a matrix with
> a list of lists. with each of the inner lists represents a row of a
> matrix. So the matrix:
>
> [1 2 3]
> [4 5 6]
> [7 8 9]
>
> is coded as:
>
> A = [
> [1,2,3],
> [4,5,6],
> [7,8,9]
> ];
>
> OK now heres the function:
>
> function create2darr(rows,cols) {
> output = new Array(rows);
> for (i=0;i<rows;i++) {
> output[i] = new Array(cols);
> }
> return output;
> }
> function copy2darr(input) {
> output = create2darr(input.length,input[0].length);
> for (i=0;i<input.length;i++) {
> for (j=0;j<input[0].length;j++) {
> output[i][j] = input[i][j];
> }
> }
> return output;
> }
> function xrow(input,row,factor) {
> output = copy2darr(input);
> for (i=0;output[row-1].length;i++) {
> output[row-1][i] *= factor;
> }
> return output;
> }


You create matrix A. You create a reference to it called 'input'
when calling the function xrow().

You then create a global variable 'output' that references a row of
A. You then do things with 'output' in other functions that are all
manipulating the same row of A - creating more references so that my
browser runs out of memory and never finishes.

There is no need to be so complex. If all you want to do is multiply
a row of A by some factor, then the following will do the job (note
that I have adjusted the index for rows to start from 1 rather than
zero, that seems to be what you were doing):

function xrow(input,row,factor) {
var j, r = row-1; // Adjust row index
// Make sure matrix and row exist and have length > 0
if ( !input || !input.length
|| !input[r] || !(j = input[r].length) ) {
return;
}
for (var i=0; i<j; i++) {
input[r][i] *= factor;
}
}

A = [
[1,2,3],
[4,5,6],
[7,8,9]
];

alert(A.join('\n'));
xrow(A,1,5);
alert(A.join('\n'));


Note that when you create A as a global object like this, if you
create a reference to some part of A and change it, then is it A
that is actually being changed.

It's the same as when you use:

x = document.getElementById('blah');

x is a reference to the element with id 'blah'. When you modify x,
you are actually modifying 'blah'.


--
Rob
 
Reply With Quote
 
 
 
 
Jc
Guest
Posts: n/a
 
      06-12-2005
greenflame wrote:
> function xrow(input,row,factor) {
> output = copy2darr(input);
> for (i=0;output[row-1].length;i++) {
> output[row-1][i] *= factor;
> }
> return output;
> }


So I assume you are seeing the code perform slower than you expect?

Well, JavaScript isn't exactly a speed demon for mathematical
operations, but if you show how you are actually using xrow in an
actual working example, maybe someone can point out some optimizations.

I should also mention that you are likely not intending to create
global var's i and j (among others) in your for loops, which is what
happens when you use a var in a fuction without declaring it using the
var keyword (this does not apply to function parameter names of course).

 
Reply With Quote
 
greenflame
Guest
Posts: n/a
 
      06-12-2005


RobG wrote:
> You create matrix A. You create a reference to it called 'input'
> when calling the function xrow().
>
> You then create a global variable 'output' that references a row of
> A. You then do things with 'output' in other functions that are all
> manipulating the same row of A - creating more references so that my
> browser runs out of memory and never finishes.
>
> There is no need to be so complex. If all you want to do is multiply
> a row of A by some factor, then the following will do the job (note
> that I have adjusted the index for rows to start from 1 rather than
> zero, that seems to be what you were doing):
>
> function xrow(input,row,factor) {
> var j, r = row-1; // Adjust row index
> // Make sure matrix and row exist and have length > 0
> if ( !input || !input.length
> || !input[r] || !(j = input[r].length) ) {
> return;
> }
> for (var i=0; i<j; i++) {
> input[r][i] *= factor;
> }
> }
>
> A = [
> [1,2,3],
> [4,5,6],
> [7,8,9]
> ];
>
> alert(A.join('\n'));
> xrow(A,1,5);
> alert(A.join('\n'));
>
>
> Note that when you create A as a global object like this, if you
> create a reference to some part of A and change it, then is it A
> that is actually being changed.
>
> It's the same as when you use:
>
> x = document.getElementById('blah');
>
> x is a reference to the element with id 'blah'. When you modify x,
> you are actually modifying 'blah'.
>
>
> --
> Rob


ok but I want the function to return a new matrix and leave the
original untouched.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-13-2005
greenflame wrote:
>

[...]
>
> ok but I want the function to return a new matrix and leave the
> original untouched.
>


Then copy the matrix to a new object first:

function copyMatrix(A){
var j, i=A.length, B = [];
while (i--) {
B[i]= [];
j = A[i].length;
while ( j-- ){
B[i][j] = A[i][j];
}
}
return B;
}

function xrow(input,row,factor) {
var j, r = row-1; // Adjust row index
// Make sure matrix and row exist and have length > 0
if ( !input || !input.length
|| !input[r] || !(j = input[r].length) ) {
return;
}
for (var i=0; i<j; i++) {
input[r][i] *= factor;
}
}

A = [
[1,2,3],
[4,5,6],
[7,8,9]
];

B = copyMatrix(A);
xrow(B, 1, 5);
alert(A.join('\n'));
alert(B.join('\n'));
 
Reply With Quote
 
greenflame
Guest
Posts: n/a
 
      06-13-2005
ok thank you for all your help!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have to take 292 & 296 can I take the 4 cores instead? =?Utf-8?B?SmltIEhvbGxvd2F5?= Microsoft Certification 6 06-02-2007 02:12 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Take a slow ride....take it eeee-zy ellis_jay Computer Support 0 08-25-2005 01:31 AM
Re: Doesn't Take Much To Take Over A Group Soapy Digital Photography 59 09-13-2004 05:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57