Maths - Vector Algebra

A vector is a set of elements which are operated on as a single object. The elements are often numbers but could be any mathematical object provided that it can be added and multiplied with acceptable properties, for example, we could have a vector whose elements are complex numbers.

Vector addition and subtraction is simple in that we just add or subtract corresponding terms. Vector multiplication is more complicated, there is not a general mutiplication that multiplies two vectors and produces another vector, however there are two types of multiplication that combine two vectors and produce another quantity.

vector add

Vector Add

Adding two vectors is equivalent to putting the tail of one vector against the head of the other. The order is not important, i.e. A + B = B + A.

This can be easily calculated by separately adding the x,y and z components.

vector subtract

Vector Subtract

Subtracting two vectors is equivalent to putting the head of the vectors together, the result is then the vector between the tail of the vectors. An alternative way to think of it is to reverse the direction of the vector to be subtracted, then add the vectors.

The resulting vector can be easily calculated by separately subtracting the x,y and z components.

dot product

Dot Product

The dot product operation combines two vectors and produces a scalar output.

It is defined as follows:

Ax * Bx + Ay * By + Az * Bz

cross product

Vector Cross Product

Both the two operands and the result of the cross product are vectors.

The vector cross product has some useful properties, it produces a vector which is mutually perpendicular to the two vectors being multiplied.

Dot Product

It is also given by A•B = |A| |B| cos(θ)

where:

It is also equal to

A * projection of B on A

or

B * projection of A on B

The following is Java code for vector dot product.

double dot(sfvec3f other) {
   return (x * other.x) + (y * other.y) + (z * other.z);
}

The following is managed C++ code for vector dot product.

Double sfvec3f::dot(sfvec3f* other) {
   return (x * other->x) + (y * other->y) + (z * other->z);
}

This method will be used by the sfvec3f class.

For further information about the dot product see this page.

Vector Cross Product

Both the two operands and the result of the cross product are vectors.

The vector cross product has some useful properties, it produces a vector which is mutually perpendicular to the two vectors being multiplied.

The vector cross product gives a vector which is perpendicular to both the vectors being multiplied. The resulting vector A × B is defined by:

x = Ay * Bz - By * Az
y = Az * Bx - Bz * Ax
z = Ax * By - Bx * Ay

where x,y and z are the components of A x B

The scalar length of A × B is,

|A × B| = |A| *|B| * sin(θ)

where θ is the angle between A and B.

The cross product interpretation only to 3D vectors. For instance in 2 dimensions it is not possible to find another vector which is mutually perpendicular to 2 arbitrary vectors, in 4 dimensions (or greater) there are many vectors which are mutually perpendicular to 2 arbitrary vectors.

The cross product can be used to calculate the Normal to a surface as shown here.

The cross product is also given by the determinant:

i j k
Ax Ay Az
Bx By Bz

where: i,j & k are unit vectors in the x,y and z dimensions.

Note:

It is important to remember that vector cross products only apply to vectors in 1,3 or 7 dimensions.

(one dimension less than the division algebras complex=2, quaternion=4,octonion=8).

There are extensions to vector algebra which does have a vector product which does work in other dimentions, for example,

Identities

A×B + B×A = 0 anti-commutative, vector cross multiplication is not commutative, changing the order reverses the direction of the resulting vector: A×B = - B×A.
A×(B×C) +
B ×(C×A) +
C ×(A×B) = 0

Jacobi identity : anti-associative : anti-symmetry
So cross multipication is not associative.

A × A = 0 A 'squared' is a zero length vector because sin(0) = 0
A × (B + C) = (A × B) + (A × C) cross multiplication for vectors is distributive over +
0 × A = 0  

Where:

For example, the following expression:

A × A × B

We might expect that this is zero, because A x A = 0, but this is not necessarily so because cross multiplication is not associative. We need to specify the order, if it is:

A × (A × B)

then the result is not necessarily zero.

Code

The following is java code for vector add.

void add(sfvec3f other){
   x+= other.x;
   y+= other.y;
   z+= other.z;
}

The following is managed C++ code for vector add.

Void sfvec3f::add(sfvec3f* other){
   x+= other->x;
   y+= other->y;
   z+= other->z;
}

This method will be used by the sfvec3f class.

 

The following is Java code for vector subtract.

Void sub(sfvec3f other){
   x-= other.x;
   y-= other.y;
   z-= other.z;
}

The following is managed C++ code for vector subtract.

Void sfvec3f::sub(sfvec3f* other){
   x-= other->x;
   y-= other->y;
   z-= other->z;
}

This method will be used by the sfvec3f class.

The following is Java code for vector cross product.

Void cross(sfvec3f other) {
   double xh = y * other.z - other.y * z;
   double yh = z * other.x - other.z * x;
   double zh = x * other.y - other.x * y;
   x = xh;
   y = yh;
   z = zh;
}

The following is managed C++ code for vector cross product.

Void sfvec3f::cross(sfvec3f* other) {
   double xh = y * other->z - other->y * z;
   double yh = z * other->x - other->z * x;
   double zh = x * other->y - other->x * y;
   x = xh;
   y = yh;
   z = zh;
}

This method will be used by the sfvec3f class.

For further information about the cross product see this page.

Scalar Product

There is another type of product that we often use, this has a scalar value as one of its inputs, this scalar is multiplied by the magnitude of the vector to change its magnitude. The direction of the vector does not change.

So this multiplication has a scalar and a vector as inputs and a vector as an output.

The code is:

Void sfvec3f::scalarProduct(double multiplier) {

    x = x * multiplier;
    y = y * multiplier;
    z = z * multiplier;
} 

Vector convert to unit length

Sometimes we just want the vector to represent a direction but not a distance. In that case we often normalised the vector to a unit length. Note normalising is not the same as generating a normal (a normal is usually normalised but a normalised vector is not always a normal).

The following is Java code for vector Normalise.

Void normalize(){
   double t = Math.sqrt(x*x + y*y + z*z);
   x /= t;
   y /= t;
   z /= t;
}

The following is managed C++ code for vector Normalise.

Void sfvec3f::normalize(){
   double t = Math::Sqrt(x*x + y*y + z*z);
   x /= t;
   y /= t;
   z /= t;
}

This method will be used by the sfvec3f class.

Vector triple product identities

If A,B and C are vectors and x is cross multiplication, then,

A × (B × C) = B (A·C) - C (A·B)

As explained above the dot product gives a scalar, so B and C are scaled by these scalars before they are subtracted.

and also,

(A × B) × C = - C × (A × B) reversing order of multiplication changes sign
  = - A (B·C) + B (A·C) using first vector triple product identity

Here are some triple product identities for dot product of cross product:

A · (B × C) = (B × C) · A

A · (B × C) = C · (A × B) = B· (C × A)

A · (B × C) = -A · (C × B)

Vector Division

Neither dot or cross vector multiplication have a unique inverse, but geometric multiplication does.

In the case of dot multiplication this converts from vector to scalar which looses information so it does not have an inverse.

In the case of cross multiplication there are many values of the vector B which will generate the result A × B so division does not give a unique value. The blue vectors in the diagram below shows some vectors that produce the same product as vector B.

inverse cross product

Given the direction of any potential inverse B' we can determine its length:

since: |A × B| = |A| *|B'| * sin(θ)

so: |B'| = |A × B| / ( |A| * sin(θ))

Thanks to Paolo De Leva for correcting this.

So, we can see that the cross product has no unique inverse, any attempt to calculate it will fail, I have tried using the triple product identity and also using the skew symmetric matrix. As predicted these methods failed as you can see here.

We want to find the inverse of the vector 'a' which is equivalent to solving the equation:

a-1 a = 1

To solve this equation we can use the property that, in geometric algebra, the square of a vector is a scalar, so,

a2 = |a|2

Combining these gives:

a-1 = a /|a|2

don't forget this last result only applies to geometric algebra.

Other vector quantities

Alternative interpretation of vectors

Upto now we have thought of the vector as the position on a 2,3 or n dimensional grid. However for some physical situations there may not be a ready defined Cartesian coordinate system. An alternative might be to represent the vector as a linear combination of 3 basis:

σ1
σ2
σ3

These basis don't have to be mutually perpendicular (although in most cases they probably will be) however they do have to be independent of each other, in other words they should not be parallel to each other and all 3 should not be in the same plane.

So a vector in 3 dimensions can be represented by [a,b,c] where a,b and c represent the scaling of the 3 basis to make the vector as follows:

a σ1 + b σ2 + c σ3

Note that if this vector represents position then it will be a relative position, i.e. relative to some other point, if we want to define an absolute point we still have to define an origin.

So the problem remains of how to define the basis, there may be some natural definition of these in the problem domain. Alternatively we could define the basis themselves as 3D vectors using a coordinate system. But why bother to do this, if we have a coordinate system why not just represent the vectors in this coordinate system? Well we might want to change coordinate systems or translate all the vectors in some way (see here). For example, we might want to represent points on a solid object in some local coordinate system, but the solid object may itself be moving relative to some absolute coordinate system.

Further Reading

Vectors can be manipulated by matrices, for example translated, rotated, scaled, reflected.

There are mathematical objects known a multivectors, these can be used to do many of the jobs that vectors do, but they don't have some of the limitations (for example vector cross product is limited to 3 dimensions and does not have an inverse).


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Introduction to 3D Game Programming with DirectX 9.0 - This is quite a small book but it has good concise information with subjects like, maths introduction and picking.

cover If you are interested in 3D games, this looks like a good book to have on the shelf. If, like me, you want to have know the theory and how it is derived then there is a lot for you here. Including - Graphics pipeline, scenegraph, picking, collision detection, bezier curves, surfaces, key frame animation, level of detail, terrain, quadtrees & octtrees, special effects, numerical methods. Includes CDROM with code.

This book contains more mathematically rigorous methods for picking than described above.

Other Math Books

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.