FORUMS: list search recent posts

# addition of 2 vectors (javascript)

FAQ   •   VIEW ALL
 addition of 2 vectors (javascript) on Aug 18, 2012 at 3:58:40 pm

Hello,

is there a javascript method to add two arrays of numbers (something like Array.add(array)) or does one have to always write a loop ?

The problem i encountered is the following:
if V1 and V2 are arrays of numbers of the same length, to make their sum one can simply write: sum = V1+V2, but if the common length is >1000, then sum is an array of length 1000 (it took me ages to realize this...lost so much time), the remaining entries are not calculated. For instance, if POLO is an array of length 1300 whose entries are all equal to 1, then all entries of POLO+POLO are equal to 2, but POLO+POLO has length ...1000, not 1300.

If instead of writing sum = V1+V2 one explicitely writes:
``` for (k=0; k < V1.length; k++) { sum[k] = V1[k]+V2[k]; } ```
then sum has the correct length.
Really weird, and very well hidden feature. Is there a reason why it is so ?

 Re: addition of 2 vectors (javascript)on Aug 18, 2012 at 5:12:08 pm

As far as I know, JavaScript doesn't include a function for adding arrays. I would guess Adobe added it to the expression language mainly to make it easier to manipulate small, property-sized arrays, although I wasn't aware there was a size limit.

I haven't tried this, but the only thing I can think of is that if the array lengths are greater than 1000, you could break them into pieces using JavaScript's Array.slice(), add the pieces using Adobe's + operator, and use Array.concat() to put the pieces together. Ugly, but it should work.

Dan

 Re: addition of 2 vectors (javascript)on Aug 18, 2012 at 9:12:16 pm