Jump to content

Matrix/Practice/Matrix multiplication: Difference between revisions

From Wikibase
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Automated edit (via update-page on MediaWiki MCP Server)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:


The solutions are programmatic: the code in each {{Hidden solution}} block runs in the page and follows [[matrix#Matrix operations in python|matrix § Matrix operations in python]], with NumPy for floating-point work and SymPy wherever an exact rational or symbolic result is wanted.
Reference: [[Matrix#Matrix operations in python|Matrix operations in python]]
 
The five exercises below come from a practical-work handout written for Matlab created by my beloved teacher [[Person:Gautier List|M. Gautier List]]<ref>{{#cite:Q3737}}</ref>, which we solve here with NumPy and SymPy.


== Exercise 1 ==
== Exercise 1 ==
Line 32: Line 34:
{{Hidden solution}}
{{Hidden solution}}


`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.
<code>np.dot</code> 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.


<syntaxhighlight lang="python" class="pyodide" copy>
<syntaxhighlight lang="python" class="pyodide" copy>
Line 232: Line 234:


3. $m = p \cdot q$
3. $m = p \cdot q$
{{Hidden solution}}
The matrix of a linear map in the canonical basis has, as its $j$-th column, the image of the $j$-th basis vector, which is the Jacobian of the map with respect to $(x,y,z)$. Composing maps multiplies their matrices in the same order: $p=g\circ f$ has matrix $\mathbf G\mathbf F$, and $q=f\circ g$ has matrix $\mathbf F\mathbf G$. Each matrix is cross-checked against the direct symbolic composition.
<syntaxhighlight lang="python" class="pyodide" copy>
import sympy as sp
x, y, z = sp.symbols("x y z")
vec = sp.Matrix([x, y, z])
f = sp.Matrix([x + 3*z, x + y + 4*z, 3*x + 3*z])
g = sp.Matrix([x - z, y, -x + y])
# columns are the images of the basis vectors e_1, e_2, e_3
F = f.jacobian(vec)
G = g.jacobian(vec)
def compose(outer, inner):
    return outer.subs(list(zip(vec, list(inner))), simultaneous=True)
# 1. p = g o f
P = G * F
print("P = G F =", P.tolist())
print("p(x, y, z) =", compose(g, f).T.tolist())
# 2. q = f o g
Q = F * G
print("Q = F G =", Q.tolist())
print("q(x, y, z) =", compose(f, g).T.tolist())
# 3. m = p . q
M = P * Q
print("M = P Q =", M.tolist())
print("m(x, y, z) =", compose(P * vec, Q * vec).T.tolist())
print("P Q == Q P :", P * Q == Q * P)
</syntaxhighlight>
{{End hidden}}
== References ==
<references />

Latest revision as of 04:36, 20 November 2026

Languages: English · français · Esperanto

Reference: Matrix operations in python

The five exercises below come from a practical-work handout written for Matlab created by my beloved teacher M. Gautier List[1], which we solve here with NumPy and SymPy.

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} $$

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.

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$

References