block runs in the page and follows
matrix § Matrix operations in python, with NumPy for floating-point work and SymPy wherever an exact rational or symbolic result is wanted.
Exercise 1
Calculate (when it exists) the matrix product $M = M_1 M_2$ with:
1.
$$
M_1 = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, \quad M_2 = \begin{pmatrix} 5 \\ 6 \end{pmatrix}
$$
2.
$$
M_1 = \begin{pmatrix} -2 & 2 & -3 \\ -1 & 1 & 3 \\ 2 & 0 & -1 \end{pmatrix}, \quad M_2 = \begin{pmatrix} 2 \\ 1 \\ -1 \end{pmatrix}
$$
3.
$$
M_1 = \begin{pmatrix} 3 & 4 & 16 \\ 45 & 4 & 0 \end{pmatrix}, \quad M_2 = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}
$$
4.
$$
M_1 = \begin{pmatrix} 3 & 2 & 1 & 2 \\ 1 & 4 & 0 & -1 \end{pmatrix}, \quad M_2 = \begin{pmatrix} -3 \\ 1 \\ 2 \\ 1 \end{pmatrix}
$$
5.
$$
M_1 = \begin{pmatrix} 0 & 0 & -1 \\ 2 & 1 & 0 \end{pmatrix}, \quad M_2 = \begin{pmatrix} 1 & 1 \\ 0 & -1 \\ -1 & 2 \end{pmatrix}
$$
`np.dot` gives the matrix product, not the element-wise one. The product exists only when the number of columns of $M_1$ equals the number of rows of $M_2$, which the code tests before multiplying.
import numpy as np
# the five pairs (M_1, M_2)
pairs = [
([[1, 2], [3, 4]], [[5], [6]]),
([[-2, 2, -3], [-1, 1, 3], [2, 0, -1]], [[2], [1], [-1]]),
([[3, 4, 16], [45, 4, 0]], [[1], [2], [3]]),
([[3, 2, 1, 2], [1, 4, 0, -1]], [[-3], [1], [2], [1]]),
([[0, 0, -1], [2, 1, 0]], [[1, 1], [0, -1], [-1, 2]]),
]
for i, (M_1, M_2) in enumerate(pairs, start=1):
M_1 = np.array(M_1)
M_2 = np.array(M_2)
compatible = M_1.shape[1] == M_2.shape[0] # columns of M_1 must equal rows of M_2
print(f"1.{i} M_1: {M_1.shape}, M_2: {M_2.shape}, product exists: {compatible}")
if compatible:
print(np.dot(M_1, M_2)) # matrix product, NOT np.multiply
print()
Exercise 2
Let $A$ and $B$ be two square matrices:
$$
A = \begin{pmatrix} 2 & -2 & 1 \\ -1 & 0 & 1 \\ 0 & 2 & -2 \end{pmatrix}, \quad B = \begin{pmatrix} 1 & -1 & 0 \\ -2 & 0 & 2 \\ 0 & 1 & -1 \end{pmatrix}
$$
Calculate the result of the following operations:
1. $\det(A)$, $\det(B)$, $A^T$, $B^T$.
2. $A^{-1}$. Deduce the comatrix of $A$.
Reminder:
$$
A^{-1} = \frac{1}{\det(A)} \operatorname{com}(A)
$$
3. $AB$, $BA$, and $A+B$.
4. $A^2 + AB + BA + B^2$ and $A^2 + 2AB + B^2$ with $A^2 = AA$. Conclude.
SymPy keeps the arithmetic exact, so $A^{-1}$ shows $3/2$ instead of a rounded float. The comatrix follows from the reminder, $\operatorname{com}(A)=\det(A)A^{-1}$, and both expansions are compared with $(\mathbf A+\mathbf B)^2$.
import sympy as sp
A = sp.Matrix([[2, -2, 1], [-1, 0, 1], [0, 2, -2]])
B = sp.Matrix([[1, -1, 0], [-2, 0, 2], [0, 1, -1]])
def show(name, M):
print(f"{name} =")
for row in M.tolist():
print(" ", row)
print()
# 1. determinants and transposes
print("det(A) =", A.det(), " det(B) =", B.det())
show("A^T", A.T)
show("B^T", B.T)
# 2. inverse, then comatrix through com(A) = det(A) A^-1
show("A^-1", A.inv())
show("com(A)", A.det() * A.inv())
# 3. products and sum
show("A B", A * B)
show("B A", B * A)
show("A + B", A + B)
# 4. the two expansions, with (A + B)^2 for comparison
E_1 = A**2 + A * B + B * A + B**2 # A**2 is AA
E_2 = A**2 + 2 * A * B + B**2
show("A^2 + AB + BA + B^2", E_1)
show("A^2 + 2AB + B^2", E_2)
show("(A + B)^2", (A + B)**2)
print("A^2 + AB + BA + B^2 == (A + B)^2 :", E_1 == (A + B)**2)
print("A^2 + 2AB + B^2 == (A + B)^2 :", E_2 == (A + B)**2)
print("AB == BA :", A * B == B * A)
Exercise 3
1. Calculate the scalar product of the vectors
$$
\vec{u} = \begin{pmatrix} 7 \\ 6 \\ 3 \end{pmatrix} \quad \text{and} \quad \vec{v} = \begin{pmatrix} 1 \\ -1 \\ 2 \end{pmatrix}
$$
using several methods.
2. Calculate the norm of the vector
$$
\vec{w} = \begin{pmatrix} 2 \\ 1 \\ -1 \end{pmatrix}
$$
using several methods.
Exercise 4
A mining company operates three deposits and extracts three minerals $A$, $B$, and $C$, whose three main constituents $a$, $b$, and $c$ are identical, but whose grades vary such that:
- 1000 tonnes of $A$ contain 600 tonnes of $a$, 200 tonnes of $b$, and 100 tonnes of $c$.
- 1000 tonnes of $B$ contain 700 tonnes of $a$, 100 tonnes of $b$, and 150 tonnes of $c$.
- 1000 tonnes of $C$ contain 700 tonnes of $a$, 150 tonnes of $b$, and 50 tonnes of $c$.
The company must produce in one month 20000 tonnes of $a$, 5000 tonnes of $b$, and 3000 tonnes of $c$, and seeks the respective quantities of the three minerals it must extract.
Let $x$, $y$, and $z$ be the respective quantities of minerals $A$, $B$, and $C$ in thousands of tonnes that must be extracted.
Put the problem in matrix form and determine the quantities of minerals $A$, $B$, and $C$ required to meet the specifications.
Exercise 5
Let $f$ and $g$ be linear maps from $\mathbb{R}^3$ to $\mathbb{R}^3$ defined in the canonical bases by:
$$
f(x,y,z) = \begin{cases} x + 3z \\ x + y + 4z \\ 3x + 3z \end{cases}, \quad g(x,y,z) = \begin{cases} x - z \\ y \\ -x + y \end{cases}
$$
Determine, in matrix form, the following linear maps (in the canonical bases):
1. $p = g \circ f$
2. $q = f \circ g$
3. $m = p \cdot q$