next up previous contents
Next: The where function Up: Array operations Previous: Array operations   Contents

Default array operations

Array + scalar ; Adds scalar value to all elements. E.g.
IDL> print,[2,1,3]+2
       4       3       5
Array * scalar ; Multiplies all elements by scalar. E.g.
IDL> print,[2,1,3]*2
       4       2       6
Array * Array ; Multiplies each element in the first array by the corresponding element in the second array. Eg.
IDL> print,[[1,2,3],[4,5,6]]*[[1,2,3],[4,5,6]]
       1       4       9
      16      25      36 ; OR 
IDL> print,[[1,2,3],[4,5,6]]*[1,2,3]
       1       4       9
because the output is just the size of the smaller array i.e. Array multiplication is NOT the default - this uses # and ## see IDL help ``Multiplying Arrays'' for detail. In brief:

The ## operator does what is commonly referred to as matrix multiplication. It computes array elements by multiplying the rows of the first array by the columns of the second array. The resulting array has the same number of rows as the first array and the same number of columns as the second array. The second array must have the same number of rows as the first array has columns.

The # operator computes array elements by multiplying the columns of the first array by the rows of the second array. The resulting array has the same number of columns as the first array and the same number of rows as the second array. The second array must have the same number of columns as the first array has rows.

BUT when Multiplying Vectors

When using the # and ## operators to multiply vectors, note the following:

For A # B, where A and B are vectors, IDL performs A # TRANSPOSE(B). In this case, C = A # B is a matrix with Cij = Ai Bj. Mathematically, this is equivalent to the outer product, usually denoted by A Ä B.

For A ## B, where A and B are vectors, IDL performs TRANSPOSE(A) ## B. In this case, C = A ## B is a matrix with Cij = Bi Aj.

To compute the dot product, usually denoted by A $\times$ B, use TRANSPOSE(A) # B.

We'll see uses of # later.


next up previous contents
Next: The where function Up: Array operations Previous: Array operations   Contents
John Marsham 2005-04-22