Vector operations

In addition to being mathematical entities studied in linear algebra, Vectors are widely used in physics and engineering as a convenient way to represent physical quantities as displacement, velocity, acceleration, force, and so on. Accordingly, basic operations between vectors can be performed via Numpy/SciPy operations as follows:

Addition/subtraction of vectors does not require any explicit loop to perform them. Let's take a look at addition of two vectors:

The output is shown as follows:

Further, we perform subtraction on two vectors:

The output is shown as follows:

Numpy has the built-in function dot to compute the scalar (dot) product between two vectors. We show you its use computing the dot product of vectorA and vectorB from the previous code snippet:

The output is shown as follows:

Alternatively, to compute this product we could perform the element-wise product between the components of the vectors and then add the respective results. This is implemented in the following lines of code:

The output is shown as follows:

First, two vectors in 3 dimensions are created before applying the built-in function from NumPy to compute the cross product between the vectors:

The output is shown as follows:

Further, we perform a cross operation of vectorB over vectorA:

The output is shown as follows:

Notice that the last expression shows the expected result that vectorA cross vectorB is the negative of vectorB cross vectorA.