An Introduction to Sage

William Stein, Professor of Mathematics, University of Washington


{{{id=53| /// }}}

Mission Statement

Create a viable free open source alternative to Magma, Maple, Mathematica, and Matlab.

 

 

Firefox <--> Internet Explorer, Opera

Open Office, Latex <--> Microsoft Office

Linux <--> Microsoft Windows

PostgreSQL, MySQL <-->  Oracle, Microsoft SQLserver

GIMP <--> Photoshop

Sage <--> Magma, Maple, Mathematica, Matlab


History of Sage

 

What is Sage?

 

Tour of the http://sagemath.org website

 

Summary:
Sage is about
building the car instead of reinventing the wheel

  1. Sage uses Python,  a mainstream programming language, instead of inventing a custom mathematics language
  2. Use straightforward method to link programs together -- C library and pseudotty's, instead of XML servers/OpenMath.  We implement all conversion routines, instead of expecting upstream to do it: we make them communicate with Sage, whether they want to or not.
  3. Give copious credit to contributors and be very developer friendly (easily build from source).
  4. Reuse, improve, and contribute to existing libraries and projects (e.g., Scipy, Numpy, R, ATLAS, CVXopt, GSL), instead of starting over and competing with them.
  5. Make the GUI using a web browser: the world of java and javascript is available and Sage integrates with the web.

 

And now a demo...

 

{{{id=59| /// }}} {{{id=56| /// }}}

Very Big Numbers

Very big numbers: In less than a second, Sage exactly computes $(10^6)!$, which has over 5 million digits. 

{{{id=12| time n = factorial(10^6) /// Time: CPU 0.68 s, Wall: 0.79 s }}} {{{id=14| n.ndigits() /// 5565709 }}} {{{id=20| time m = n * (n+1) /// Time: CPU 0.38 s, Wall: 0.39 s }}}

and a matrix with a big determinant:

{{{id=63| a = random_matrix(ZZ,200, x=2^128); a /// 200 x 200 dense matrix over Integer Ring (type 'print a.str()' to see all of the entries) }}} {{{id=64| a = random_matrix(ZZ,200, x=2^128); a /// 200 x 200 dense matrix over Integer Ring (type 'print a.str()' to see all of the entries) }}} {{{id=61| a[0,0] /// 50110420315670456383223454274407788191 }}} {{{id=67| time a.determinant() /// 112181052052308097465663161901987233414079274767659577992031997992127035950126846293332383280971863257042078310032291276787887552961323974685923411424608861905155094502221014779181477505470517438611658177376836767601781307309727849749924982009178940882173698288144263815216885352831263837872621655396985941434034760470474317858708414313396158998234180606063510605953486065805079822775254478177764566297427885235838114631369591111154917051636835300946790153093828666657313155216166700506575420273478237005973112235762192331017991880478031663685663464846426893662651494065431407172656216181491348120736982373639973182712017836910871292657287998186479284276901219451891295981757472546956797435102431590434381351874066892787102840758266606997294517607195381763277556295324011746271913224171763879088155654520565560658530859032797215172357444883550180976882364514903423357037198335530020320410405277233100923890701970933947692628646843234266954221970817404594457141147355491934009191699170660505553453092671289278038692333777748653018217451460868825124567939497100519941796064932767217683240766207004421123731164865126688300864815575949209091475064071778855370066472360223338259131055717975641320671484183543028529369827671595430412051776298717467132923584667223880159769173072831097436866399376675906768535810426279962360175453862328734790267932709059632621613093376738977980270368366630352578338127373156179907282340782340736678988822848947589144685369360376577767432491376060076218207814715533901285983771352045053467791792005738002051106325901228319486500444738435607092961926278905577055288150553456427420465083264994865376732107996668602081719332818297302669965193705584814259008522309161391674501672506945295557969312435101081151542062804701451671374063373179721272049376996408792853040938545115912690500293709302997313378018125795256611927722335223496536293702321462602107682594640312111721291328567495537497210413732508523048519500318226538669037743905442701188546878390503065034337842041453601492293748492483624878827591405065846914781695344257236992087011017099492127043067470501515517817415615668417350206496850193538181984086673862827912169832623326215886404840607241991889868572045294347929686494593683542388740815378900873037200229816989422909025027206250569755261300744573872752558046183489168699456422886812977393967826767080836045931271060332836992538805068337689575201397753888291322317759706520184747620476857410204773970645983086356310661552254441606330879874593064242677546286103626357966755846032958303645171898999797096250589035496934402282408462209923236082585023615777735063540228817936856599771768113630169625182362644281072647684161201447026806264727589167865519787167582568720758062404958771721008046455130424038088456781748367770829099431571597744726213961923976688841355424714398359381241727136335376081067012647783868632047618998667059628807930716484954296510057086072184950039143993681486149443915013243797083171747371190036229296930436562859848198843798210556063895661249874314108436936312482886038587464966962811124657004751173555156762002783632125087578423695515043002995133967766409783857351340870839198644335782062277151790639641509614166659702542327889305800347913371764941228434973490528387628461933043158035007302828560662537998303084978630561024112613716632426696473008142644496738526567937776084918475541010007669523927860942321488018268875236583476282681478600596227511788387478919077138410089774541196800459740146975387065312514922828072500958467695419573104956540117190610466112178853677946240444068698517576708671292669674415631198689531441706120774600393775128069637984465034940093232857912721627841744477714927286781507785806042512963718599706252724988003027534478399561854905604403318646262925841547660612936888061935378106539794521630091904566579402035506722497028961906414343521675430389121135989621142000096578978043373815768956343967232920820822466970562592112851247480499198011465135089993217553230967682300954876068965907806611721612123492858508410518007643702614674519159228430352275454813107397164292531295744751555389884547522049619102163684658241116603522243430698837309797239678679314047240237820002677231818033523628047937641656390920727644335706604482555635419398113912873271671766880712376642662196369813310823936791058522279321121903106144999976821475384486204785955364356181689068216582513631822692817988987234828405602698455287216828810988452846605732897091612876154943246959768683340973093914871077384896823796790318475053029021934826744698321449906685816549678282275830566279207695608891350707645073149956181997781576109234607638827135743520736967214483627207318797313361996665539067501162722748211673333776365964012817947874494917914901480988314729675799878866768501917875075864655848975865896126969451641218286830855020120961402611654891039357940299734585109729902053873191862496407867333484329568878125327134150959964333549077912710040052145940658453784744522364219628963531204573436401517636226605626563524011644783648334215977764163855675989988417232866818430372581152399131419717698593683785049728618145666755298828910066311822727255317514442022478325769309951138928240281270667511657341873365898988873224596553669665937105875973687180723567142274218563744531003364463041227150027555213776384494054869933946040230978624093505535178373183958400133204038354090110317048948208739535208471127498924349336338782789700580708539353507415484637014366988255485683907339800865463176041637113078014592831417787683069352644717498646078821274735437350867332198001703853164028316591375260473933300589978177605122239741364564622536276140147890949525564540668263734975535006006223085003871431245413134571402179957599842219352768264950422026741523201525448557316771918869947348781111958285400738614901838622082779292067849016732305033225803784245931208211822487348641911506337346506377589668996013893592634535504826013088684938535437654090698380769515670342156287752189440247400844830687170884747541320661847202150566130998773472843812987283664482104189538641048214961439347586822151048821173563141372438567229512371648649123302823972293420035530147385935636288884479457228862192596498626350682729882099959106654289072207674039010824671334480214348294014324267676232243514360461659753081389330447645693227909809392289588702412868870275116354843799195714409335050898974761878834884086034406702438794621785801027925979339213338460004320721342068842794241270466273576913783009192152513207128876738178122825580600642326655347283761760719977403849496269905497208629920218046041477676424087606656233234475581374929424494494067910758506916034154460206510583254370274641316649205799203465755063472832739013882638610503093478264730038225225633441949124685235476118280853639901255118079928474220183410659421960003297055065694738477994998175638472033485873588027087694350383414909495060794798283352583071197916764842524290705490965005249579242782367461197832956232828472994795278831771891230017445868003833628733018766562619969798766880120825090154607014698473176409834495536384700805845152775761077434187793766976396191560527500417740232892661606324748796291397318066425344940700675365899595025949380260096154070261361979623438289509852009770155461160305016365506872106159458284927846148417186348365679379107311452372173537604329799759929081296259158042677882722219365476328364853641842622520804391413816872273590288603765231707905425161351375778160244924315761919412491940777220768554733214875408347789884197833189498776347099684011833369196029722752003229113836098211899129316734480329054543032870723480267042025304703874503433904994909970086014353902451517323139957376264607734380090511519300799278037498172200455612655244127025627209086770239740952766715768877480639366873068449044769499162598752373471003208063605392446779485242126227002209 Time: CPU 2.38 s, Wall: 2.50 s }}} {{{id=66| /// }}}

Calculus

Sage does Calculus:

{{{id=8| f(x) = sin(x)^2*cos(x)*exp(x) show(f) ///
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ e^{x} \sin\left(x\right)^{2} \cos\left(x\right)
}}} {{{id=9| plot(f, 0, 3) /// }}} {{{id=5| show(integrate(f, x)) ///
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ -\frac{3}{40} \, e^{x} \sin\left(3 \, x\right) + \frac{1}{8} \, e^{x} \sin\left(x\right) - \frac{1}{40} \, e^{x} \cos\left(3 \, x\right) + \frac{1}{8} \, e^{x} \cos\left(x\right)
}}} {{{id=68| /// }}} {{{id=65| /// }}}

Graph Theory

Sage can do graph theory:

{{{id=69| g = graphs.FlowerSnark(); g /// Flower Snark: Graph on 20 vertices }}} {{{id=24| graph_editor(g) ///
}}}

Sage contains many unique and deep algorithms:

{{{id=70| g.automorphism_group() /// Permutation Group with generators [(2,11)(3,12)(4,13)(5,8)(6,9)(7,10)(16,19)(17,18), (1,4,7,10,13)(2,5,8,11,14)(3,6,9,12,20)(15,16,17,18,19), (1,14)(2,4)(5,7)(8,10)(11,13)] }}} {{{id=38| /// }}}

Statistics

Sage includes R, scipy.stats, and GSL (=Gnu Scientific Library).

{{{id=41| t = stats.TimeSeries(10^6).randomize('normal') plot(t.sums(), frame=True, figsize=[8,3]) /// }}} {{{id=42| t.plot_histogram() /// }}} {{{id=43| mean(t) /// -0.00033107089746577014 }}} {{{id=44| std(t) /// 1.0004268203066153 }}} {{{id=45| show(std([1,pi,e])) ///
\newcommand{\Bold}[1]{\mathbf{#1}}\sqrt{\frac{1}{18} \, {\left(\pi - 2 \, e + 1\right)}^{2} + \frac{1}{18} \, {\left(\pi + e - 2\right)}^{2} + \frac{1}{18} \, {\left(2 \, \pi - e - 1\right)}^{2}}
}}}

Generalized Hidden Markov Models Library (a complete new implementation in Sage, by me) -- useful for math biology, computer learning, etc.:

{{{id=46| m = hmm.DiscreteHiddenMarkovModel([[0.4,0.5,0.1],[0.1,0.8,0.1],[.2,.3,.5]], [[0.1,0.9],[0.5,0.5],[1,0]], [.2,.5,.3], emission_symbols=['a','b']); m /// Discrete Hidden Markov Model with 3 States and 2 Emissions Transition matrix: [0.4 0.5 0.1] [0.1 0.8 0.1] [0.2 0.3 0.5] Emission matrix: [0.1 0.9] [0.5 0.5] [1.0 0.0] Initial probabilities: [0.2000, 0.5000, 0.3000] Emission symbols: ['a', 'b'] }}} {{{id=72| m.baum_welch(['a','b','b','a','b','b','a','b','b']) /// (-4.6196788513299064e-05, 20) }}} {{{id=75| m /// Discrete Hidden Markov Model with 3 States and 2 Emissions Transition matrix: [ 2.30981274908e-05 0.999976901873 1.89806678835e-23] [7.14454696757e-221 1.98424978494e-232 1.0] [ 1.0 0.0 0.0] Emission matrix: [6.75306548194e-69 1.0] [ 0.0 1.0] [ 1.0 0.0] Initial probabilities: [0.0000, 0.0000, 1.0000] Emission symbols: ['a', 'b'] }}} {{{id=76| m.sample(10) /// ['a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'b', 'a'] }}} {{{id=79| g = m.graph() /// }}} {{{id=78| show(plot(m.graph(),graph_border=1), figsize=3) /// }}} {{{id=77| A = [[0.5,0.5],[0.5,0.5]] B = [[(0.9,(0.0,1.0)), (0.1,(1,10000))],[(1,(1,1)), (0,(0,0.1))]] m = hmm.GaussianMixtureHiddenMarkovModel(A, B, [1,0]); m /// Gaussian Mixture Hidden Markov Model with 2 States Transition matrix: [0.5 0.5] [0.5 0.5] Emission parameters: [0.9*N(0.0,1.0) + 0.1*N(1.0,10000.0), 1.0*N(1.0,1.0) + 0.0*N(0.0,0.1)] Initial probabilities: [1.0000, 0.0000] }}} {{{id=74| plot(m.sample(1000).sums()) /// }}} {{{id=84| /// }}}

Fortran!

Example here: http://sagenb.org/home/pub/1708/

{{{id=73| /// }}} {{{id=40| /// }}}

Matrices

And numerical linear algebra:

{{{id=31| import pylab A_image = pylab.mean(pylab.imread(DATA + 'lonestar.png'), 2) @interact def svd_image(i = ("Eigenvalues (quality)",(20,(1..100))), display_axes = ("Display Axes", True)): u,s,v = pylab.linalg.svd(A_image) A = sum(s[j]*pylab.outer(u[0:,j], v[j,0:]) for j in range(i)) g = graphics_array([matrix_plot(A),matrix_plot(A_image)]) show(g, axes=display_axes, figsize=(8,3)) html('

Lonestar: compressed using %s eigenvalues

'%i) ///
Eigenvalues (quality) 
Display Axes 
}}} {{{id=30| /// }}}

FemHub -- a customized Sage

See http://femhub.org/

{{{id=82| /// }}} {{{id=81| /// }}}

3-Dimensional Plots

Sage can draw 3d plots:

{{{id=10| var('x,y') b = 2.2 (plot3d(sin(x^2-y^2),(x,-b,b),(y,-b,b), opacity=.9) + plot3d(0, (x,-b,b), (y,-b,b), color='red')) /// }}} {{{id=48| var('x,y,z') T = golden_ratio p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x)) r = 4.78 implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=50) /// }}} {{{id=47| /// }}}

Sage can plot Yoda:

{{{id=4| from scipy import io x = io.loadmat(DATA + 'yodapose.mat', struct_as_record=True) from sage.plot.plot3d.index_face_set import IndexFaceSet V = x['V']; F3 = x['F3']-1; F4 = x['F4']-1 Y = (IndexFaceSet(F3, V, color = Color('#00aa00')) + IndexFaceSet(F4, V, color = Color('#00aa00'))) Y = Y.rotateX(-1) Y.show(aspect_ratio = [1,1,1], frame = False, figsize = 4) /// }}} {{{id=3| /// }}}

And if you're really serious, Sage is Python, so you can also use:

{{{id=26| /// }}} {{{id=27| /// }}} {{{id=25| /// }}}

Key take-away points: 

 

Questions?  

Now, take a break, then start with the tutorials...

{{{id=29| /// }}}