Some basic vector operations in IDL

Some basic vector operations will be described using IDL. They are easily translated to other languages. Vectors in IDL may be defined as A = [Ax,Ay] for 2-d, and A = [Ax,Ay,Az] for 3-d.

A list of vectors may be defined as:

   V = V1x, V1y, V1z
       V2x, V2y, V2z
            ...
       Vnx, Vny, Vnz
This can be set up in IDL as
V = [[V1x, V1y, V1z],[V2x, V2y, V2z],...,[Vnx, Vny, Vnz]]

Vector Addition
C = A + B

In IDL: C = A + B

If vectors are considered as arrows then adding vectors A and B is done by moving arrow B, without changing its size or direction, to place its tail at the head of arrow A. A new vector from the tail of arrow A to the head of arrow B is the sum, C. Mathematically vectors are added by adding their corresponding components. The following figure illustrates vector addition.

Vector addition works for two lists of vectors if the have the same size and shape.


Product of a Scalar and a Vector
B = s*A = (s*Ax, s*Ay, s*Az)

In IDL: B = s*A

A list of vectors, V, may also be multiplied by a scalar, s: T = s*V


Vector Subtraction
C = A - B = A + -1*B

In IDL: C = A - B

Vector subtraction works for two lists of vectors if the have the same size and shape.


Dot Product
The dot product of two vectors, A and B is written as
A dot B = Ax*Bx + Ay*By + Az*Bz

In IDL: A dot B is simply TOTAL(A*B).

The dot product of a list of vectors, V, with a single vector, B is in IDL: V##B
which gives a column of values, one for each dot product.


Length of a Vector
The length of a vector A is |A| = sqrt(Ax^2 + Ay^2 + Az^2) = sqrt(A dot A).

In IDL: this may be computed as SQRT(TOTAL(A*A)).

The lengths of a list of vectors, V, is found in IDL as: total(V*V,1)
which gives a length for each vector in the list.


Unit Vector
A unit vector is a vector with a length of 1 unit. For the vector A the unit vector is written as A_hat and is A/|A|.

In IDL: AU = A/SQRT(TOTAL(A*A)).
The function UNIT does this operation: AU = UNIT(A).


Angle between Vectors
The dot product between two unit vectors is the cosine of the angle between them.

In IDL: the angle between vectors A and B is given by ANG_A_B = ACOS(TOTAL(UNIT(A)*UNIT(B))) in radians (multiply by !RADEG to convert to degrees).


Testing for Perpendicular Vectors
From the last paragraph it follows that the dot product of two vectors is 0 if they are perpendicular to each other. This follows since the cosine of + or - 90 degrees is 0. The converse is also true, two vectors are perpendicular if their dot product is 0.

In IDL: For vectors A and B if TOTAL(A*B) is 0 then A and B are perpendicular.


Component of a Vector along a Direction
The dot product of an arbitrary vector, A, and a unit vector, U_hat, is the length of the component of A parallel to U_hat. Dot products are scalars. To get the actual vector component of A parallel to U_hat the dot product must be multiplied by the unit vector U_hat: A_par = (A dot U_hat) U_hat.

In IDL: if A is an arbitrary vector and U is a unit vector, then the component of A parallel to U is given by TOTAL(A*U)*U.

For a list of vectors, V, a list of the components of V parallel to a unit vector U is in IDL: U#(V##U)
which gives a list of vectors, one for each vector in V.
Transforming coordinates to a new system
This is very useful for transforming a set of vectors (or points) from one coordinate system to another. Set up unit vectors along the directions of the new x, y, and z axes. Find the components of the original vectors along these unit vectors to get the x, y, and z coordinates in the new coordinate system. If the original axis unit vectors are also transformed this way they can be used to transform the points back to the original coordinate system.


Finding a Perpendicular Vector to a single Vector
For a 2-d vector, A = (Ax, Ay), a vector perpendicular to it is B = (-Ay, Ax). The vector -B = (Ay,-Ax) is also perpendicular to A.

A method for 3-d vectors is the following. For vector A = (Ax,Ay,Az) find the smallest component (absolute value) and set the corresponding element of perpendicular vector B to 0. The remaining two elements of B are the remaining two elements of A switched and with one of them negated. For example, if Ay has the smallest absolute value, then B = (-Az, 0, Ax) is a vector perpendicular to A as may be seen by showing that the dot product is 0. The 2-d case given above is seen to be a special case of this method.


Cross Product: Finding a Perpendicular Vector to two Vectors
The vector cross product is very useful for finding a vector that is perpendicular to two other, non-colinear vectors. For two vectors, A = (Ax, Ay, Az) and B = (Bx, By, Bz), the cross product is A x B = (Ay Bz - Az By)i_hat + (Az Bx - Ax Bz)j_hat + (Ax By - Ay Bx)k_hat = (Ay Bz - Az By, Az Bx - Ax Bz, Ax By - Ay Bx), where i_hat, j_hat, and k_hat are unit vectors along the x, y, and z axes respectively.

For 2-d vectors contained in the XY plane the cross product is a vector parallel to the z axis, and (Ax By - Ay Bx) is the signed area of the parallelogram with sides along A and B.

In general (3-d vectors included) |A x B| is the area (non-signed) of the parallelogram. If vectors A and B are parallel or antiparallel, then the area is 0 and the magnitude of the cross product is also 0.

In IDL: a function to compute C = A x B is called CROSS. If A and B are 3-D vectors, then the call is C = CROSS(A,B).


Resolving vectors into orthogonal components
The following figure shows the resolution of a vector into orthogonal components.

Let V_par and V_perp be orthogonal components of V, then
V = V_par + V_perp.
If V_par is the component of V that is parallel to the unit vector U_hat then
V_par = (V dot U_hat) U_hat.
It follows that V_perp = V - V_par.

In IDL: if V is a vector and U a unit vector, then
V_PAR = total(V*U)*U
V_PERP = V - V_PAR