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=R5V = \mathbb{R}^5 och F3F^3 där F=GF(5)F = \mathrm{GF}(5).
V = RR^5 V print V
R5\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
F53\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()
5\displaystyle 5
F3.dimension()
3\displaystyle 3
Basvektorer:
V.basis()
[(1.00000000000000,0.000000000000000,0.000000000000000,0.000000000000000,0.000000000000000)\displaystyle \left(1.00000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000\right), (0.000000000000000,1.00000000000000,0.000000000000000,0.000000000000000,0.000000000000000)\displaystyle \left(0.000000000000000,\,1.00000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000\right), (0.000000000000000,0.000000000000000,1.00000000000000,0.000000000000000,0.000000000000000)\displaystyle \left(0.000000000000000,\,0.000000000000000,\,1.00000000000000,\,0.000000000000000,\,0.000000000000000\right), (0.000000000000000,0.000000000000000,0.000000000000000,1.00000000000000,0.000000000000000)\displaystyle \left(0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,1.00000000000000,\,0.000000000000000\right), (0.000000000000000,0.000000000000000,0.000000000000000,0.000000000000000,1.00000000000000)\displaystyle \left(0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000,\,1.00000000000000\right)]
F3.basis()
[(1,0,0)\displaystyle \left(1,\,0,\,0\right), (0,1,0)\displaystyle \left(0,\,1,\,0\right), (0,0,1)\displaystyle \left(0,\,0,\,1\right)]
Eftersom FF är en ändlig kropp så innehåller F3F^3 ett ändligt antal vektorer.
F3.cardinality() # antal element
125\displaystyle 125
F3.list() # satmliga element i vektorrummet
[(0,0,0)\displaystyle \left(0,\,0,\,0\right), (1,0,0)\displaystyle \left(1,\,0,\,0\right), (2,0,0)\displaystyle \left(2,\,0,\,0\right), (3,0,0)\displaystyle \left(3,\,0,\,0\right), (4,0,0)\displaystyle \left(4,\,0,\,0\right), (0,1,0)\displaystyle \left(0,\,1,\,0\right), (1,1,0)\displaystyle \left(1,\,1,\,0\right), (2,1,0)\displaystyle \left(2,\,1,\,0\right), (3,1,0)\displaystyle \left(3,\,1,\,0\right), (4,1,0)\displaystyle \left(4,\,1,\,0\right), (0,2,0)\displaystyle \left(0,\,2,\,0\right), (1,2,0)\displaystyle \left(1,\,2,\,0\right), (2,2,0)\displaystyle \left(2,\,2,\,0\right), (3,2,0)\displaystyle \left(3,\,2,\,0\right), (4,2,0)\displaystyle \left(4,\,2,\,0\right), (0,3,0)\displaystyle \left(0,\,3,\,0\right), (1,3,0)\displaystyle \left(1,\,3,\,0\right), (2,3,0)\displaystyle \left(2,\,3,\,0\right), (3,3,0)\displaystyle \left(3,\,3,\,0\right), (4,3,0)\displaystyle \left(4,\,3,\,0\right), (0,4,0)\displaystyle \left(0,\,4,\,0\right), (1,4,0)\displaystyle \left(1,\,4,\,0\right), (2,4,0)\displaystyle \left(2,\,4,\,0\right), (3,4,0)\displaystyle \left(3,\,4,\,0\right), (4,4,0)\displaystyle \left(4,\,4,\,0\right), (0,0,1)\displaystyle \left(0,\,0,\,1\right), (1,0,1)\displaystyle \left(1,\,0,\,1\right), (2,0,1)\displaystyle \left(2,\,0,\,1\right), (3,0,1)\displaystyle \left(3,\,0,\,1\right), (4,0,1)\displaystyle \left(4,\,0,\,1\right), (0,1,1)\displaystyle \left(0,\,1,\,1\right), (1,1,1)\displaystyle \left(1,\,1,\,1\right), (2,1,1)\displaystyle \left(2,\,1,\,1\right), (3,1,1)\displaystyle \left(3,\,1,\,1\right), (4,1,1)\displaystyle \left(4,\,1,\,1\right), (0,2,1)\displaystyle \left(0,\,2,\,1\right), (1,2,1)\displaystyle \left(1,\,2,\,1\right), (2,2,1)\displaystyle \left(2,\,2,\,1\right), (3,2,1)\displaystyle \left(3,\,2,\,1\right), (4,2,1)\displaystyle \left(4,\,2,\,1\right), (0,3,1)\displaystyle \left(0,\,3,\,1\right), (1,3,1)\displaystyle \left(1,\,3,\,1\right), (2,3,1)\displaystyle \left(2,\,3,\,1\right), (3,3,1)\displaystyle \left(3,\,3,\,1\right), (4,3,1)\displaystyle \left(4,\,3,\,1\right), (0,4,1)\displaystyle \left(0,\,4,\,1\right), (1,4,1)\displaystyle \left(1,\,4,\,1\right), (2,4,1)\displaystyle \left(2,\,4,\,1\right), (3,4,1)\displaystyle \left(3,\,4,\,1\right), (4,4,1)\displaystyle \left(4,\,4,\,1\right), (0,0,2)\displaystyle \left(0,\,0,\,2\right), (1,0,2)\displaystyle \left(1,\,0,\,2\right), (2,0,2)\displaystyle \left(2,\,0,\,2\right), (3,0,2)\displaystyle \left(3,\,0,\,2\right), (4,0,2)\displaystyle \left(4,\,0,\,2\right), (0,1,2)\displaystyle \left(0,\,1,\,2\right), (1,1,2)\displaystyle \left(1,\,1,\,2\right), (2,1,2)\displaystyle \left(2,\,1,\,2\right), (3,1,2)\displaystyle \left(3,\,1,\,2\right), (4,1,2)\displaystyle \left(4,\,1,\,2\right), (0,2,2)\displaystyle \left(0,\,2,\,2\right), (1,2,2)\displaystyle \left(1,\,2,\,2\right), (2,2,2)\displaystyle \left(2,\,2,\,2\right), (3,2,2)\displaystyle \left(3,\,2,\,2\right), (4,2,2)\displaystyle \left(4,\,2,\,2\right), (0,3,2)\displaystyle \left(0,\,3,\,2\right), (1,3,2)\displaystyle \left(1,\,3,\,2\right), (2,3,2)\displaystyle \left(2,\,3,\,2\right), (3,3,2)\displaystyle \left(3,\,3,\,2\right), (4,3,2)\displaystyle \left(4,\,3,\,2\right), (0,4,2)\displaystyle \left(0,\,4,\,2\right), (1,4,2)\displaystyle \left(1,\,4,\,2\right), (2,4,2)\displaystyle \left(2,\,4,\,2\right), (3,4,2)\displaystyle \left(3,\,4,\,2\right), (4,4,2)\displaystyle \left(4,\,4,\,2\right), (0,0,3)\displaystyle \left(0,\,0,\,3\right), (1,0,3)\displaystyle \left(1,\,0,\,3\right), (2,0,3)\displaystyle \left(2,\,0,\,3\right), (3,0,3)\displaystyle \left(3,\,0,\,3\right), (4,0,3)\displaystyle \left(4,\,0,\,3\right), (0,1,3)\displaystyle \left(0,\,1,\,3\right), (1,1,3)\displaystyle \left(1,\,1,\,3\right), (2,1,3)\displaystyle \left(2,\,1,\,3\right), (3,1,3)\displaystyle \left(3,\,1,\,3\right), (4,1,3)\displaystyle \left(4,\,1,\,3\right), (0,2,3)\displaystyle \left(0,\,2,\,3\right), (1,2,3)\displaystyle \left(1,\,2,\,3\right), (2,2,3)\displaystyle \left(2,\,2,\,3\right), (3,2,3)\displaystyle \left(3,\,2,\,3\right), (4,2,3)\displaystyle \left(4,\,2,\,3\right), (0,3,3)\displaystyle \left(0,\,3,\,3\right), (1,3,3)\displaystyle \left(1,\,3,\,3\right), (2,3,3)\displaystyle \left(2,\,3,\,3\right), (3,3,3)\displaystyle \left(3,\,3,\,3\right), (4,3,3)\displaystyle \left(4,\,3,\,3\right), (0,4,3)\displaystyle \left(0,\,4,\,3\right), (1,4,3)\displaystyle \left(1,\,4,\,3\right), (2,4,3)\displaystyle \left(2,\,4,\,3\right), (3,4,3)\displaystyle \left(3,\,4,\,3\right), (4,4,3)\displaystyle \left(4,\,4,\,3\right), (0,0,4)\displaystyle \left(0,\,0,\,4\right), (1,0,4)\displaystyle \left(1,\,0,\,4\right), (2,0,4)\displaystyle \left(2,\,0,\,4\right), (3,0,4)\displaystyle \left(3,\,0,\,4\right), (4,0,4)\displaystyle \left(4,\,0,\,4\right), (0,1,4)\displaystyle \left(0,\,1,\,4\right), (1,1,4)\displaystyle \left(1,\,1,\,4\right), (2,1,4)\displaystyle \left(2,\,1,\,4\right), (3,1,4)\displaystyle \left(3,\,1,\,4\right), (4,1,4)\displaystyle \left(4,\,1,\,4\right), (0,2,4)\displaystyle \left(0,\,2,\,4\right), (1,2,4)\displaystyle \left(1,\,2,\,4\right), (2,2,4)\displaystyle \left(2,\,2,\,4\right), (3,2,4)\displaystyle \left(3,\,2,\,4\right), (4,2,4)\displaystyle \left(4,\,2,\,4\right), (0,3,4)\displaystyle \left(0,\,3,\,4\right), (1,3,4)\displaystyle \left(1,\,3,\,4\right), (2,3,4)\displaystyle \left(2,\,3,\,4\right), (3,3,4)\displaystyle \left(3,\,3,\,4\right), (4,3,4)\displaystyle \left(4,\,3,\,4\right), (0,4,4)\displaystyle \left(0,\,4,\,4\right), (1,4,4)\displaystyle \left(1,\,4,\,4\right), (2,4,4)\displaystyle \left(2,\,4,\,4\right), (3,4,4)\displaystyle \left(3,\,4,\,4\right), (4,4,4)\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 uu ett element i VV och gäller det att xF3x \in F^3?
u in V
True\displaystyle \mathrm{True}
x in F3
True\displaystyle \mathrm{True}
Syntax för vektoraritmetik följer hur det ser ut i läroböcker (tänk på att i FF sker alla beräkningar modulo 5)
u + v
(1,5,2,1,0)\displaystyle \left(1,\,5,\,2,\,1,\,0\right)
3 * u - 5 * v
(3,17,6,5,8)\displaystyle \left(3,\,-17,\,6,\,-5,\,-8\right)
u.dot_product(v) # skalärprodukten av u och v
3\displaystyle 3
norm(u) # Euklediska normen av u
7\displaystyle \sqrt{7}
u.norm(Infinity) # maximumnormen av u
2\displaystyle 2
2*x - y
(4,2,0)\displaystyle \left(4,\,2,\,0\right)
x.cross_product(y) # vektorprodukten av x oh y
(3,4,3)\displaystyle \left(3,\,4,\,3\right)
Låt w=2u3vw = 2u - 3v och låt UU vara det linjära höljet som genereras av uu, vv och ww.
w = 2*u - 3*v U = V.subspace([u, v, w]) U
RowSpanR(1.000000000000000.0000000000000002.000000000000000.2500000000000001.250000000000000.0000000000000001.000000000000000.0000000000000000.2500000000000000.250000000000000)\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)
True\displaystyle \mathrm{True}
U.dimension()
2\displaystyle 2
Vektorn ww är onödig vid konstruktionen av UU eftersom den är en linjärkombination av uu och vv.
V.linear_dependence([u, v, w])
[(2,3,1)\displaystyle \left(2,\,-3,\,-1\right)]
Alltså är 2u3vw=02u -3v - w = 0, där 00 är nollvektorn.

Matriser

Precis som vektorer defineirar man matriser via en datatyp.
A = matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) A
(12345678)\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
(0125113)\displaystyle \left(\begin{array}{rr} 0 & -1 \\ 2 & 5 \\ 11 & -3 \end{array}\right)
C = matrix(2, 2, 5) C
(5005)\displaystyle \left(\begin{array}{rr} 5 & 0 \\ 0 & 5 \end{array}\right)
D = matrix(4, 1, [7, 6, 0, 2]) D
(7602)\displaystyle \left(\begin{array}{r} 7 \\ 6 \\ 0 \\ 2 \end{array}\right)
identity_matrix(5)
(1000001000001000001000001)\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
(0.0000000000000001.000000000000001.000000000000000.0000000000000000.0000000000000002.000000000000005.000000000000000.0000000000000001.000000000000000.00000000000000011.00000000000003.000000000000000.0000000000000000.0000000000000001.000000000000005.000000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000005.000000000000000.0000000000000000.0000000000000000.000000000000000)\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
(2787)\displaystyle \left(\begin{array}{r} 27 \\ 87 \end{array}\right)
A.transpose() # transponatet av A
(15263748)\displaystyle \left(\begin{array}{rr} 1 & 5 \\ 2 & 6 \\ 3 & 7 \\ 4 & 8 \end{array}\right)
D.transpose()
(7602)\displaystyle \left(\begin{array}{rrrr} 7 & 6 & 0 & 2 \end{array}\right)
D.transpose() * D
(89)\displaystyle \left(\begin{array}{r} 89 \end{array}\right)
E = D * D.transpose() E
(494201442360120000141204)\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
0\displaystyle 0
T.det()
25.0000000000000\displaystyle 25.0000000000000
T.inverse() # inversen till matrisen T
(0.0000000000000006.93889390390723×10180.0000000000000000.2000000000000001.66533453693774×10170.0000000000000005.55111512312578×10180.0000000000000001.11022302462516×10160.2000000000000001.000000000000005.55111512312578×10180.0000000000000001.11022302462516×10160.2000000000000000.0000000000000001.000000000000000.0000000000000000.3999999999999991.000000000000000.0000000000000006.66133814775094×10171.000000000000002.200000000000000.600000000000000)\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 uTuu \mapsto Tu och uuTu \mapsto uT som linjära avbildningar.
T * u
(1.00000000000000,7.00000000000000,7.00000000000000,5.00000000000000,5.00000000000000)\displaystyle \left(1.00000000000000,\,7.00000000000000,\,7.00000000000000,\,5.00000000000000,\,5.00000000000000\right)
u * T
(24.0000000000000,7.00000000000000,1.00000000000000,1.00000000000000,2.00000000000000)\displaystyle \left(24.0000000000000,\,-7.00000000000000,\,1.00000000000000,\,1.00000000000000,\,2.00000000000000\right)
Nollrummet till uEuu \mapsto Eu, d.v.s. det vektorrum vars element som avbildas på nollvektorn:
E.left_kernel()
RowSpanZ(200701030010)\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 uEuu \mapsto Eu:
E.column_space()
RowSpanZ(7602)\displaystyle \mathrm{RowSpan}_{\Bold{Z}}\left(\begin{array}{rrrr} 7 & 6 & 0 & 2 \end{array}\right)
Karakteristiska polynomet till matrisen TT.
T.charpoly('lambda')
λ55.00000000000000λ49.00000000000000λ3+66.0000000000000λ2+5.00000000000000λ25.0000000000000\displaystyle \lambda^{5} - 5.00000000000000 \lambda^{4} - 9.00000000000000 \lambda^{3} + 66.0000000000000 \lambda^{2} + 5.00000000000000 \lambda - 25.0000000000000
Egenvärden till TT.
T.eigenvalues()
[4.17683792998295+1.36763005398640i\displaystyle 4.17683792998295 + 1.36763005398640i, 4.176837929982951.36763005398640i\displaystyle 4.17683792998295 - 1.36763005398640i, 0.610587041516745\displaystyle 0.610587041516745, 0.637072524310718\displaystyle -0.637072524310718, 3.32719037717192\displaystyle -3.32719037717192]