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;
} 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
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).
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.
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/
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'));
ok thank you for all your help! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |