SharedSage genom exempel / Linjär_algebra.sagewsOpen in CoCalc
Author: Robert Nyqvist

# Linjär algebra

typeset_mode(True)


## Vektorrum

Låt oss som exempel utgå från vektorrummen $V = \mathbb{R}^5$ och $F^3$ där $F = \mathrm{GF}(5)$.
V = RR^5
V
print V

$\displaystyle \Bold{R}^{5}$
Vector space of dimension 5 over Real Field with 53 bits of precision
F = GF(5)
F3 = F^3
F3
print F3

$\displaystyle \Bold{F}_{5}^{3}$
Vector space of dimension 3 over Finite Field of size 5
Vi kan enkelt bestämma dimensionen för ett vektorrum.
V.dimension()

$\displaystyle 5$
F3.dimension()

$\displaystyle 3$
Basvektorer:
V.basis()

[$\displaystyle \left(1.00000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000\right)$, $\displaystyle \left(0.000000000000000,\,1.00000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000\right)$, $\displaystyle \left(0.000000000000000,\,0.000000000000000,\,1.00000000000000,\,0.000000000000000,\,0.000000000000000\right)$, $\displaystyle \left(0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,1.00000000000000,\,0.000000000000000\right)$, $\displaystyle \left(0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,1.00000000000000\right)$]
F3.basis()

[$\displaystyle \left(1,\,0,\,0\right)$, $\displaystyle \left(0,\,1,\,0\right)$, $\displaystyle \left(0,\,0,\,1\right)$]
Eftersom $F$ är en ändlig kropp så innehåller $F^3$ ett ändligt antal vektorer.
F3.cardinality()  # antal element

$\displaystyle 125$
F3.list()  # satmliga element i vektorrummet

[$\displaystyle \left(0,\,0,\,0\right)$, $\displaystyle \left(1,\,0,\,0\right)$, $\displaystyle \left(2,\,0,\,0\right)$, $\displaystyle \left(3,\,0,\,0\right)$, $\displaystyle \left(4,\,0,\,0\right)$, $\displaystyle \left(0,\,1,\,0\right)$, $\displaystyle \left(1,\,1,\,0\right)$, $\displaystyle \left(2,\,1,\,0\right)$, $\displaystyle \left(3,\,1,\,0\right)$, $\displaystyle \left(4,\,1,\,0\right)$, $\displaystyle \left(0,\,2,\,0\right)$, $\displaystyle \left(1,\,2,\,0\right)$, $\displaystyle \left(2,\,2,\,0\right)$, $\displaystyle \left(3,\,2,\,0\right)$, $\displaystyle \left(4,\,2,\,0\right)$, $\displaystyle \left(0,\,3,\,0\right)$, $\displaystyle \left(1,\,3,\,0\right)$, $\displaystyle \left(2,\,3,\,0\right)$, $\displaystyle \left(3,\,3,\,0\right)$, $\displaystyle \left(4,\,3,\,0\right)$, $\displaystyle \left(0,\,4,\,0\right)$, $\displaystyle \left(1,\,4,\,0\right)$, $\displaystyle \left(2,\,4,\,0\right)$, $\displaystyle \left(3,\,4,\,0\right)$, $\displaystyle \left(4,\,4,\,0\right)$, $\displaystyle \left(0,\,0,\,1\right)$, $\displaystyle \left(1,\,0,\,1\right)$, $\displaystyle \left(2,\,0,\,1\right)$, $\displaystyle \left(3,\,0,\,1\right)$, $\displaystyle \left(4,\,0,\,1\right)$, $\displaystyle \left(0,\,1,\,1\right)$, $\displaystyle \left(1,\,1,\,1\right)$, $\displaystyle \left(2,\,1,\,1\right)$, $\displaystyle \left(3,\,1,\,1\right)$, $\displaystyle \left(4,\,1,\,1\right)$, $\displaystyle \left(0,\,2,\,1\right)$, $\displaystyle \left(1,\,2,\,1\right)$, $\displaystyle \left(2,\,2,\,1\right)$, $\displaystyle \left(3,\,2,\,1\right)$, $\displaystyle \left(4,\,2,\,1\right)$, $\displaystyle \left(0,\,3,\,1\right)$, $\displaystyle \left(1,\,3,\,1\right)$, $\displaystyle \left(2,\,3,\,1\right)$, $\displaystyle \left(3,\,3,\,1\right)$, $\displaystyle \left(4,\,3,\,1\right)$, $\displaystyle \left(0,\,4,\,1\right)$, $\displaystyle \left(1,\,4,\,1\right)$, $\displaystyle \left(2,\,4,\,1\right)$, $\displaystyle \left(3,\,4,\,1\right)$, $\displaystyle \left(4,\,4,\,1\right)$, $\displaystyle \left(0,\,0,\,2\right)$, $\displaystyle \left(1,\,0,\,2\right)$, $\displaystyle \left(2,\,0,\,2\right)$, $\displaystyle \left(3,\,0,\,2\right)$, $\displaystyle \left(4,\,0,\,2\right)$, $\displaystyle \left(0,\,1,\,2\right)$, $\displaystyle \left(1,\,1,\,2\right)$, $\displaystyle \left(2,\,1,\,2\right)$, $\displaystyle \left(3,\,1,\,2\right)$, $\displaystyle \left(4,\,1,\,2\right)$, $\displaystyle \left(0,\,2,\,2\right)$, $\displaystyle \left(1,\,2,\,2\right)$, $\displaystyle \left(2,\,2,\,2\right)$, $\displaystyle \left(3,\,2,\,2\right)$, $\displaystyle \left(4,\,2,\,2\right)$, $\displaystyle \left(0,\,3,\,2\right)$, $\displaystyle \left(1,\,3,\,2\right)$, $\displaystyle \left(2,\,3,\,2\right)$, $\displaystyle \left(3,\,3,\,2\right)$, $\displaystyle \left(4,\,3,\,2\right)$, $\displaystyle \left(0,\,4,\,2\right)$, $\displaystyle \left(1,\,4,\,2\right)$, $\displaystyle \left(2,\,4,\,2\right)$, $\displaystyle \left(3,\,4,\,2\right)$, $\displaystyle \left(4,\,4,\,2\right)$, $\displaystyle \left(0,\,0,\,3\right)$, $\displaystyle \left(1,\,0,\,3\right)$, $\displaystyle \left(2,\,0,\,3\right)$, $\displaystyle \left(3,\,0,\,3\right)$, $\displaystyle \left(4,\,0,\,3\right)$, $\displaystyle \left(0,\,1,\,3\right)$, $\displaystyle \left(1,\,1,\,3\right)$, $\displaystyle \left(2,\,1,\,3\right)$, $\displaystyle \left(3,\,1,\,3\right)$, $\displaystyle \left(4,\,1,\,3\right)$, $\displaystyle \left(0,\,2,\,3\right)$, $\displaystyle \left(1,\,2,\,3\right)$, $\displaystyle \left(2,\,2,\,3\right)$, $\displaystyle \left(3,\,2,\,3\right)$, $\displaystyle \left(4,\,2,\,3\right)$, $\displaystyle \left(0,\,3,\,3\right)$, $\displaystyle \left(1,\,3,\,3\right)$, $\displaystyle \left(2,\,3,\,3\right)$, $\displaystyle \left(3,\,3,\,3\right)$, $\displaystyle \left(4,\,3,\,3\right)$, $\displaystyle \left(0,\,4,\,3\right)$, $\displaystyle \left(1,\,4,\,3\right)$, $\displaystyle \left(2,\,4,\,3\right)$, $\displaystyle \left(3,\,4,\,3\right)$, $\displaystyle \left(4,\,4,\,3\right)$, $\displaystyle \left(0,\,0,\,4\right)$, $\displaystyle \left(1,\,0,\,4\right)$, $\displaystyle \left(2,\,0,\,4\right)$, $\displaystyle \left(3,\,0,\,4\right)$, $\displaystyle \left(4,\,0,\,4\right)$, $\displaystyle \left(0,\,1,\,4\right)$, $\displaystyle \left(1,\,1,\,4\right)$, $\displaystyle \left(2,\,1,\,4\right)$, $\displaystyle \left(3,\,1,\,4\right)$, $\displaystyle \left(4,\,1,\,4\right)$, $\displaystyle \left(0,\,2,\,4\right)$, $\displaystyle \left(1,\,2,\,4\right)$, $\displaystyle \left(2,\,2,\,4\right)$, $\displaystyle \left(3,\,2,\,4\right)$, $\displaystyle \left(4,\,2,\,4\right)$, $\displaystyle \left(0,\,3,\,4\right)$, $\displaystyle \left(1,\,3,\,4\right)$, $\displaystyle \left(2,\,3,\,4\right)$, $\displaystyle \left(3,\,3,\,4\right)$, $\displaystyle \left(4,\,3,\,4\right)$, $\displaystyle \left(0,\,4,\,4\right)$, $\displaystyle \left(1,\,4,\,4\right)$, $\displaystyle \left(2,\,4,\,4\right)$, $\displaystyle \left(3,\,4,\,4\right)$, $\displaystyle \left(4,\,4,\,4\right)$]
Sage har en egen datatyp för vektorer - även om vi skulle kunna jämställa vektorer med listor (tänk på att addition av listor betyder konkatenering).
u = vector([1, 1, 2, 0, -1])
v = vector([0, 4, 0, 1, 1])

Man kan precisera till vilken kropp elementen i vektorn ska tillhöra.
x = vector(F, [1, 0, 4])
y = vector(F, [3, 3, 3])

Är $u$ ett element i $V$ och gäller det att $x \in F^3$?
u in V

$\displaystyle \mathrm{True}$
x in F3

$\displaystyle \mathrm{True}$
Syntax för vektoraritmetik följer hur det ser ut i läroböcker (tänk på att i $F$ sker alla beräkningar modulo 5)
u + v

$\displaystyle \left(1,\,5,\,2,\,1,\,0\right)$
3 * u - 5 * v

$\displaystyle \left(3,\,-17,\,6,\,-5,\,-8\right)$
u.dot_product(v)  # skalärprodukten av u och v

$\displaystyle 3$
norm(u)  # Euklediska normen av u

$\displaystyle \sqrt{7}$
u.norm(Infinity)  # maximumnormen av u

$\displaystyle 2$
2*x - y

$\displaystyle \left(4,\,2,\,0\right)$
x.cross_product(y)  # vektorprodukten av x oh y

$\displaystyle \left(3,\,4,\,3\right)$
Låt $w = 2u - 3v$ och låt $U$ vara det linjära höljet som genereras av $u$, $v$ och $w$.
w = 2*u - 3*v
U = V.subspace([u, v, w])
U

$\displaystyle \mathrm{RowSpan}_{\Bold{R}}\left(\begin{array}{rrrrr} 1.00000000000000 & 0.000000000000000 & 2.00000000000000 & -0.250000000000000 & -1.25000000000000 \\ 0.000000000000000 & 1.00000000000000 & 0.000000000000000 & 0.250000000000000 & 0.250000000000000 \end{array}\right)$
U.is_subspace(V)

$\displaystyle \mathrm{True}$
U.dimension()

$\displaystyle 2$
Vektorn $w$ är onödig vid konstruktionen av $U$ eftersom den är en linjärkombination av $u$ och $v$.
V.linear_dependence([u, v, w])

[$\displaystyle \left(2,\,-3,\,-1\right)$]
Alltså är $2u -3v - w = 0$, där $0$ är nollvektorn.

## Matriser

Precis som vektorer defineirar man matriser via en datatyp.
A = matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
A

$\displaystyle \left(\begin{array}{rrrr} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \end{array}\right)$
B = matrix(3, 2, [0, -1, 2, 5, 11,-3])  # specificera antal rader och kolonner
B

$\displaystyle \left(\begin{array}{rr} 0 & -1 \\ 2 & 5 \\ 11 & -3 \end{array}\right)$
C = matrix(2, 2, 5)
C

$\displaystyle \left(\begin{array}{rr} 5 & 0 \\ 0 & 5 \end{array}\right)$
D = matrix(4, 1, [7, 6, 0, 2])
D

$\displaystyle \left(\begin{array}{r} 7 \\ 6 \\ 0 \\ 2 \end{array}\right)$
identity_matrix(5)

$\displaystyle \left(\begin{array}{rrrrr} 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \end{array}\right)$
T = block_matrix(CC, [[B, 1], [C, 0]])
T

$\displaystyle \left(\begin{array}{rr|rrr} 0.000000000000000 & -1.00000000000000 & 1.00000000000000 & 0.000000000000000 & 0.000000000000000 \\ 2.00000000000000 & 5.00000000000000 & 0.000000000000000 & 1.00000000000000 & 0.000000000000000 \\ 11.0000000000000 & -3.00000000000000 & 0.000000000000000 & 0.000000000000000 & 1.00000000000000 \\ \hline 5.00000000000000 & 0.000000000000000 & 0.000000000000000 & 0.000000000000000 & 0.000000000000000 \\ 0.000000000000000 & 5.00000000000000 & 0.000000000000000 & 0.000000000000000 & 0.000000000000000 \end{array}\right)$
Matriseritmetik:
A * D

$\displaystyle \left(\begin{array}{r} 27 \\ 87 \end{array}\right)$
A.transpose()  # transponatet av A

$\displaystyle \left(\begin{array}{rr} 1 & 5 \\ 2 & 6 \\ 3 & 7 \\ 4 & 8 \end{array}\right)$
D.transpose()

$\displaystyle \left(\begin{array}{rrrr} 7 & 6 & 0 & 2 \end{array}\right)$
D.transpose() * D

$\displaystyle \left(\begin{array}{r} 89 \end{array}\right)$
E = D * D.transpose()
E

$\displaystyle \left(\begin{array}{rrrr} 49 & 42 & 0 & 14 \\ 42 & 36 & 0 & 12 \\ 0 & 0 & 0 & 0 \\ 14 & 12 & 0 & 4 \end{array}\right)$
E.det()  # determinanten av E

$\displaystyle 0$
T.det()

$\displaystyle 25.0000000000000$
T.inverse()  # inversen till matrisen T

$\displaystyle \left(\begin{array}{rrrrr} 0.000000000000000 & 6.93889390390723 \times 10^{-18} & 0.000000000000000 & 0.200000000000000 & -1.66533453693774 \times 10^{-17} \\ 0.000000000000000 & 5.55111512312578 \times 10^{-18} & 0.000000000000000 & -1.11022302462516 \times 10^{-16} & 0.200000000000000 \\ 1.00000000000000 & 5.55111512312578 \times 10^{-18} & 0.000000000000000 & -1.11022302462516 \times 10^{-16} & 0.200000000000000 \\ 0.000000000000000 & 1.00000000000000 & 0.000000000000000 & -0.399999999999999 & -1.00000000000000 \\ 0.000000000000000 & -6.66133814775094 \times 10^{-17} & 1.00000000000000 & -2.20000000000000 & 0.600000000000000 \end{array}\right)$
Vi kan betrakta $u \mapsto Tu$ och $u \mapsto uT$ som linjära avbildningar.
T * u

$\displaystyle \left(1.00000000000000,\,7.00000000000000,\,7.00000000000000,\,5.00000000000000,\,5.00000000000000\right)$
u * T

$\displaystyle \left(24.0000000000000,\,-7.00000000000000,\,1.00000000000000,\,1.00000000000000,\,2.00000000000000\right)$
Nollrummet till $u \mapsto Eu$, d.v.s. det vektorrum vars element som avbildas på nollvektorn:
E.left_kernel()

$\displaystyle \mathrm{RowSpan}_{\Bold{Z}}\left(\begin{array}{rrrr} 2 & 0 & 0 & -7 \\ 0 & 1 & 0 & -3 \\ 0 & 0 & 1 & 0 \end{array}\right)$
Värderummet till $u \mapsto Eu$:
E.column_space()

$\displaystyle \mathrm{RowSpan}_{\Bold{Z}}\left(\begin{array}{rrrr} 7 & 6 & 0 & 2 \end{array}\right)$
Karakteristiska polynomet till matrisen $T$.
T.charpoly('lambda')

$\displaystyle \lambda^{5} - 5.00000000000000 \lambda^{4} - 9.00000000000000 \lambda^{3} + 66.0000000000000 \lambda^{2} + 5.00000000000000 \lambda - 25.0000000000000$
Egenvärden till $T$.
T.eigenvalues()

[$\displaystyle 4.17683792998295 + 1.36763005398640i$, $\displaystyle 4.17683792998295 - 1.36763005398640i$, $\displaystyle 0.610587041516745$, $\displaystyle -0.637072524310718$, $\displaystyle -3.32719037717192$]