3. Vector operations I#
In engineering mathematics, we often have to work with vector quantities, like force, position, electric field, etc. In scientific computing in Python, these vectors are encoded as one-dimensional NumPy arrays, and these exercises walk you through how to create them.
Summary of commands#
In this exercise, we will demonstrate the following:
type()
- Function to display the type of an object.The NumPy package for scientific computing, specifically
np.array()
- Constructor for NumPy arrays.np.linspace()
- Function for creating evenly spaced arrays between endpoints.np.arange()
- Function for creating evenly spaced arrays with a step size.
Manual enumeration#
One way to create a vector is to manually specify each element.
We’ll first construct a list data structure in Python and use the type()
function to confirm.
my_list = [1, 2, 3]
print(type(my_list))
print(my_list)
<class 'list'>
[1, 2, 3]
Now, the above looks like a vector, but we’re not quite done yet. We have to convert it to an array using the canonical scientific computing library NumPy.
Unlike MATLAB, in Python we have to explicitly import any special packages we want to use, such as:
import my_package_name
my_package_name.special_function()
And then any functions we want to use from that package will have the package name prepended with a period. But, since NumPy is used so frequently, the community has a special paradigm to abbreviate the package import (called an alias) as follows:
import numpy as np
np.special_function()
This saves your fingers a bit, and we will adopt this practice as well.
We will import NumPy and then convert the list from above to a NumPy array using the np.array()
constructor.
import numpy as np
my_array = np.array(my_list)
print(type(my_array))
print(my_array)
<class 'numpy.ndarray'>
[1 2 3]
Note how the printed array looks the same as the list, but it is a different class.
Note
We should mention: Not only do you have to import special packages, but you also have to install them separately from the base Python. Luckily, if you’re using Google Colab, all of the packages we’re using are common enough to come pre-installed.
Now we’ll do a series of tasks:
Display each element. We can select array elements by using the appropriate index,
array[ind]
.Multiple the first and second elements. We use the standard
*
operator with the selected elements.Change the third element to
4
and display the new vector on the screen.
Important
Python is 0-indexed which means the first element of an array is array[0]
, not 1
!
This is one of the biggest differences between Python and MATLAB.
# Display each element
print(my_array[0])
print(my_array[1])
print(my_array[2])
print()
# Multiply the first and second elements
print(my_array[0] * my_array[1])
print()
# Change the third element to 4
my_array[2] = 4
print(my_array)
1
2
3
2
[1 2 4]
Helper functions#
Creating vectors manually isn’t bad when there are only a few elements, but it would be quite annoying if you had dozens or hundreds of entries! Luckily, there are some convenient functions that can be employed depending on your use case.
I want exactly \(N\) equally spaced values#
Here you want to use the np.linspace(start, stop, num)
function, which accepts as arguments:
start
: The first value of your array. Required.stop
: The last value of your array. Required.num
: The total number of elements in your array, equally spaced between start and stop inclusive (by default). If you do not specify this parameter, the default isnum=50
.
my_array2 = np.linspace(0, 10, 6)
print(my_array2)
[ 0. 2. 4. 6. 8. 10.]
Some notes:
In Python, functions can be called with explicit parameter names, if you want. So
np.linspace(start=0, stop=10, num=6)
also would’ve worked.You can use
array.shape
to check the dimensions of the vector you constructed.array.size
returns the total number of elements (essentially a product of all array dimensions).
Note
NumPy can be a little tricky when it comes to vectors (1-D arrays) and whether it’s a row vector or column vector.
You’ll notice the shape below is abbreviated (6,)
.
We’ll discuss this nuance later when we encounter it.
my_array2_named = np.linspace(start=0, stop=10, num=6)
print(my_array2_named.shape)
print(my_array2_named.size)
(6,)
6
I want values equally spaced by \(\delta\)#
Here you want to use the np.arange(start, stop, step)
function, which accepts as arguments:
start
: The first value of your array. Default is0
, but best to always specify.stop
: The last value of your array, not inclusive. Required.step
: The increment between values starting fromstart
and going up to but not includingstop
. Default is1
, but best to always specify.
# not quite!
my_array3 = np.arange(0, 10, 2)
print(my_array3)
# better
my_array3_redo = np.arange(0, 11, 2)
print(my_array3_redo)
[0 2 4 6 8]
[ 0 2 4 6 8 10]