Linear Systems and Vector Spaces

Today, you will learn about solving systems of linear equations using Sage. 

{{{id=6| /// }}}

1. Solving Linear Systems of Equations

a. Symbolic Systems (using the solve command)

You can create a system of linear equations that literally looks like a system of equations, then solve it for all the variables using the solve command.  

{{{id=282| var('x') f = 3*x^3 + 5*x -7 == 5 /// }}} {{{id=285| f /// 3*x^3 + 5*x - 7 == 5 }}} {{{id=289| show(f) ///
\newcommand{\Bold}[1]{\mathbf{#1}}3 \, x^{3} + 5 \, x - 7 = 5
}}} {{{id=286| type(f) /// }}} {{{id=287| bool(f) /// False }}} {{{id=288| show(f.solve(x)) ///
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, {\left(\frac{1}{27} \, \sqrt{3041} + 2\right)}^{\left(\frac{1}{3}\right)} {\left(I \, \sqrt{3} + 1\right)} + \frac{{\left(\left(-5 I\right) \, \sqrt{3} + 5\right)}}{18 \, {\left(\frac{1}{27} \, \sqrt{3041} + 2\right)}^{\left(\frac{1}{3}\right)}}, x = -\frac{1}{2} \, {\left(\frac{1}{27} \, \sqrt{3041} + 2\right)}^{\left(\frac{1}{3}\right)} {\left(-I \, \sqrt{3} + 1\right)} + \frac{{\left(\left(5 I\right) \, \sqrt{3} + 5\right)}}{18 \, {\left(\frac{1}{27} \, \sqrt{3041} + 2\right)}^{\left(\frac{1}{3}\right)}}, x = {\left(\frac{1}{27} \, \sqrt{3041} + 2\right)}^{\left(\frac{1}{3}\right)} + \frac{-5}{9 \, {\left(\frac{1}{27} \, \sqrt{3041} + 2\right)}^{\left(\frac{1}{3}\right)}}\right]
}}} {{{id=283| 3*x < 5 /// 3*x < 5 }}} {{{id=281| /// }}} {{{id=225| var('x,y') solve([2*x + 3*y == 5, 10*x + sqrt(2)*y == 1], [x,y]) /// [[x == -36/223*sqrt(2) + 35/446, y == 24/223*sqrt(2) + 360/223]] }}}

NOTE (!):  Above, we use == (double equals) to create symbolic equations.  When you accidentally put = (single equals), you will get a syntax error. 

In the following example, there are infinitely many solutions:

{{{id=213| var('x,y') solve([2*x + 3*y == 5, 4*x + 6*y == 10], [x,y]) /// [[x == -3/2*r5 + 5/2, y == r5]] }}} {{{id=290| solve(sin(x) == 0, x) /// [x == 0] }}}

The main advantage of this approach is that it is so similar to the mathematics.     However, it is cumbersome and potentially slow if the system is large.   

Another advantage is that exactly the same format works for non-linear equations:

{{{id=230| var('x,y') solve([2*x^2 + 3*y^2 == 5, 4*x^2 + 6*y^3 == 10], [x,y]) /// [[x == -1/2*sqrt(2)*sqrt(5), y == 0], [x == 1/2*sqrt(2)*sqrt(5), y == 0], [x == -1, y == 1], [x == 1, y == 1]] }}}

We plot the zero loci of the two equations above, along with the set of intersection points, as computed by the solve command.  Note the use of the solution_dict=True parameter to obtain this solutions in an easier to use format. 

{{{id=227| var('x,y') S = solve([2*x^2 + 3*y^2 == 5, 4*x^2 + 6*y^3 == 10], [x,y], solution_dict=True) print S G = implicit_plot(2*x^2 + 3*y^2 == 5, (x,-2,2), (y,-2,2)) G += implicit_plot(4*x^2 + 6*y^3 == 10, (x,-2,2), (y,-2,2), cmap='hsv') G += points([(a[x], a[y]) for a in S], pointsize=50) G /// [{y: 0, x: -1/2*sqrt(2)*sqrt(5)}, {y: 0, x: 1/2*sqrt(2)*sqrt(5)}, {y: 1, x: -1}, {y: 1, x: 1}] }}} {{{id=232| /// }}} {{{id=236| /// }}}

Problem:  Use the Sage solve command to solve the following system of three equations in three unknowns:

$2x + 3y + 5z = 7$
$11x +13 y +17 z = 19$
$23x + 29y + 31z = 37$

{{{id=238| /// }}} {{{id=235| /// }}}

Problem: Use the Sage solve command to the following system of equations (note: Sage can't find exact solutions, so it returns numerical ones):

$2x^3 + \sqrt{2} \cdot y = x$
$x^2 + y^2 = 5$

{{{id=240| /// }}} {{{id=239| /// }}} {{{id=231| /// }}} {{{id=217| /// }}}

b. Solving Linear Systems Using Matrices

In a linear algebra class you learn how to encode a system of linear equations as a matrix equation:

$$ A \cdot x = b $$

where $A$ is a matrix, $x$ is an unknown vector, and $b$ is a vector.   Sage has excellent support for solving a system of linear equations presented this way.  

Recall:

{{{id=244| var('x,y') solve([2*x + 3*y == 5, 10*x + sqrt(2)*y == 1], [x,y]) /// [[x == -36/223*sqrt(2) + 35/446, y == 24/223*sqrt(2) + 360/223]] }}}

In terms of matrices:

{{{id=248| A = matrix(2, 2, [2,3, 10, sqrt(2)]); A /// [ 2 3] [ 10 sqrt(2)] }}} {{{id=243| b = vector([5, 1]); b /// (5, 1) }}} {{{id=250| x = A.solve_right(b); x /// (36/(sqrt(2) - 15) + 5/2, -24/(sqrt(2) - 15)) }}} {{{id=291| A.solve_left(b) /// (65/2/(sqrt(2) - 15) + 5/2, -13/2/(sqrt(2) - 15)) }}} {{{id=249| A*x == b /// True }}} {{{id=292| A*x /// (5, -24*sqrt(2)/(sqrt(2) - 15) + 360/(sqrt(2) - 15) + 25) }}} {{{id=295| zz = (A*x)[1]; zz /// -24*sqrt(2)/(sqrt(2) - 15) + 360/(sqrt(2) - 15) + 25 }}} {{{id=296| zz.simplify_full() /// 1 }}} {{{id=294| b /// (5, 1) }}}

Instead of solve_right, you can write A \ b like in MATLAB.

{{{id=242| A \ b /// (36/(sqrt(2) - 15) + 5/2, -24/(sqrt(2) - 15)) }}} {{{id=298| A = random_matrix(QQ, 4); B = random_matrix(QQ, 4) /// }}} {{{id=299| X = A \ B ; X /// [-48/31 -57/31 -53/31 14/31] [ 6/31 11/31 -41/62 -19/62] [ 30/31 86/31 68/31 -32/31] [ -7/31 -49/31 -9/62 24/31] }}} {{{id=300| A * X == B /// True }}} {{{id=301| X = A.solve_left(B); X /// [ 6/31 21/62 -18/31 -71/62] [-96/31 18/31 40/31 -21/31] [-51/31 -39/62 29/31 15/31] [-13/31 -15/31 8/31 2/31] }}} {{{id=297| X * A == B /// True }}} {{{id=302| A.transpose() /// [ 1 2 1 2] [ -1 -2 -2 2] [-1/2 2 1/2 2] [ -1 2 -2 1] }}}

Note that solve_right just finds one solution.  It gives no information about how many solutions there are.

{{{id=253| var('x,y') solve([2*x + 3*y == 5, 4*x + 6*y == 10], [x,y]) /// [[x == -3/2*r6 + 5/2, y == r6]] }}} {{{id=255| A = matrix(QQ, 2, 2, [2,3, 4,6]) b = vector([5,10]) A.solve_right(b) /// (5/2, 0) }}} {{{id=305| show(A) ///
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 2 & 3 \\ 4 & 6 \end{array}\right)
}}}

If you know linear algebra, you'll know that you can get all solutions by finding one solution, and adding to it any element of the right kernel of $A$.  This right kernel is the set of vector $v$ such that $Av = 0$.

{{{id=252| V = A.right_kernel(); V /// Vector space of degree 2 and dimension 1 over Rational Field Basis matrix: [ 1 -2/3] }}} {{{id=306| type(V) /// }}} {{{id=257| var('r') * vector([1,-2/3]) + vector([5/2, 0]) /// (r + 5/2, -2/3*r) }}} {{{id=258| /// }}}

Efficiency:  If the matrix A has entries in the rational numbers or double precision floating point (e.g., RDF or CDF), then solving $Ax = b$ should be (not "is") very fast.   In fact, Sage is one of the fastest programs in the world for doing this. Let's try some benchmarks.

A 50x50 system gets solved almost instantly:

{{{id=262| n = 50 A = random_matrix(ZZ, n) b = random_matrix(ZZ, n, 1) time x = A.solve_right(b) /// Time: CPU 0.03 s, Wall: 0.03 s }}} {{{id=308| print A.str() /// [ 1 7 7 -1 -1 -5 1 1 -1 1 -1 1 2 -2 4 2 -1 -45 1 -1 1 372 -1 -7 -2 3 -1 1 1 -9 -1 -16 1 -1 -1 2 1 -1 -1 -10 1 1 -5 -4 11 2 -5 1 14 6] [ 1 -67 -1 3 -117 8 -1 -3 -1 -17 -1 -1 -13 -3 2 6 -1 -2 1 -1 2 -2 1 -8 2 1 1 1 -2 1 -6 -49 -1 1 -1 -1 -1 -3 1 -1 1 -1 2 1 2 1 -1 2 2 6] [ 1 1 -21 -3 -1 -1 20 -1 1 1 1 -2 1 -99 -26 1 14 4 1 -3 1 1 2 42 3 -3 1 -1 -1 4 -5 5 2 1 1 3 -1 -1 68 8 2 -4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-2 1 1 -1 5 -6 1 1 -2 1 1 4 -2 -14 1 -1 -1 -2 37 -1 -1 -10 -6 1 1 1 1 1 2 3 -2 -3 -4 -2 -2 5 2 4 1 -13 -1 -7 -1 1 -1 -1 -2 -18 -10 1] [ 1 2 3 -14 2 -1 2 1 13 -55 -2 1 -1 -1 -1 2 -4 1 1 1 -3 1 -1 -1 -1 -1 -1 -2 3 -3 4 -1 1 -1 1 1 -1 5 1 -7 -6 1 5 1 -1 -1 -1 -1 1 -1] [ -117 -1 -1 12 -1 -6 1 -2 1 -3 -3 1 1 -4 1 1 -1 1 1 1 -1 -1 9 14 18 -1 -3 11 -11 -2 -1 -1 -2 2 5 -3 4 3 -3 -1 -1 -6 2 2 2 -1 1 -1 4 -1] [ -1 3 1 -1 -1 1 5 10 -2 -1 -1 1 -2 -11 3 1 6 -1 1 1 1 -6 -1 1 1 -1 -1 1 -24 -1 -1 1 -1 -3 2 -14 -3 2 12 1 1 -1 6 -5 -1 -3 67 -5 -1 3] [ 2 1 -3 -1 -1 12 -12 2 -5 -2 1 -1 -2 -4 -1 -2 -2 1 1 -1 -2 -2 -20 1 4 -1 2 -1 1 -1 -2 -4 1 2 -15 -1 -7 -1 -13 1 3 2 -3 -2 -1 -1 14 -1 1 2] [ 8 1 5 -1 -2 -1 4 1 2 1 -12 42 -1 -1 19 1 3 1 -2 -1 -3 8 1 1 2 -1 1 1 1 -1 3 2 6 -3 1 6 -2 -1 4 21 14 -1 -5 -4 -2 1 -1 -1 -1 -3] [ 1 -5 1 1 13 -1 -1 -1 -1 1 -1 4 13 -1 5 8 -1 -5 -5 -1 -1 -5 -4 10 20 7 1 -1 -5 -1 -1 2 -2 -1 1 -1 -10 -1 -3 1 1 -1 8 -1 1 -1 -8 -4 3 -1] }}} {{{id=312| denom = 8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257 /// }}} {{{id=313| A.det() /// -8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257 }}} {{{id=311| print x.str() /// [ 110093174356767707244637626815411176976880671764284139174329013269862704158263159144560405705626513032/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 165301191280756793671183886408329361052695999902874211120897574010909029019219983741965668079201480047/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 1982541788177486126996680487952767558361377064815223148331996580387446387430072417706679850071033127062/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 23255191997183481331338033313380302028548682281926495519316958264463266040238861679623000207396240530/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -262093295823651719389810263017174563789197751350461292168059085390346862667687884550390825372864164770/2861927715295762156838965314280395645623621275744119387219261987698050815198241146669418173700132419] [-7958572126718953095455803384993865935790032877259375476476356044407461085506979698494552549166467102824/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 5291361492444042337132755724613541656962983890386925052200861412157568869423595412053797233857985277750/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 1120284941470426091700041732173208067970484134838843671586226218945477277460275738103336142963407306522/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -111611956866468535565764481965547467011789708995214973130344356934279403121159754739070178159070259448/953975905098587385612988438093465215207873758581373129073087329232683605066080382223139391233377473] [ 1653936974485949387971437033138696984357677689689600420240103261370002832362431163197809904720145086236/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -237189475969977906861415698212270862055312666243049904934134497366723866735632422190078692441529076769/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -72533865801223377671499262042238517730054660717221322710845929588175241981144937337158177761868975645/953975905098587385612988438093465215207873758581373129073087329232683605066080382223139391233377473] [ 167861474918744109076340440712594915229665192827845717885173385087208754337984642900679921281726839225/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 308038712762840044943232863292759695445862983386759880725056726708403597149702189916874195608936694155/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 829830301329265853568587083398926631919849339797254399286949569130752052798440750177054347349537948244/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 1377594743258067503254985391382041416094841070109372160410253681377601333230352274886497763762719749822/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -97996311275650175213459027915048839894031520846865166355121574980634370129642660551670970359381686468/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 2197278025518672723120335063480197863546956051783893940988438466660639383956578853700420428005413804934/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 140363494730887204074276992660370064787368239771908949858669337565598542679456518783232009902841279172/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 73016193905250749557799223332448302552377542162019650666885639452872209415837522845671428980682947295/953975905098587385612988438093465215207873758581373129073087329232683605066080382223139391233377473] [ 7882738577777795883876933329164668695444528933192224557997113358514111498281048619789049497106525259004/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 1091604164343817176309917695671499373680724699582810638570694257326312047304696991857961736765687069008/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [-2536973578762410346988372373135129820291911567404741334553091382769769637231325354681554434327518796545/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 2645730350856093223591150807103887516981708559001213714191350739925531704573922919705720439596248691555/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -31604694204145957910252403504995393665318407696719825991754462552637615943532393726053326018141779359/136282272156941055087569776870495030743981965511624732724726761318954800723725768889019913033339639] [ 216869702621629876058191086841126109389109474079785390139633494626237658526823398188685568817702045834/408846816470823165262709330611485092231945896534874198174180283956864402171177306667059739100018917] [ 832105696205651504898286701926495490916450513782563537740790033377578886536250502654385881875681932866/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 2417309797337061187988048393567735127174195360734261211461062030505211112558901744894177642645740909178/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 1434910243042375791584468987424752335682075661072624846426087930655683313219258886374566458747546789261/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 1823703689328852320387403865641842080211783455134269249385798139083465711244695075913751872515703146367/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 103539574007689619345671060983005211019749912619953070014135763410274480999823510571479030341865990849/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -372095977321782781379259527931494752279954390431035269565056078000435400858156799507983814726470227795/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -152479659484629477471655200842119387745398691972111306408389491505284639796036237219529475121245447565/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 473587807819709734202401273655773580831584053980673724732743808075623284237314701330565354699849259161/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 68017932475414961412197169633552686218738799738117378757378046272811427784269581679175251823704429021/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 1443466125378431512441006067258466672707972238676827707213279591195698769603880797017762006692811794173/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 16057340171537860710867251564891198796077554258618111430622485283919381524617006382940987425518283107/136282272156941055087569776870495030743981965511624732724726761318954800723725768889019913033339639] [ 2564480431109337314430666951825425931287713431847398314451449917719613210218642629456825137948931933323/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 58621982052671057242990433487831122839347204394731642516870041519792093352627904768949711199009604/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [-2298459950466680873742818044118928805929410422565015694121852808440877559741919763984646352014482248904/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -109407319082425666344114601329885556409409704314774858238082745452458182233707656235652540910349188001/1226540449412469495788127991834455276695837689604622594522540851870593206513531920001179217300056751] [ 258347779526252657288734792753032507102676791638123080796249441953535398245396099031988769903020443244/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 9343302863848918233628007496099866093167798743720496752094893608190336375003421927651460232059969683921/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ -144373313175447130957539962748053212331668228128267253390693094887258979536235942357785763786692791926/2861927715295762156838965314280395645623621275744119387219261987698050815198241146669418173700132419] [ 643918615168584330578343141823873627190575468449929671228529066107923104422446145643475653267776929473/953975905098587385612988438093465215207873758581373129073087329232683605066080382223139391233377473] [ 267273001586673893738338683179747118348361455982256644133060215756914410201172652499702503573023598443/953975905098587385612988438093465215207873758581373129073087329232683605066080382223139391233377473] [ 3044787888575867468629580739510004277967556847305297934156003236683020660716574571148571439057615255355/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 573228884710650426946901808492514331525257135020467739045138027107754549763263073733544899183423719567/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] [ 3657889326062127524432395073202073987925583363258294353562041708113190200229117418418161542441629831936/2861927715295762156838965314280395645623621275744119387219261987698050815198241146669418173700132419] [ 521128072132154417617794024517063719032740699294861661495362143577447255322674657605176209429341778040/8585783145887286470516895942841186936870863827232358161657785963094152445594723440008254521100397257] }}} {{{id=310| /// }}} {{{id=309| /// }}}

A 500x500 system only takes a few seconds:

{{{id=261| n = 500 A = random_matrix(ZZ, n) b = random_matrix(ZZ, n, 1) time x = A.solve_right(b) /// Time: CPU 2.65 s, Wall: 3.41 s }}}

Writing down the solution vector takes about 1.5 million characters !

{{{id=263| len(x.str()) /// 1406999 }}}

Here's the first coordinate, which is a huge rational number.

{{{id=259| x[0] /// (-89807882522446475830350686593829008784496159757047955127163983443871006013232786022225682638190403132665748585130556226869348007423642226977442704548231313008385390933147526374085481820232518491489521161225143317172464246885203903889641167532260042080730658953212922883372788539971907086160726924260203429870993203084847451657919929485595548904663462475391547714979271546298592605828613248340742597174912254134359702617473348207178645441056459686316814425027694378263137292244920075860454987023444634148565057226764952008189193882976502344521289318862460474207463124335075519281781804113809964753192592614747581162045692922781461839718017401422406179499133871581335442914252085428943598598044088794571123860690508898067317044014500091818044137350261031406825982966180144307416972957890090829578132624248028016235423363334454594844021769021091525990074460481890719359684709261615693333966653300695692072825409719619876025272864114553605586634954526796201907792965866557548705993451240053602055703325760791699692491522930612510213163449136519259251311838296934605846685227555751018956116389102986716734360167966433801726094105032615096590795953756057709394836649826122772878435031852757957836888255948625078419689214115951435594044693281959111627784341111936206181180072986270053169047816389171235109474882563144564893183243319102684239309991231273186939759449808271981491982603467808352119653802171550443/187393223086867140752118521966821737808681299800876234538223177203757497885466044168883727751793913986650167909798409900008775673786484135759590364479564822357534550625024321898676960152028841466555877882707555848347559127894864728356929085724995470429847672608713673571705637736950930560404080981422906796979198845466222196918765974619858526044695342228343521667798063894889681649173385812838153628875127898646948556279870638660892890045256646902875533722758968959748644544065806521749904183915498357061518367161408026401171146471228906155467429993925895949187467686045921553120349416617935481796545986399401410953611884984884194543096026037445922077250575306211369198601444330809047501330857491850561348832396086064204336870851725562609993962498168702334704236132662657897417091517089563900912206537199046368719973331771800688689012359524231544895230395977721186179766999176924968791183624971477158500914499533776496726303796897940631144330989056462335893597823557705302256875172715881916220890524467509248732472596661469774453833622738549801755623964999416476667692044113888606588850502466223793927135329494299538249715683366487280691408239221326288779511675326951111857500974406889929702107675490299107842496258317395276520156702833967041274150910035863125640829997106454098491705079404267971495323908590665655299132582642992161302952944254843835086336753198671073378774660599913428804656336858053033) }}}

Why so fast?

{{{id=271| /// }}}

Here is a numerical example:

{{{id=270| n = 50 A = random_matrix(RDF, n) b = random_matrix(RDF, n, 1) time x = A.solve_right(b) /// Time: CPU 0.19 s, Wall: 0.19 s }}} {{{id=216| n = 200 A = random_matrix(RDF, n) b = random_matrix(RDF, n, 1) time x = A.solve_right(b) /// Time: CPU 12.20 s, Wall: 12.94 s }}}

Yuck, that is DOG SLOW, because evidently nobody has implemented a fast solve_right using numpy yet (bummer!).  We'll talk a lot more about numpy on Friday.

{{{id=314| A.numpy() /// array([[ 0.82158837, -0.83891932, 0.07219063, ..., 0.39105053, -0.48804913, -0.09668646], [ 0.27486603, 0.63627402, 0.72382737, ..., 0.0853531 , 0.14670969, 0.2315571 ], [-0.61024147, -0.25164937, 0.74271985, ..., 0.44411971, -0.78147722, -0.21390113], ..., [ 0.24168969, -0.67818189, -0.7744996 , ..., 0.62237813, -0.13230021, 0.63540967], [ 0.4147547 , -0.67801834, -0.28771367, ..., -0.51365367, -0.92851258, -0.03620117], [-0.96420877, -0.03472246, 0.46792467, ..., -0.61131182, 0.08691858, 0.95212629]]) }}} {{{id=215| import numpy time x = matrix(numpy.linalg.solve(A.numpy(), b.numpy())) /// Time: CPU 0.00 s, Wall: 0.00 s }}}

Sanity check:

{{{id=212| (A*x - b)[:10] /// [ 2.72004641033e-15] [ 5.21804821574e-15] [-5.66213742559e-15] [-1.93178806285e-14] [ 3.47499806708e-14] [ 2.05391259556e-15] [ 3.94129173742e-14] [ 2.22044604925e-16] [-3.60822483003e-15] [ 1.28785870857e-14] }}} {{{id=221| n = 500 A = random_matrix(RDF, n) b = random_matrix(RDF, n, 1) import numpy time x = matrix(numpy.linalg.solve(A.numpy(), b.numpy())) /// Time: CPU 0.04 s, Wall: 0.03 s }}} {{{id=222| /// }}}

Problem:  Use Sage matrices to solve the following system of three equations in three unknowns:

$2x + 3y + 5z = 7$
$11x +13 y +17 z = 19$
$23x + 29y + 31z = 37$

{{{id=173| /// }}} {{{id=208| /// }}} {{{id=277| /// }}}

2. Determinants and Characteristic Polynomials

{{{id=7| /// }}}

Determinants

{{{id=107| a = random_matrix(ZZ,100,x=-10000, y=10000) /// }}} {{{id=162| time a.det() /// -69118183991412206953466185620027755051094593987436743975854114751962407732206197752129747942002326479718116822417617968749730782507892952272012342333249634499152604652939010776686311855496604587857557933799729370253408728949060729398831603659403561659091824875875389423343892901050412653386331065509112138065968585040783220082284767990771793759565430224142468833249483340754121949955588912132225899076383208854337485961677095985993616580294086225721265052 Time: CPU 0.05 s, Wall: 0.05 s }}} {{{id=126| a = random_matrix(ZZ,500,x=-9,y=9) /// }}} {{{id=124| time a.determinant() /// -218130694939179587287661047753506262679352794010799323952037296550643652218042823841879033092074871913405924109523891512852671056670572678585023033583201032231170959602231511508643695133134388588239024413691308999124330421203345824357230269306010441224051155813227818747143464857579930936524588082877265952319039169254784202073721262184159268275981036018909396897240510593955791576073492129956773880834264614933551013848789710965999258592965791562478697971011944591069390598351008253974884796979736678544152750345242233792858950028618944637150215549538175432094522355953390499105637045088461852071087810643688659769845125270856336449415787996152641758929628659140816862121148915407496416143895568622530654424461499749542326547152477363967592105002657060019819068308429463108317233407555026085263177931555985335739557524003171620866393019284960113545649434794487748174558953438868249059053899270455621017654785852382397039009358200 Time: CPU 2.21 s, Wall: 2.12 s }}} {{{id=164| /// }}}

Characteristic Polynomial

{{{id=10| a = random_matrix(ZZ, 5) show(a) ///
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrr} -3 & 3 & 1 & 10 & 1 \\ -1 & 1 & 5 & 2 & -1 \\ 1 & -1 & 2 & -8 & 2 \\ -1 & 1 & 1 & -1 & -3 \\ -4 & 1 & -6 & 1 & 1 \end{array}\right)
}}} {{{id=174| show(charpoly(a)) ///
\newcommand{\Bold}[1]{\mathbf{#1}}x^{5} + 35x^{3} + 67x^{2} + 752x + 1050
}}} {{{id=175| a = random_matrix(ZZ, 10) /// }}} {{{id=176| time f = a.charpoly() f /// Time: CPU 0.00 s, Wall: 0.00 s x^10 - 2*x^9 + 187*x^8 - 705*x^7 - 769*x^6 + 414933*x^5 - 3111459*x^4 + 46472522*x^3 + 251007537*x^2 - 839624249*x + 13831127716 }}} {{{id=211| /// }}} {{{id=210| /// }}}

3. Vector Spaces

A vector space is a a mathematical object -- like a ring -- that you will probably know about if you've taken linear algebra.   

In Sage, every vector space is just the set of linear combinations of a finite list of vectors. 

Sage fully supports vector spaces as mathematical objects in their own right.

Vector spaces arise natural as the kernels of matrices:

{{{id=224| A = random_matrix(QQ, 2, 4); A /// [ 1 -1 -2 1] [ 1 1 -2 -1] }}} {{{id=223| V = A.right_kernel(); V /// Vector space of degree 4 and dimension 2 over Rational Field Basis matrix: [ 1 0 1/2 0] [ 0 1 0 1] }}} {{{id=209| vector([0,2,1,0]) in V /// False }}} {{{id=278| V.basis() /// [ (1, 0, 1/2, 0), (0, 1, 0, 1) ] }}} {{{id=279| /// }}} {{{id=280| /// }}}