greenflame said:
Everyone: So it is more efficient to fill in an empty array then to
copy one and shange the values?
Yes, it's about 20% slower. You have to go through the array twice,
doing most of what you have to do when mulitplying.
Also, since the idea has been brought
up, Is there a way good way to make an iff block that checks whether a
variable is a string, number, "1D Matrix", "2D Matrix, ... etc. A "1D
Matrix is just an array. A "2D Matrix" is an arry of arrays.
A 'good' way? no. You have to checek each element. Your 'matrix' is
just an object that holds arrays, it has no special methods to do
matrix stuff (unless you create your own Matrix object, see below).
Javascript only has 3 types: number, string and object. You can assume
that your mulitply function gets something it can digest, or try to
make it bullet-proof (which will add bloat and slowness). If you make
the assumption that the calling function will only pass arrays or
numbers, your work is much simpler and your function runs much faster.
There is no native JavaScript 'matrix', though 'pointed ears' posted
some code that creates a matrix object:
<URL:
http://groups-beta.google.com/group...b84800c?q=RobG&rnum=59&hl=en#75a08b121b84800c>
If you encounter an element that isn't a number (or a string that will
convert to a number like '6' or '034' or such) or an array, what do you
want to do? scalematrix() will return 'NaN', an alternative might be
to return the element unmodified. But that will require an extra test
and else statement (i.e. slowness). And what kind of matrix will
contain non-number entries?
The function that calls multiply should make sure whatever it passes
fits the requirements of the function.
Rob G: Does your multuplyArr function just multiply all the elements of
an "n dimensional matrix" by a certain factor (where an "n dimensional
matrix" is n nested arrays)?
Yes - but it's about 50% slower than using your cleaned-up multiply
function, so if you know you have 1D or 2D arrays, don't use it. There
may be more efficient algorithms, you'll need to search. I wrote it to
be concise, I haven't had much luck making it more efficient.
Try Wolfram research, they have lots of cool stuff based on working
from first principles and present some neat algorthims for doing stuff.
And it's presented in plain, simple terms (which suits me!).
<URL:
http://www.wolfram.com/>
Checkout the links in the 'Site highlights' menu, lower right.
Here is a version of the 'nDimension' multiplier that checks the input
more thoroughly. If it's not a number or array, it just returns the
thing unmodified (it's fractionally slower than the original).
// Multiply an n dimension array
function multiplyArr( A, n ) {
if ( !isNaN(A) ) { return A*n; }
if ( Array != A.constructor) { return A; }
var i = A.length;
var B = [];
while ( i-- ) {
B
= multiplyArr( A, n );
}
return B;
}
However, a function designed specifically to multiply a 2D array runs
in one third of the time (and 20% to 50% faster than scalematrix() ):
function multiplyArr2D(A, n){
var j, i = A.length;
var B = [];
while ( i-- ) {
j = A.length ;
B = [];
while ( j-- ) {
B[j] = A[j]*n;
}
}
return B;
}