473,387 Members | 1,536 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,387 software developers and data experts.

why does it take forever?

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;
}

Jul 23 '05 #1
6 1116
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
Jul 23 '05 #2
Jc
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).

Jul 23 '05 #3


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.

Jul 23 '05 #4

Keep it concise and precise, all I can tell is that you're doing a
2d-array set for some element iteration for multiplication, and it really
depends on how's coded really, no matter what language, if you don't use
resources sparingly and return them back as soon as not needed, it can bog
down the system, so, it isn't the language per se. Not sure what you're
specifically doing but I'd think it wouldn't take me 3 functions just to
take 2 arguments and process something for stdout.

Danny

On Sat, 11 Jun 2005 17:19:28 -0700, greenflame <al*********@yahoo.com>
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;
}


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #5
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'));
Jul 23 '05 #6
ok thank you for all your help!

Jul 23 '05 #7

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

Similar topics

54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
5
by: Shay | last post by:
essentially I am trying to do some counts based on some assumptions in the recordset. So I get the RS back, put the values into a variable, move to the next record in the RS and compare what is in...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
1
by: Amil Hanish | last post by:
I'm having an issue on IIS 6 on Windows 2003 Server. The following simple file hangs the client forever (I quite waiting after about five minutes). No error in IIS error file or IIS log file. The...
28
by: Peter Olcott | last post by:
I want to double check my understanding about how the .NET framework works. From what I understand every call to the .NET framework is ultimately translated into one of more API calls, is this...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
34
by: NewToCPP | last post by:
Hi, Why does a C/C++ programs crash? When there is access to a null pointer or some thing like that programs crash, but why do they crash? Thanks.
3
by: Inny | last post by:
I want To offer a login Option, two Checkboxs, 1 labeled 'normal' the other labeled 'Forever'. I want to assign the check boxes to switch between the cookie script below (normal) and An altered...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.