Vectors

This is certainly the most difficult data type, because a vector consists of more than on value. The individual compontents can be integers or floats. In 2D space a vector is represented by two values:

vector = ( X, Y )

These values are actually nothing more than coordinates, but the most obvious problem is surely how to calculate with such a data type? Before this question is answered, it is a good idea to clarify the term “vector” first. A vector can be seen as an arrow, pointing in a certain direction. To describe a vector’s direction, we will need at least two dimensions: X and Y. By drawing a vector into a coordinate system, we get the main properties: direction and magnitude. The direction tells you which way the vector is pointing, while the magnitude is the length of the arrow. 

If you consider gravity as a vector, the direction would be vertical and its magnitude or length would be the strength of gravity at a certain point. Since gravity is not constant, you would have to draw a new arrow or vector at each measuring point. The result is a so-called vector field.

 

2D vectors with different length and orientation.

 

The graphical illustration is useful to get an idea of what a vector is, but currently it does not seem to tell us very much about how to calculate with vectors. How can we perform operations with arrows? Well, the first thing is to tell you a few things about notations. RealFlow is a 3D program, so we have to introduce a third coordinate and instead of “vector”, we are just using letters. The letter is the variable in this case! Each 3D vector consists of X, Y and Z coordinates. These coordinates can be any positive or negative number, including zero. So a complete vector could be written as:

a = ( X, Y, Z )

That is basically enough, but with more vectors we have to index the X, Y and Z coordinates somehow. So a better notation would be:

a = ( ax, ay, a)

b = ( bx, by, b)

Each coordinate ax, ay, az is called a scalar. Vector calculations can be performed by either using the individual scalars or the vector’s magnitude (or length). There are some basic operations you should know.

Vector Addition

c = a + b

( cx, cy, c) = ( ax, ay, a)  + ( bx, by, b

( cx, cy, c) = ( ax + bx, ay + by, az + b)

 

In this case all a components are added individually with their b counterpart and the result is again a triplet of scalars, resulting in vector c.

Vector Subtraction 

c = a - b

( cx, cy, c) = ( ax, ay, a)  - ( bx, by, b)

( cx, cy, c) = ( ax - bx, ay - by, az - b)

 

This operation is the same as the previously discussed vector addition. Again, you receive a vector c consisting of three scalars.

Scalar Product or Dot Product

c = a * b

c = ( ax, ay, a) * ( bx, by, b)

c = ( ax * bx + ay * by + az * b)

 

This operation can be considered as the multiplication of vectors. As with the previous examples, all components are multiplied individually with their appropriate counterpart, but here the coordinates are also added. The result is again a scalar, not a vector.

 

A special case is the square of a vector:

c = ax2 + ay2  + az2

 

By extracting the square root, “sqrt”, you will get the magnitude or length of a vector, which is a scalar:

m = sqrt( ax2 + ay2  + az)

Multiplication with a Scalar

c = a * s

( cx, cy, c) = ( ax, ay, a)  * s

( cx, cy, c) = ( ax * s, ay * s, az * s )

 

In this case there is only a single value, a scalar, that is simply multiplied with each coordinate as factor. The scalar s can be any positive floating or integer number, including 0.

Cross Product

c = a x b

cx = ay * bz - az * by 

cy = az * bx - ax * bz 

cz = ax * by - ay * bx

c = ( cx, cy, c)

 

The term a x b is pronounced "a cross b". This operation is more complex and therefore split into three lines for the individual components of the resulting vector. The cross product is very common with physical formulas dealing with natural phenomena. Many forces, for example the Lorentz force in magnetic fields or vortex forces, are calculated with the cross product. 

Vector Division

The division of two vectors like c = a : b is actually not defined and therefore not possible by default. But you can divide a vector by a scalar, in just the same way you have seen before under “Multiplication with a Scalar”.

c = a / s

( cx, cy, c) = ( ax, ay, a) / s

( cx, cy, c) = (ax / s, ay / s, az / s)

or:

c = a * 1 / s

( cx, cy, c) = ( ax, ay, a) * 1 / s

( cx, cy, c) = (ax * 1 / s, ay * 1 / s, az * 1 / s)

 

Since some vector calculations are rather time-consuming to write and often lead to unwanted mistakes, RealFlow offers a variety ready-to-use implementations of these rules. With them it is possible to perform vector calculations like any other algebraic operation. These are the vector operators:

 

Additiona + b
Subtractiona - b
Magnitudea.module()
Cross producta.cross(b)
Divisiona / b

 

 

Vector multiplications (a * b) and multiplications with scalars (a * s) must be performed manually according to the rules shown above.