Jump to content

Matrix/Practice/Matrix multiplication: Difference between revisions

From Wikibase
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Line 80: Line 80:


4. $A^2 + AB + BA + B^2$ and $A^2 + 2AB + B^2$ with $A^2 = AA$. Conclude.
4. $A^2 + AB + BA + B^2$ and $A^2 + 2AB + B^2$ with $A^2 = AA$. Conclude.
{{Hidden solution}}
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$.
<syntaxhighlight lang="python" class="pyodide" copy>
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)
</syntaxhighlight>
{{End hidden}}


== Exercise 3 ==
== Exercise 3 ==

Revision as of 09:32, 19 November 2026

Languages: English · français · Esperanto

The solutions are programmatic: the code in each