NUMPY
The NumPy library
So essentially what NumPy does is provide the user with a powerful object called ndarray (but generally referred to simply as array) that expands the functionalities of Python quite a bit.
Loading the library
import numpy as np
a=np.zeros((4))
print(a)
[0. 0. 0. 0.]
From this point on, in the code examples written in this tutorial the line import numpy as np will be omitted. They will all work, as long as you have already loaded NumPy in another cell.
Creating arrays
a=np.array([1,2,3,4])
b=np.array([[1,2],[3.5, 4.2]])
print("a={0}".format(a))
print("b={0}".format(b))
a=[1 2 3 4]
b=[[1. 2. ]
[3.5 4.2]]
If you know the dimensions of an array that you are going to need, but you want to populate its positions later, you can create arrays of zeros and ones:
a=np.zeros((3))
b=np.ones((2,3), dtype=int)
print("a={0}".format(a))
print("b={0}".format(b))
a=[0. 0. 0.]
b=[[1 1 1]
[1 1 1]]
To create an array that contains a sequence of numbers that goes from a starting value to a finishing value, you can use the linspace() function:
a=np.linspace(1,10,6)
print("a={0}".format(a))
a=[ 1. 2.8 4.6 6.4 8.2 10. ]
Retrieving an array's dimension and dtype
- ndarray.ndim: returns the number of axes of the array
- ndarray.shape: returns the dimensions of the array as a tuple. Essentially it returns the number of elements in each axis
- ndarray.size: the total number of elements in the array
- ndarray.dtype: returns the dtype of the elements of the array
a=np.array([[1,2],[3,4]])
print(a.ndim)
print(a.shape)
print(a.size)
print(a.dtype)
2
(2, 2)
4
int32
Access and modify the elements of an array
a=np.array([2.45, 3.75])
a[0]=16
b=np.array([[0,1],[2,3]])
print(a[0])
print(b[1,1])
16.0
3
[i,j]
If you remember, for lists the same operation was made using this syntax instead:
[i][j]
Be careful not to confuse the two!
As we did for lists, we can change the value stored at a certain position of the array, like we see in line 2.
Mathematical operations between arrays
a=np.array([1,2,3,4]) b=np.array([2,4,8,16]) print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a**2)
print(np.sqrt(b))
[ 3 6 11 20]\
[ -1 -2 -5 -12]
[ 2 8 24 64]
[0.5 0.5 0.375 0.25 ]
[ 1 4 9 16]
[1.41421356 2. 2.82842712 4. ]
a=np.array([[1,2],[3,4]])
b=np.array([[2,4],[8,16]])
print(a@b)
[[18 36]
[38 76]]
Array slicing
a=np.linspace(0,10,11) print(a)
print(a[2:5])
print(a[1:8:2])
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
[2. 3. 4.]
[1. 3. 5. 7.]
For multidimensional arrays the syntax is the same, except each axis of the array can be sliced independently. To understand what this means, let's go over code the next piece of code:
mat=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print(mat[:,2])
print(mat[3,:])
print(mat[:,2:4])
[ 3 7 11 15]
[13 14 15 16]
[[} 3 4]
[ 7 8]
[11 12]
[15 16]]
NOTE: You may have noticed that when slicing using the semicolon, NumPy cuts the array from the starting position to the ending position excluded. This is why line 4 does not give an error, even tough the index 4 is out of bounds for an array with 4 columns. (remember that arrays are indexed from 0!)
Array union and concatenation
a=np.array([1,1])
b=np.array([2,2])
c=np.array([3,3])
print(np.hstack((a,b,c)))
print(np.vstack((a,b,c)))
[1 1 2 2 3 3]
[[1 1]
[2 2]
[3 3]]
a=np.array([1,2,3])
print(a)
a=np.append(a,4)
print(a)
a=np.insert(a,2,7)
print(a)
[1 2 3]
[1 2 3 4]
[1 2 7 3 4]
NumPy offers many other functionalities on top of the ones explained in the previous sections. So many, in fact, that they would require a separate book to be explained in their entirety. In the next chapters we will introduce some of them, however we will focus our attention only on the ones that are most useful and relevant to the practical goals that we have.
EXAMPLE: calculating the internal forces of a beam using NumPy
Problem definition
\[q=20\;kN/m\]
\[l=5\;m\]
Input the problem's quantities in Python
import numpy as np
In the next cell, we store the length of the beam and the value of the distributed load in two variables:
l=5 #m q=20 #kN/m