sd22: Sage: Creating an open source alternative to Magma, Maple, Mathematica, and Matlab

699 days ago by wstein

Creating an Open Source Alternative to
 
Magma, Maple, Mathematica, and MATLAB

William Stein, Professor of Mathematics, University of Washington


 
       

Mission Statement

Create an 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

  • I started Sage at Harvard in January 2005.
  • Sage-1.0 released February 2006 at Sage Days 1 (UC San Diego).
  • Nearly 30 Sage Days Workshops (!)  at UCLA, UW, Cambridge, Bristol, Austin, France, San Diego, Seattle, MSRI, ..., Barcelona, ...
  • Sage won first prize in the Trophees du Libre (November 2007)
  • Funding from Microsoft, Univ of Washington, UC San Diego, NSF, DoD, Google, Sun, private donations, etc.
  • Hundreds of other people subsequently got involved in Sage's development, and the scope of the project has widened to cover all mathematical computation. There is interest from all areas of mathematics, physics, engineering, etc.  (Developer mailing list has 1184 subscribers and about 50 messages/day... and sage-support has 1718 subscribers.)
  • About 8,000 downloads per month. (Probably most users do not download sage themselves.)

 

What is Sage?

  • Sage = Python + Math
  • A unified self-contained distribution of open source mathematical software.
  • Nearly a half million lines of new Python (and Cython) code/documentation that implements new capabilities and algorithms.
  • Over 100,000 lines of automated tests.
  • A "cloud" application like GMail or Google Docs: http://sagenb.org  (over 33,000 accounts);  of course, Sage also runs on your desktop.

 

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...

 

Interfaces

2 + 2 
       
4
4

The following opens a single persistent running copy of Mathematica:

%mathematica 2 + 2 
       
         4
         4
%lisp (+ 2 2) 
       
4
4
%cython print 2 + 2 
%mathematica Integrate[ Sin[x^2], x ] 
       
              Pi                2
         Sqrt[--] FresnelS[Sqrt[--] x]
              2                 Pi
              Pi                2
         Sqrt[--] FresnelS[Sqrt[--] x]
              2                 Pi

Moving from Mathematica to Maple (via Sage):

mathematica.eval('f := x^2 + 1') 
       
mathematica('f') 
       
1 + x^2
1 + x^2
g = mathematica('f').sage(); g 
       
x^2 + 1
x^2 + 1
maple(g) 
       
x^2+1
x^2+1

You can also open a connection to a remote copy of Mathematica (or Maple, etc.):

mathematica2 = Mathematica(server='sage.math.washington.edu') 
       
No remote temporary directory (option server_tmpdir) specified, using
/tmp/ on sage.math.washington.edu
No remote temporary directory (option server_tmpdir) specified, using /tmp/ on sage.math.washington.edu
mathematica2('2+3') 
       
5
5

The above just happened in Seattle.  But otherwise it is nearly indistinguishable from working locally.

 
       

Very Big Numbers

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

time n = factorial(10^6) 
       
Time: CPU 0.67 s, Wall: 0.69 s
Time: CPU 0.67 s, Wall: 0.69 s
n.ndigits() 
       
5565709
5565709

Multiplying huge numbers is also fast:

time m = n * (n+1) 
       
Time: CPU 0.42 s, Wall: 0.75 s
Time: CPU 0.42 s, Wall: 0.75 s

and a matrix with a big determinant:

ZZ 
       
Integer Ring
Integer Ring
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)
200 x 200 dense matrix over Integer Ring (type 'print a.str()' to see all of the entries)
matrix_plot(a) 
       
a[0,0] 
       
292853541129496404771787723772635035936
292853541129496404771787723772635035936
time a.determinant() 
       
-42994296614978891754921284769725516937518657620099526927852615874536848\
940087137928084725705546416154023018351370442158519709278588380157854281\
409023551483111180202882638372123189368282651855781120243815793526746560\
086949102172910439095016410591909159865863340458326542184995105809304917\
779062263756092347902160900077350682833823222200167753164554711258949973\
020447612306154205041211411058096384181479211018221738905399923585574889\
111551755247631178495469210677146588941150989042729336971898695351124971\
064417251214886561831879767231688381190216425616890430024621329334693782\
608792277657750034381144547099334609722831987192508615344021487274064678\
943560947717672122018556528585856193291271263567914927188367756436958847\
849736034224340862186787344615800502744593385635657272504061957668938328\
417938601906251374253154366576867413369019314735461532350238615477394030\
597182729968077488117897885147668015674332111393550408573370504770089547\
668922759685284731381108786984250474903821185016461099482612258665535600\
585180986209855070727702809414107449058282447384572197451973371780650941\
855684166578715811381305732617500270849921968284478059093793857955237048\
355456520180215552264944409972084135214376339721815634864938731924780718\
492164240872088698552109948769761810613729865182903788155030913289543859\
573385768082051275331822574078656941462082356571035221965710737756509478\
270979127827109816771159825075828620003671552370974461508098880659827196\
969401359068421655499905088279521660507374841271580617338778328499786534\
700091583749454037302418167256090788124432520031575870201608719469243327\
416176868398960089771784046683384688990784138659152301504977660786780546\
592396141131161914043591797559937316361175357912752674157660277736014930\
097647525950295916960462372176625962871528324882745299394394423799100628\
079259651710156146128047071339320109002482004457253856890048533448725716\
652720876252804541073949520138522974489985443117474069057609064674008406\
560701937590383622566457224644003932911357385408131972301030141209134937\
286127604790012826301609335490260432081349314138322531525132796308703907\
813395758178538936085018113272569695113497439115909833108385353413165908\
205497314360753889842034806155146668183917238053017336719225348805189512\
161249849381001587023186094497600343278700981625806681676681506280654525\
052111760978126033244054705280697763243010153095934448316816382468035059\
112232309235057180883325116915823892038033293861620663988477718757470947\
093952741435090954135646826573473942487006936151960014791124946703871861\
354721424169781375227012898680278423658770963507993847570635117328911639\
732523258179667672941401568492801184543313513191377680648218323359644249\
587674972131534239000577357579340469030171097204443927136443622349123237\
386930070109550622691137683838136011428832630370506999863126547380088791\
241778137716728006749730037916253776656131881892162613513753735749112694\
343693516776299347295307309100975572437137611248853428963060215217961414\
328805680244406642085109631648711462276958676351374815379119560558200902\
412100043444128696727422355157860511985976250595371432729026726043157653\
541856617204489998010086869256178612243805817859639289176029245414419921\
238355964975179484311362399083481483609939536292663996445163652788186183\
739669554358016944830451299722369721158921081054610829112291982205159680\
865188285687088916513628487510277610199367328951566425185645346282647928\
601386335212015935417158446631040923540296020112972274633750203493333619\
179515901237320333291111372422515375542999567372685909154786350783809726\
594687990967528623517259393706862393977191926715105902927697840444174533\
980004279456824587789096127442182951310845308010557709652262846391705276\
570104584284436662603562185166807947993276980785569276115391982246011937\
372029410974080238567442659756783277221287767226186627868521668071347315\
824247126015421979559715664362933984095178858914064996951808007718366021\
522592852330609065200651806798622381332527144265442236677427602315664226\
158294501085062633200033348898970520264967589663141432137178262800186992\
187977024452720171054043168156844561859584304557490033953368260514092965\
929948161740112896226831394369930237107197242569522581744479800685001247\
716977677418078009588889489160529399571845058242808932117884829142393427\
723922671355088898717477998805458354899280672665382293084062342570653126\
386396055763190478187399428109923418521689264027232280468354739052143247\
802176738861340716218388576405895232424344927061656673627162283783314359\
468208426970041714613776390611578944696244136525620716228206745826435027\
003958060408423890032142019259111121353201254546092361500779345398166062\
014816687745919947234303209631566598798338465218800526281277539833275481\
468477773405011768529430461376408141736401465278495416039469037422597638\
203039552419235834525129501424173986136653868708786649451296708363115431\
182779418559842312410513402178964102759698235858732963261648699515910118\
732217507350544623365490412541854560741143023863370591231803949931077538\
495576826573492049332255653243307132970926418123604481675947949827986291\
663397766253715371141376424858756182486738645487295717354520867527014164\
995258385105580536160725509749348861369630802135598793703137759124228872\
782833381296620451776441977047060217640899870358340380169875786513435511\
619703606429447890353428370361240980999740513613716067472768553466355073\
820851113621457336785378284953509365105047915027848776441885428767281348\
374212565870272308518330991976148167293625891126258947115073357657028932\
605104462991892288741073381740777106155752849023802445205369892934247185\
013424005327057301469523778921661323471053427143817777202839178342841792\
897082053236177213277887350627677061561261602766010039909531925296824907\
195714952292831431409357785270690227848307396626674348154336483998877589\
180877016379068483259139040281439392981538553782822137591109246210897770\
902456201561796611340931049130719226071700004412172149370814176364612500\
105561226742727107062438218173966350383690319319272878828136757697629464\
025740792425080640391881207786931520062353206864156608115396418101631810\
780262097036006401974614828648659771409443590891428493910929853032133331\
830269336179620679535645917421609782493061628583983558242303368420784394\
870427848511596104770601366739149883508537395016472588211609554545040913\
981420239569968872165644865301764828400209500603236386293449797601024943\
959821192914219536903499901241027239002450505877599343928288847072659839\
521292156033541987855037306482673646033988876387764819366091674948775606\
194485609559858730080395758367742499812627931482238851438648716510085557\
571131508580385196031096378903194963009256492362516719054032002916671229\
525074499800365699025082885923059882945878835635245977561740301931440670\
683512296037101842166124772423687948635980005587924925584585959443112035\
787758062095577149240526490867802768052079449968365646159965208315290389\
202860650294205807137956573696366626134989646972623741115115504265564911\
273601795925164816519310672846372223815523373819073089076810489903035032\
646748902083853094557796731251624210172191285729067693983686415448557315\
285162625549650361278179812711094511421553501676524219067658785851355616\
465492182863067727973394386960704810679527371194737171178323828072812005\
420236888603062909704825477526126671494611810094566671726367013088788620\
350171563634168712425327398538745756521179196026466997597449831396924009\
823295048153783808591919093535922161059506208228150991598943184394142181\
334048535055851272655144728329923884519171957327660524620519118164550470\
885578892026721690916854143043903939306129865750289397391434829080713930\
079223482945194528858006589707708327292334178356133742183869793674126912\
417812353778430697457361170669953534166323819596226893871246658107402238\
693684359360362009184100669015315875929510994520953192515777674593986021\
22153404750
Time: CPU 2.44 s, Wall: 2.72 s
-4299429661497889175492128476972551693751865762009952692785261587453684894008713792808472570554641615402301835137044215851970927858838015785428140902355148311118020288263837212318936828265185578112024381579352674656008694910217291043909501641059190915986586334045832654218499510580930491777906226375609234790216090007735068283382322220016775316455471125894997302044761230615420504121141105809638418147921101822173890539992358557488911155175524763117849546921067714658894115098904272933697189869535112497106441725121488656183187976723168838119021642561689043002462132933469378260879227765775003438114454709933460972283198719250861534402148727406467894356094771767212201855652858585619329127126356791492718836775643695884784973603422434086218678734461580050274459338563565727250406195766893832841793860190625137425315436657686741336901931473546153235023861547739403059718272996807748811789788514766801567433211139355040857337050477008954766892275968528473138110878698425047490382118501646109948261225866553560058518098620985507072770280941410744905828244738457219745197337178065094185568416657871581138130573261750027084992196828447805909379385795523704835545652018021555226494440997208413521437633972181563486493873192478071849216424087208869855210994876976181061372986518290378815503091328954385957338576808205127533182257407865694146208235657103522196571073775650947827097912782710981677115982507582862000367155237097446150809888065982719696940135906842165549990508827952166050737484127158061733877832849978653470009158374945403730241816725609078812443252003157587020160871946924332741617686839896008977178404668338468899078413865915230150497766078678054659239614113116191404359179755993731636117535791275267415766027773601493009764752595029591696046237217662596287152832488274529939439442379910062807925965171015614612804707133932010900248200445725385689004853344872571665272087625280454107394952013852297448998544311747406905760906467400840656070193759038362256645722464400393291135738540813197230103014120913493728612760479001282630160933549026043208134931413832253152513279630870390781339575817853893608501811327256969511349743911590983310838535341316590820549731436075388984203480615514666818391723805301733671922534880518951216124984938100158702318609449760034327870098162580668167668150628065452505211176097812603324405470528069776324301015309593444831681638246803505911223230923505718088332511691582389203803329386162066398847771875747094709395274143509095413564682657347394248700693615196001479112494670387186135472142416978137522701289868027842365877096350799384757063511732891163973252325817966767294140156849280118454331351319137768064821832335964424958767497213153423900057735757934046903017109720444392713644362234912323738693007010955062269113768383813601142883263037050699986312654738008879124177813771672800674973003791625377665613188189216261351375373574911269434369351677629934729530730910097557243713761124885342896306021521796141432880568024440664208510963164871146227695867635137481537911956055820090241210004344412869672742235515786051198597625059537143272902672604315765354185661720448999801008686925617861224380581785963928917602924541441992123835596497517948431136239908348148360993953629266399644516365278818618373966955435801694483045129972236972115892108105461082911229198220515968086518828568708891651362848751027761019936732895156642518564534628264792860138633521201593541715844663104092354029602011297227463375020349333361917951590123732033329111137242251537554299956737268590915478635078380972659468799096752862351725939370686239397719192671510590292769784044417453398000427945682458778909612744218295131084530801055770965226284639170527657010458428443666260356218516680794799327698078556927611539198224601193737202941097408023856744265975678327722128776722618662786852166807134731582424712601542197955971566436293398409517885891406499695180800771836602152259285233060906520065180679862238133252714426544223667742760231566422615829450108506263320003334889897052026496758966314143213717826280018699218797702445272017105404316815684456185958430455749003395336826051409296592994816174011289622683139436993023710719724256952258174447980068500124771697767741807800958888948916052939957184505824280893211788482914239342772392267135508889871747799880545835489928067266538229308406234257065312638639605576319047818739942810992341852168926402723228046835473905214324780217673886134071621838857640589523242434492706165667362716228378331435946820842697004171461377639061157894469624413652562071622820674582643502700395806040842389003214201925911112135320125454609236150077934539816606201481668774591994723430320963156659879833846521880052628127753983327548146847777340501176852943046137640814173640146527849541603946903742259763820303955241923583452512950142417398613665386870878664945129670836311543118277941855984231241051340217896410275969823585873296326164869951591011873221750735054462336549041254185456074114302386337059123180394993107753849557682657349204933225565324330713297092641812360448167594794982798629166339776625371537114137642485875618248673864548729571735452086752701416499525838510558053616072550974934886136963080213559879370313775912422887278283338129662045177644197704706021764089987035834038016987578651343551161970360642944789035342837036124098099974051361371606747276855346635507382085111362145733678537828495350936510504791502784877644188542876728134837421256587027230851833099197614816729362589112625894711507335765702893260510446299189228874107338174077710615575284902380244520536989293424718501342400532705730146952377892166132347105342714381777720283917834284179289708205323617721327788735062767706156126160276601003990953192529682490719571495229283143140935778527069022784830739662667434815433648399887758918087701637906848325913904028143939298153855378282213759110924621089777090245620156179661134093104913071922607170000441217214937081417636461250010556122674272710706243821817396635038369031931927287882813675769762946402574079242508064039188120778693152006235320686415660811539641810163181078026209703600640197461482864865977140944359089142849391092985303213333183026933617962067953564591742160978249306162858398355824230336842078439487042784851159610477060136673914988350853739501647258821160955454504091398142023956996887216564486530176482840020950060323638629344979760102494395982119291421953690349990124102723900245050587759934392828884707265983952129215603354198785503730648267364603398887638776481936609167494877560619448560955985873008039575836774249981262793148223885143864871651008555757113150858038519603109637890319496300925649236251671905403200291667122952507449980036569902508288592305988294587883563524597756174030193144067068351229603710184216612477242368794863598000558792492558458595944311203578775806209557714924052649086780276805207944996836564615996520831529038920286065029420580713795657369636662613498964697262374111511550426556491127360179592516481651931067284637222381552337381907308907681048990303503264674890208385309455779673125162421017219128572906769398368641544855731528516262554965036127817981271109451142155350167652421906765878585135561646549218286306772797339438696070481067952737119473717117832382807281200542023688860306290970482547752612667149461181009456667172636701308878862035017156363416871242532739853874575652117919602646699759744983139692400982329504815378380859191909353592216105950620822815099159894318439414218133404853505585127265514472832992388451917195732766052462051911816455047088557889202672169091685414304390393930612986575028939739143482908071393007922348294519452885800658970770832729233417835613374218386979367412691241781235377843069745736117066995353416632381959622689387124665810740223869368435936036200918410066901531587592951099452095319251577767459398602122153404750
Time: CPU 2.44 s, Wall: 2.72 s
 
       

Symbolic Calculus

Sage does Calculus:

f = sin(x)^2*cos(x)*exp(x) show(f) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}e^{x} \sin\left(x\right)^{2} \cos\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}e^{x} \sin\left(x\right)^{2} \cos\left(x\right)
plot(f, 0, 3) 
       
show(f.taylor(x, 0, 5)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{2}{3} \, x^{5} - \frac{1}{3} \, x^{4} + x^{3} + x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{2}{3} \, x^{5} - \frac{1}{3} \, x^{4} + x^{3} + x^{2}
show(integrate(f, x)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-\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)
\newcommand{\Bold}[1]{\mathbf{#1}}-\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)
 
       
 
       

Graph Theory

Sage can do graph theory:

g = graphs.FlowerSnark(); g 
       
Flower Snark: Graph on 20 vertices
Flower Snark: Graph on 20 vertices
graph_editor(g) 
       
g.plot(graph_border=True) 
       

Sage contains many unique and deep algorithms:

G = g.automorphism_group(); G 
       
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)]
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)]
G.order() 
       
20
20
 
       

Statistics

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

reset('r') 
       
%r D <- data.frame(x=c(1,2,3,1), y=c(7,19,2,2)) # Sort on x indexes <- order(D$x) D[indexes,] 
       
  x  y
1 1  7
4 1  2
2 2 19
3 3  2
  x  y
1 1  7
4 1  2
2 2 19
3 3  2
%r # Print out sorted dataset, sorted in reverse by y D[rev(order(D$y)),] 
       
  x  y
2 2 19
1 1  7
4 1  2
3 3  2
  x  y
2 2 19
1 1  7
4 1  2
3 3  2

There is also a C library interface to R:

import rpy2.robjects str(rpy2.robjects.r('mean( c(1,2,5,19) )')) 
       
'[1] 6.75'
'[1] 6.75'
 
       

Scipy is a large Python library included in Sage with statistics functions:

import scipy.stats 
       
scipy.stats.norm? 
       

GSL is a C library in Sage, which also has stats functions.  It can only be used from Cython, though.

Sage also has its own native statistics functionality:

t = stats.TimeSeries(10^6).randomize('normal') plot(t.sums(), frame=True, figsize=[8,3]) 
       
t.plot_histogram() 
       
mean(t) 
       
-0.00019249918463875795
-0.00019249918463875795
std(t) 
       
0.99978723260878721
0.99978723260878721
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}}
\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}}
import numpy 
       
numpy.std([1,pi,e]) 
       
sqrt(0.33333333333333331*(-0.33333333333333331*pi -
0.33333333333333331*e + 0.66666666666666674)^2 +
0.33333333333333331*(-0.33333333333333331*pi + 0.66666666666666674*e -
0.33333333333333331)^2 + 0.33333333333333331*(0.66666666666666674*pi -
0.33333333333333331*e - 0.33333333333333331)^2)
sqrt(0.33333333333333331*(-0.33333333333333331*pi - 0.33333333333333331*e + 0.66666666666666674)^2 + 0.33333333333333331*(-0.33333333333333331*pi + 0.66666666666666674*e - 0.33333333333333331)^2 + 0.33333333333333331*(0.66666666666666674*pi - 0.33333333333333331*e - 0.33333333333333331)^2)

Sage has a new Generalized Hidden Markov Models Library.

This is a complete new implementation in Sage -- useful for math biology, computer learning, etc.:

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']
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']
m.baum_welch(['a','b','b','a','b','b','a','b','b']) 
       
(-4.6196788513299064e-05, 20)
(-4.6196788513299064e-05, 20)
       
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']
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']
m.sample(10) 
       
['a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'b', 'a']
['a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'b', 'a']
g = m.graph() 
       
show(plot(m.graph(),graph_border=1), figsize=3) 
       
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]
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]
plot(m.sample(1000).sums()) 
       
 
       

Fortran!

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

 
       
 
       

Matrices

 

 

Exact linear algebra: great support for very sophisticated algorithms over \mathbf{Z}, finite fields, \mathbf{F}_2, etc. 

a = random_matrix(GF(2),1000,2000) a.visualize_structure() 
       
time b = a.echelon_form() b.visualize_structure() 
       
Time: CPU 0.01 s, Wall: 0.01 s
Time: CPU 0.01 s, Wall: 0.01 s
 
       

And numerical linear algebra:

import pylab MSRI = pylab.imread(DATA + 'msri.png') A_image = numpy.mean(MSRI, 2) u,s,v = numpy.linalg.svd(A_image) S = numpy.zeros( A_image.shape ) S[:len(s),:len(s)] = numpy.diag(s) @interact def svd_image(i = ("Eigenvalues (quality)",(20,(1..A_image.shape[0])))): # Thus the following expression involves only the first i eigenvalues, # and the first i rows of v and the first i columns of u. # Thus we use i*m + i*n + i = i*(m+n+1) storage, compared to the # original image that uses m*n storage. A_approx = numpy.dot(numpy.dot(u[:,:i], S[:i,:i]), v[:i,:]) # Now plot both matrices using Sage's matrix_plot command. g = graphics_array([matrix_plot(A_approx), matrix_plot(A_image)]) # And show the plot, with no axes: show(g, axes=False, figsize=(8,3)) # And add a caption html("<h2>MSRI: compressed using %s eigenvalues</h2>"%i) html("Compressed to %.1f%% of original size."%(100*(2.0*i*n+i)/(n*n))) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       

Very Deep, Cutting Edge Mathematics

  • Number theory
  • Combinatorics
  • Algebraic topology

Number theory - Verify that the 5-part of the Shafarevich-Tate group of a rank 2 curve is trivial:

S = EllipticCurve('389a').sha(); S 
       
Shafarevich-Tate group for the Elliptic Curve defined by y^2 + y = x^3 +
x^2 - 2*x over Rational Field
Shafarevich-Tate group for the Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field
S.p_primary_bound(5) 
       
0
0

Algebraic topology -- Examples of computing homology groups:

S = simplicial_complexes.Sphere(2) # the 2-sphere S 
       
Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3),
(0, 1, 2), (1, 2, 3), (0, 1, 3)}
Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3), (0, 1, 3)}
S.homology() 
       
{0: 0, 1: 0, 2: Z}
{0: 0, 1: 0, 2: Z}
X = simplicial_complexes.SurfaceOfGenus(3); X 
       
Simplicial complex with 15 vertices and 38 facets
Simplicial complex with 15 vertices and 38 facets
X.homology() 
       
{0: 0, 1: Z^6, 2: Z}
{0: 0, 1: Z^6, 2: Z}
X.graph().plot() 
       
 
       

FemHub -- a customized Sage

See http://femhub.org/

 
       
 
       

3-Dimensional Plots

Sage can draw 3d plots:

var('x,y') b = 2.2 show((plot3d(sin(x^2-y^2),(x,-b,b),(y,-b,b), opacity=.7) + plot3d(0, (x,-b,b), (y,-b,b), color='red'))) 
       
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) 
       
 
       

Sage can plot Yoda:

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) 
       
 
       

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

 
       
 
       
 
       

Key take-away points: 

  • FOSS: 100% free open source GPL software: good for clusters, sharing, research, collaboration
  • Python: mainstream, scientific-computing friendly programming language
  • Cython: optimized Python to C/C++ compiler we develop with support for C/C++ datatypes
  • Interfaces: to control Matlab, Octave, Mathematica, etc., from Sage
  • Self contained: can have many copies of Sage at once, so no "dependency hell"
  • Peer reviewed: get your code refereed
  • Web based: notebook interface

 

Questions?