[1 2 4 6 5 9]
PHOT 110: Introduction to programming
LECTURE 08: Arrays, vectors & linear algebra (Ch. 5)
Michaël Barbier, Spring semester (2023-2024)
For a list:
If you do not grow them !
\[ [a_1, .. , a_N] \rightarrow [a_1, .. , a_N, a_{N+1}] \]
[[6 0]
[7 4]
[2 9]]
Type of the elements is: int32
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[1.2 1.2 1.2]
[1.2 1.2 1.2]
[1.2 1.2 1.2]
[1.2 1.2 1.2]
[1.2 1.2 1.2]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
a = [[0. 0. 0. 0.]
[0. 0. 0. 0.]]
b = [[4.5 4.5 4.5 4.5]
[4.5 4.5 4.5 4.5]]
[[ 3. 0.34 ]
[ 0.546 34.9 ]
[ 5.3462 9. ]]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
For more on domains see the Numpy meshgrid howto.
[i]
[start:end+1:step]
a = np.array([[ 1, 2, 3],[ 4, 5, 6],[ 7, 8, 9]])
print(f"a[2, 0] = {a[2, 0]}")
print(f"a[0:2:2, :] = {a[0:2:2, :]}")
a[2, 0] = 7
a[0:2:2, :] = [[1 2 3]]
math.sin()
\(\rightarrow\) np.sin()
A vector is a 1D array, e.g. a column or a row vector: \[ \vec{a} = \pmatrix{a_1\\a_2\\ \vdots \\ a_M} \qquad \vec{b} = \pmatrix{b_1 & b_2 & \dots & b_N} \]
A vector is a 1D array, e.g. a column or a row vector: \[ \vec{a} = \pmatrix{a_1\\a_2\\ \vdots \\ a_M} \qquad \vec{b} = \pmatrix{b_1 & b_2 & \dots & b_N} \]
A row vector:
The scalar product \(\vec{a} \cdot \vec{b}\) is defined as scalar value
\[ \vec{a} \cdot \vec{b} = |a| |b| \cos(\theta) \]
with \(\theta\) the angle between \(a\) and \(b\)
Alternative definition of the scalar product \(\vec{a} \cdot \vec{b}\):
\[ \vec{a} \cdot \vec{b} = \sum_{i=1}^d a_i \, b_i \] where \(d\) is the dimension: 2D, 3D, …, ND
Scalar product \(\vec{a} \cdot \vec{b}\) can be computed in Numpy using np.dot
:
[ 1 -1] . [1.5 2.5] = -1.0
Scalar product \(\vec{a} \cdot \vec{b}\) can be computed in Numpy using np.dot
:
[ 1 -1] . [1.5 2.5] = -1.0
\[ |\vec{a}| = \sqrt{\vec{a}\cdot\vec{a}} = | \pmatrix{a_1 & a_1 & \dots & a_d} | = \sqrt{(\sum_{i=1}^d a_i^2)} \]
The vector product \(\vec{a} \times \vec{b}\) is defined as vector \(\vec{c}\) with
Alternative definition (suitable for computers):
\[ \begin{aligned} \vec{a} \times \vec{b} & = \det\pmatrix{ \vec{e_1} & \vec{e_2} & \vec{e_3}\\ a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\} \\ & \\ & = \begin{vmatrix} a_2 & a_3\\ b_2 & b_3\\ \end{vmatrix}\vec{e_1} - \begin{vmatrix} a_1 & a_3\\ b_1 & b_3\\ \end{vmatrix} \vec{e_2} + \begin{vmatrix} a_2 & a_3\\ b_2 & b_3\\ \end{vmatrix} \vec{e_3} \\ & \\ & = (a_2 b_3 - a_3 b_2) \vec{e_1} - (a_1 b_3 - a_3 b_1) \vec{e_2} + (a_1 b_2 - a_2 b_1) \vec{e_2} \\ \end{aligned} \]
Calculate \(\vec{a} \times \vec{b}\) using the Numpy function np.cross(a, b)
:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
The matrix product between matrices \(A\) and \(B\) is defined as
\[ \begin{aligned} A \cdot B & = \pmatrix{ a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ } \pmatrix{ b_{11} & b_{12} & b_{13}\\ b_{21} & b_{22} & b_{23}\\ b_{31} & b_{32} & b_{33}\\ }\\ &\\ & = \sum_{j} a_{ij} b_{jk}\\ \end{aligned} \]
The matrix product \(A \cdot B\) using Numpy np.matmul()
:
[[ 1 -2 3]
[ 0 2 4]]
Alternatives to np.matmul(A, B)
for \(A \cdot B\) using Numpy:
@
operator: A @ B
, andnp.dot(A, B)
[[-2 -3 -5]
[-4 8 8]]
The determinant of a matrix \(A\): \[ \begin{aligned} \det(A) & = \det\pmatrix{ a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\} \\ & \\ & = \begin{vmatrix} a_{22} & a_{23}\\ a_{32} & a_{33}\\ \end{vmatrix} a_{11} - \begin{vmatrix} a_{21} & a_{23}\\ a_{31} & a_{33}\\ \end{vmatrix} a_{12} + \begin{vmatrix} a_{21} & a_{22}\\ a_{31} & a_{32}\\ \end{vmatrix} a_{13} \\ &\\ & = (a_{22} a_{33} - a_{23} a_{32}) a_{11} - \dots \\ \end{aligned} \]
Use Numpy numpy.linalg.det()
function
[[ 1 -2 3]
[ 0 2 4]
[-1 1 0]]
Determinant is given by
The inverse \(A^{-1}\) of a matrix \(A\) is defined as:
\[ A^{-1} A = A^{-1} A = \mathbb{1} \]
The inverse \(A^{-1}\) of a matrix \(A\) is defined as:
\[ A^{-1} A = A^{-1} A = \mathbb{1} \] Since \(\det(A) \neq 0 \rightarrow A^{-1}\) exists
The inverse \(A^{-1}\) of a matrix \(A\) is defined as:
\[ A^{-1} A = A^{-1} A = \mathbb{1} \]
Verify \(A^{-1} A = \mathbb{1}\):
array([[ 1.00000000e+00, -2.77555756e-17, -5.55111512e-17],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[ 1.11022302e-16, 5.55111512e-17, 1.00000000e+00]])
Lecture 08: Arrays, vectors & linear algebra (Ch. 5)