CECM 2007 system:sage {{{id=0| 3 + 5 /// 8 }}} {{{id=1| # Algebra var('a,b,c,x') S = solve(a*x^2 + b*x + c == 0, x) show(S[0]) ///
x = \frac{-\sqrt{ {b}^{2} - {{4 \cdot a} \cdot c} } - b}{{2 \cdot a}}
}}} {{{id=86| }}} {{{id=53| # Calculus -- plotting functions reset('e,x') show(plot(e^x - x^2, -1, 3) + text('$e^x - x^2$', (1,3), rgbcolor=(0,0,0), fontsize=25)) }}} {{{id=104| }}} {{{id=2| # Calculus -- symbolic integration # (Currently Maxima -- the first major CAS -- is used behind the scenes for # symbolic calculus, but this may change.) var('x,y') f = sin(x)^4 + cos(y) show(integrate(f, x)) ///
{x \cdot \cos \left( y \right)} + \frac{\frac{\frac{\sin \left( {4 \cdot x} \right)}{2} + {2 \cdot x}}{8} - \frac{\sin \left( {2 \cdot x} \right)}{2} + \frac{x}{2}}{2}
}}} {{{id=105| show(animate([plot(sin(x^n), 0, 2) for n in range(1,10)])) }}} {{{id=3| # Calculus -- Taylor series show(taylor(f, x, 0, 15)) ///
\cos \left( y \right) + {x}^{4} - \frac{{2 \cdot {x}^{6} }}{3} + \frac{{x}^{8} }{5} - \frac{{34 \cdot {x}^{10} }}{945} + \frac{{62 \cdot {x}^{12} }}{14175} - \frac{{4 \cdot {x}^{14} }}{10395}
}}} {{{id=12| show(integrate(cos(x^2), x)) ///
\frac{{-\sqrt{ \pi } \cdot \left( {\left( {\sqrt{ 2 } \cdot i} - \sqrt{ 2 } \right) \cdot \left( \text{erf} \left( \frac{{\left( {\sqrt{ 2 } \cdot i} + \sqrt{ 2 } \right) \cdot x}}{2} \right) \right)} + {\left( {\sqrt{ 2 } \cdot i} + \sqrt{ 2 } \right) \cdot \left( \text{erf} \left( \frac{{\left( {\sqrt{ 2 } \cdot i} - \sqrt{ 2 } \right) \cdot x}}{2} \right) \right)} \right)}}{8}
}}} {{{id=99| }}} {{{id=4| # Combinatorics -- combinatorial functions print number_of_partitions(10) /// 42 }}} {{{id=7| list(partitions(10)) /// [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 2, 2, 2), (1, 1, 2, 2, 2, 2), (2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 2, 3), (1, 1, 1, 2, 2, 3), (1, 2, 2, 2, 3), (1, 1, 1, 1, 3, 3), (1, 1, 2, 3, 3), (2, 2, 3, 3), (1, 3, 3, 3), (1, 1, 1, 1, 1, 1, 4), (1, 1, 1, 1, 2, 4), (1, 1, 2, 2, 4), (2, 2, 2, 4), (1, 1, 1, 3, 4), (1, 2, 3, 4), (3, 3, 4), (1, 1, 4, 4), (2, 4, 4), (1, 1, 1, 1, 1, 5), (1, 1, 1, 2, 5), (1, 2, 2, 5), (1, 1, 3, 5), (2, 3, 5), (1, 4, 5), (5, 5), (1, 1, 1, 1, 6), (1, 1, 2, 6), (2, 2, 6), (1, 3, 6), (4, 6), (1, 1, 1, 7), (1, 2, 7), (3, 7), (1, 1, 8), (2, 8), (1, 9), (10,)] }}} {{{id=5| # number_of_partitions is extremely fast, maybe or soon # to be the fastest in the world (thanks to Jon Bober) time n = number_of_partitions(10^8) /// Time: CPU 5.37 s, Wall: 5.39 s }}} {{{id=6| len(str(n)) /// 11132 }}} {{{id=87| >>> Compare with Maple -- this was pasted from the console d142-058-050-016:~ was$ maple |\^/| Maple 10 (APPLE PPC OSX) ._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2005 \ MAPLE / All rights reserved. Maple is a trademark of <____ ____> Waterloo Maple Inc. | Type ? for help. > with('combinat'); Warning, the protected name Chi has been redefined and unprotected [Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, fibonacci, firstpart, graycode, inttovec, lastpart, multinomial, nextpart, numbcomb, numbcomp, numbpart, numbperm, partition, permute, powerset, prevpart, randcomb, randpart, randperm, setpartition, stirling1, stirling2, subsets, vectoint] > numbpart(10^5); bytes used=4002496, alloc=2620960, time=0.71 bytes used=8025168, alloc=3079628, time=1.08 bytes used=12026356, alloc=3276200, time=1.40 ... bytes used=420468464, alloc=4586680, time=26.51 bytes used=424485468, alloc=4586680, time=26.83 bytes used=428485856, alloc=4783252, time=27.02 27493510569775696512677516320986352688173429315980054758203125984302147328114964173055050741660736\ 6215901578447742962489404930630702004617927644930335101160793424571901557189435097253124661084\ 5200636955893446424871682878983218234500926285383140459702130713067451062441922731123899970228\ 4408609370935531629697851569569892196108480158600569421098519 }}} {{{id=90| time number_of_partitions(10^5) /// 27493510569775696512677516320986352688173429315980054758203125984302147328114964173055050741660736621590157844774296248940493063070200461792764493033510116079342457190155718943509725312466108452006369558934464248716828789832182345009262853831404597021307130674510624419227311238999702284408609370935531629697851569569892196108480158600569421098519 CPU time: 0.01 s, Wall time: 0.01 s }}} {{{id=9| # Combinatorics -- tons of other functions bell_number(200) /// 624748477619370179475169176526172040868676631541156088557319107233592458655068693596197197036539240046478541880130367251305801992731793116433134463929399319082714196961652168013602803041772182448739785880587721883539643696750170168607678730423675712080858884633763266376601388 }}} {{{id=102| >>> For comparison, in Maple: d142-058-050-016:~ was$ maple |\^/| Maple 10 (APPLE PPC OSX) ._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2005 \ MAPLE / All rights reserved. Maple is a trademark of <____ ____> Waterloo Maple Inc. | Type ? for help. > with('combinat'); > bell(100) > ; 47585391276764833658790768841387207826363669686825611466616334637559114497892442622672724044217756\ 306953557882560751 > bell(200); bytes used=4000104, alloc=3538296, time=0.54 bytes used=8000488, alloc=4652204, time=0.79 ... bytes used=5048393772, alloc=5504016, time=309.63 bytes used=5052394312, alloc=5504016, time=309.87 bytes used=5056394588, alloc=5504016, time=310.11 ... Got board waiting... }}} {{{id=10| # Commutative Algebra R. = PolynomialRing(GF(17)) R /// Polynomial Ring in x, y, z over Finite Field of size 17 }}} {{{id=13| # SAGE does arithmetic in R faster than anything else in the world # (the below really expands out) (x+y+z)^5 /// x^5 + 5*x^4*y - 7*x^3*y^2 - 7*x^2*y^3 + 5*x*y^4 + y^5 + 5*x^4*z + 3*x^3*y*z - 4*x^2*y^2*z + 3*x*y^3*z + 5*y^4*z - 7*x^3*z^2 - 4*x^2*y*z^2 - 4*x*y^2*z^2 - 7*y^3*z^2 - 7*x^2*z^3 + 3*x*y*z^3 - 7*y^2*z^3 + 5*x*z^4 + 5*y*z^4 + z^5 }}} {{{id=14| time f = (x+y+z)^1000 # this is expanded /// CPU time: 0.79 s, Wall time: 0.79 s }}} {{{id=119| factor(2^997-1) }}} {{{id=15| len(str(f)) /// 873156 }}} {{{id=16| P. = PolynomialRing(QQ, 5, order='lex') I = sage.rings.ideal.Katsura(P,5) print I /// Ideal (a + 2*b + 2*c + 2*d + 2*e - 1, a^2 - a + 2*b^2 + 2*c^2 + 2*d^2 + 2*e^2, 2*a*b + 2*b*c - b + 2*c*d + 2*d*e, 2*a*c + b^2 + 2*b*d + 2*c*e - c, 2*a*d + 2*b*c + 2*b*e - d) of Polynomial Ring in a, b, c, d, e over Rational Field }}} {{{id=18| time G = I.groebner_basis() /// CPU time: 0.01 s, Wall time: 0.03 s }}} {{{id=21| G /// [140091030503424*e^16 - 160104034861056*e^15 + 63230675189760*e^14 - 5440199491584*e^13 - 3420375588864*e^12 + 1176521826304*e^11 - 100107248640*e^10 - 23813057024*e^9 + 6674207680*e^8 - 318548480*e^7 - 95473904*e^6 + 13393680*e^5 + 334100*e^4 - 123848*e^3 + 847*e^2 + 327*e, 13802496983130487357702587956647281359620407880702140792719922*d + 268373149239912452353362647526113901359682066793730885282652770463817531392*e^15 - 341471761730933999023368825137487819993908614302387558587348720175995486208*e^14 + 165438939242307968807287760195717628364682091188875297967961837009863049216*e^13 - 31937322851209919094336222655894395159091954278706956687086308825187696640*e^12 - 2381938825439575641103531905031208461382807757165830768974083698415071232*e^11 + 2558754067127672667836992699753619660138425297322862919862494496097581056*e^10 - 524490136812097176516480915776545108466290970329944420517686416031116800*e^9 + 22833534930307586413803015793309131967567389519015945426577122181447680*e^8 + 9764391640480870738422385620505001945885555728868610772116932532282848*e^7 - 1878689121405308982059967477410961921706359917443878704636063129457104*e^6 + 62459580587449203938583803424472018189522127883259499803799747411184*e^5 + 17386794467762482414336873939786971749271586291917691578986726475808*e^4 - 1622662884053845805505492259056874405932595633798049160769133762622*e^3 - 24567099537646635435758840459401373579537769989951032921451990763*e^2 + 4800949815956232562418113422634867873188757036285513662096489095*e, 56603004690980496*c + 377995660126329608352*d^4*e + 155076768526221673824*d^4 + 1212223082382249857568*d^3*e^2 + 95938817796660336276*d^3*e - 32831235606435030170*d^3 + 2688694476152333270688*d^2*e^3 + 235388754293651426616*d^2*e^2 - 146157287923289473488*d^2*e - 12275811638152181098*d^2 + 3844585513076323998672*d*e^4 - 644260976862549620592*d*e^3 - 222070688429080909912*d*e^2 + 9276405433858345164*d*e + 2491786697637327349*d + 2461144309604340433920*e^5 - 900569252637848548800*e^4 - 98242681471658478240*e^3 + 31891534396231119168*e^2 + 3255261146369028384*e, 220*b + 6048*c^2*d + 12582*c^2*e - 1431*c^2 + 11124*c*d^2 + 37800*c*d*e - 5742*c*d + 30276*c*e^2 - 9322*c*e + 664*c + 5832*d^3 + 31986*d^2*e - 4697*d^2 + 50976*d*e^2 - 15600*d*e + 1116*d + 28782*e^3 - 13269*e^2 + 1225*e, a + 2*b + 2*c + 2*d + 2*e - 1] }}} {{{id=106| P. = PolynomialRing(QQ, 6, order='lex') I = sage.rings.ideal.Cyclic(P) print I /// Ideal (a + b + c + d + e + f, a*b + a*f + b*c + c*d + d*e + e*f, a*b*c + a*b*f + a*e*f + b*c*d + c*d*e + d*e*f, a*b*c*d + a*b*c*f + a*b*e*f + a*d*e*f + b*c*d*e + c*d*e*f, a*b*c*d*e + a*b*c*d*f + a*b*c*e*f + a*b*d*e*f + a*c*d*e*f + b*c*d*e*f, a*b*c*d*e*f - 1) of Polynomial Ring in a, b, c, d, e, f over Rational Field }}} {{{id=107| time G = I.groebner_basis() /// CPU time: 0.20 s, Wall time: 1.06 s }}} {{{id=108| G[0] /// f^48 - 2554*f^42 - 399710*f^36 - 499722*f^30 + 499722*f^18 + 399710*f^12 + 2554*f^6 - 1 }}} {{{id=109| len(G) /// 17 }}} {{{id=22| # Cryptography -- # David Kohel implemented a whole introductory course in classical # crypto in SAGE. S = AlphabeticStrings() E = VigenereCryptosystem(S,11) K = S("SHAKESPEARE") e = E(K) m = S("THECATINTHEHAT") e(m) /// LOEMELXRTYIZHT }}} {{{id=23| # Cryptography # Fast point counting on elliptic curves modulo p E = EllipticCurve([1,2,3,4,5]) time E.sea(next_prime(10^50)) /// 99999999999999999999999982865176588640916234222362 Time: CPU 0.05 s, Wall: 3.89 s }}} {{{id=24| # Cryptography -- mod 2 linear algebra # (thanks to M. Albrecht and G Bard). # SAGE has probably the world's fastest dense mod-2 linear # algebra up to around 10000x10000. A = random_matrix(GF(2), 4000) time B = A*A /// Time: CPU 0.55 s, Wall: 0.60 s }}} {{{id=26| }}} {{{id=25| # Group theory G = AlternatingGroup(4); G /// Alternating group of order 4!/2 as a permutation group }}} {{{id=27| G.is_solvable() /// True }}} {{{id=28| show(G.multiplication_table()) ///
\left(\begin{array}{rrrrrrrrrrrr} x_{0}&x_{1}&x_{2}&x_{3}&x_{4}&x_{5}&x_{6}&x_{7}&x_{8}&x_{9}&x_{10}&x_{11}\\ x_{1}&x_{2}&x_{0}&x_{5}&x_{3}&x_{4}&x_{7}&x_{8}&x_{6}&x_{11}&x_{9}&x_{10}\\ x_{2}&x_{0}&x_{1}&x_{4}&x_{5}&x_{3}&x_{8}&x_{6}&x_{7}&x_{10}&x_{11}&x_{9}\\ x_{3}&x_{6}&x_{9}&x_{0}&x_{7}&x_{10}&x_{1}&x_{4}&x_{11}&x_{2}&x_{5}&x_{8}\\ x_{4}&x_{8}&x_{10}&x_{2}&x_{6}&x_{11}&x_{0}&x_{5}&x_{9}&x_{1}&x_{3}&x_{7}\\ x_{5}&x_{7}&x_{11}&x_{1}&x_{8}&x_{9}&x_{2}&x_{3}&x_{10}&x_{0}&x_{4}&x_{6}\\ x_{6}&x_{9}&x_{3}&x_{10}&x_{0}&x_{7}&x_{4}&x_{11}&x_{1}&x_{8}&x_{2}&x_{5}\\ x_{7}&x_{11}&x_{5}&x_{9}&x_{1}&x_{8}&x_{3}&x_{10}&x_{2}&x_{6}&x_{0}&x_{4}\\ x_{8}&x_{10}&x_{4}&x_{11}&x_{2}&x_{6}&x_{5}&x_{9}&x_{0}&x_{7}&x_{1}&x_{3}\\ x_{9}&x_{3}&x_{6}&x_{7}&x_{10}&x_{0}&x_{11}&x_{1}&x_{4}&x_{5}&x_{8}&x_{2}\\ x_{10}&x_{4}&x_{8}&x_{6}&x_{11}&x_{2}&x_{9}&x_{0}&x_{5}&x_{3}&x_{7}&x_{1}\\ x_{11}&x_{5}&x_{7}&x_{8}&x_{9}&x_{1}&x_{10}&x_{2}&x_{3}&x_{4}&x_{6}&x_{0} \end{array}\right)
}}} {{{id=29| G.conjugacy_classes_subgroups() /// [Group(()), Group([ (1,2)(3,4) ]), Group([ (2,4,3) ]), Group([ (1,3)(2,4), (1,2)(3,4) ]), Group([ (1,3)(2,4), (1,2)(3,4), (2,4,3) ])] }}} {{{id=30| G.cayley_graph().show3d() }}} {{{id=31| }}} {{{id=32| # Graph theory G = graphs.DodecahedralGraph() }}} {{{id=33| show(G) }}} {{{id=95| G /// Dodecahedron: Graph on 20 vertices }}} {{{id=120| G.plot() /// Graphics object consisting of 1 graphics primitive }}} {{{id=121| G.plot3d().show() }}} {{{id=100| # Graph theory -- SAGE computes automorphism groups of graphs # This is the first ever open source implementation of graph automorphism computation, # thanks to Robert Miller (grad student at Washington). time G.automorphism_group() /// Permutation Group with generators [(1,19)(2,3)(4,6)(7,17)(8,18)(9,11)(12,13)(14,16), (2,8)(3,9)(4,13)(5,14)(6,7)(10,19)(11,18)(12,17), (1,8,7,14,15,16,17,18,19,20)(2,9,6,13,5,12,4,11,3,10), (2,20)(3,19)(4,18)(5,11)(6,10)(7,9)(12,15)(13,14)] Time: CPU 0.28 s, Wall: 0.29 s }}} {{{id=101| G.is_isomorphic(G) /// True }}} {{{id=35| # Graph theory -- SAGE includes a large database of graphs # (thanks to Emily Kirkman, a Washington undergrad) time n=graphs_query.display_all(num_vertices=5,diameter=3) ///

D@s
Vertices: 5 Regular: False Aut Group Size: 2
Edges: 4 Min Degree: 1 Orbits: 4
Cycles: 0 Max Degree: 3 Fixed Points: 3
Hamiltonian Cycles: 0 Average Degree: 1.6 Vertex Transitive: False
Eulerian: False Degree SD: 0.8 Edge Transitive: False
Planar: True Degree Sequence: 32111 Vertex Connectivity: 1
Perfect: True Min Eigenvalue: -1.84776 Edge Connectivity: 1
Lovasz Number: 3.0 Girth: 0 Max Eigenvalue: 1.84776 Cut Vertices: 2
Independence Number: 3 Radius: 2 Eigenvalues SD: 1.26491 Min Vertex Cover Size: 2
Clique Number: 2 Diameter: 3 Energy: 5.22625 Spanning Trees: 1
Components: 1 Complement: DJk Spectrum: 1.84776, 0.765367, 0, -0.765367, -1.84776
Induced Subgraphs: D@s, CL, CF, CB, C@, BW, BG, B?, A_, A?, @

DIk
Vertices: 5 Regular: False Aut Group Size: 2
Edges: 5 Min Degree: 1 Orbits: 4
Cycles: 1 Max Degree: 3 Fixed Points: 3
Hamiltonian Cycles: 0 Average Degree: 2.0 Vertex Transitive: False
Eulerian: False Degree SD: 0.632456 Edge Transitive: False
Planar: True Degree Sequence: 32221 Vertex Connectivity: 1
Perfect: True Min Eigenvalue: -2.13578 Edge Connectivity: 1
Lovasz Number: 3.0 Girth: 4 Max Eigenvalue: 2.13578 Cut Vertices: 1
Independence Number: 3 Radius: 2 Eigenvalues SD: 1.41421 Min Vertex Cover Size: 2
Clique Number: 2 Diameter: 3 Energy: 5.59587 Spanning Trees: 4
Components: 1 Complement: DK[ Spectrum: 2.13578, 0.662153, 0, -0.662153, -2.13578
Induced Subgraphs: DIk, C], CL, CF, CB, BW, BG, B?, A_, A?, @

DBk
Vertices: 5 Regular: False Aut Group Size: 2
Edges: 5 Min Degree: 1 Orbits: 3
Cycles: 1 Max Degree: 3 Fixed Points: 1
Hamiltonian Cycles: 0 Average Degree: 2.0 Vertex Transitive: False
Eulerian: False Degree SD: 0.894427 Edge Transitive: False
Planar: True Degree Sequence: 33211 Vertex Connectivity: 1
Perfect: True Min Eigenvalue: -1.61803 Edge Connectivity: 1
Lovasz Number: 3.0 Girth: 3 Max Eigenvalue: 2.30278 Cut Vertices: 2
Independence Number: 3 Radius: 2 Eigenvalues SD: 1.41421 Min Vertex Cover Size: 2
Clique Number: 3 Diameter: 3 Energy: 5.84162 Spanning Trees: 3
Components: 1 Complement: DBk Spectrum: 2.30278, 0.618034, 0, -1.30278, -1.61803
Induced Subgraphs: DBk, CL, CN, CB, Bw, BW, BG, B?, A_, A?, @

DK[
Vertices: 5 Regular: False Aut Group Size: 2
Edges: 5 Min Degree: 1 Orbits: 4
Cycles: 1 Max Degree: 3 Fixed Points: 3
Hamiltonian Cycles: 0 Average Degree: 2.0 Vertex Transitive: False
Eulerian: False Degree SD: 0.632456 Edge Transitive: False
Planar: True Degree Sequence: 32221 Vertex Connectivity: 1
Perfect: True Min Eigenvalue: -1.67513 Edge Connectivity: 1
Lovasz Number: 2.0 Girth: 3 Max Eigenvalue: 2.21432 Cut Vertices: 2
Independence Number: 2 Radius: 2 Eigenvalues SD: 1.41421 Min Vertex Cover Size: 3
Clique Number: 3 Diameter: 3 Energy: 6.42864 Spanning Trees: 3
Components: 1 Complement: DIk Spectrum: 2.21432, 1, -0.539189, -1, -1.67513
Induced Subgraphs: DK[, CK, CL, CN, CJ, Bw, BW, BG, A_, A?, @

DJk
Vertices: 5 Regular: False Aut Group Size: 2
Edges: 6 Min Degree: 1 Orbits: 4
Cycles: 3 Max Degree: 3 Fixed Points: 3
Hamiltonian Cycles: 0 Average Degree: 2.4 Vertex Transitive: False
Eulerian: False Degree SD: 0.8 Edge Transitive: False
Planar: True Degree Sequence: 33321 Vertex Connectivity: 1
Perfect: True Min Eigenvalue: -1.77571 Edge Connectivity: 1
Lovasz Number: 2.0 Girth: 3 Max Eigenvalue: 2.64119 Cut Vertices: 1
Independence Number: 2 Radius: 2 Eigenvalues SD: 1.54919 Min Vertex Cover Size: 3
Clique Number: 3 Diameter: 3 Energy: 6.72986 Spanning Trees: 8
Components: 1 Complement: D@s Spectrum: 2.64119, 0.723742, -0.589216, -1, -1.77571
Induced Subgraphs: DJk, C^, CL, CN, CJ, Bw, BW, BG, A_, A?, @
Time: CPU 0.15 s, Wall: 1.72 s }}} {{{id=40| }}} {{{id=39| # Numerical Linear algebra # Very sophisticated numerical linear algebra via numpy # This is a very widely used package in the world of scientific computation. import numpy a = numpy.array([[1,2,3], [4,5,6], [1,1,1]], dtype=float) a /// array([[ 1., 2., 3.], [ 4., 5., 6.], [ 1., 1., 1.]]) }}} {{{id=97| # Or use SAGE's more SAGE-like structured linear algebra (which often uses # numpy behind the scenes) a = random_matrix(RDF, 400) a /// 400 x 400 dense matrix over Real Double Field }}} {{{id=96| time Z = a.eigen() # all eigenvalues and eigenvectors /// CPU time: 1.14 s, Wall time: 1.15 s }}} {{{id=98| }}} {{{id=42| # Linear algebra - exact a = random_matrix(ZZ, 20) show(a) ///
\left(\begin{array}{rrrrrrrrrrrrrrrrrrrr} -1&-1&-1&1&-1&-1&-2&-4&-1&-1&4&1&-1&2&1&1&-1&1&5&1\\ 16&-3&1&-69&4&-1&-17&-2&1&1&-1&-1&-1&-1&1&-2&-6&-1&3&3\\ -6&-2&6&3&1&-2&6&-1&-1&-1&-1&2&26&1&-1&-1&1&2&-4&1\\ 6&23&1&4&1&1&1&-1&1&22&1&-6&1&-5&-1&2&2&3&1&1\\ 1&1&2&-3&1&1&-30&1&3&1&3&-1&-4&-7&1&-1&-1&2&-3&-1\\ -1&-1&4&-1&6&2&1&-2&-3&4&-2&-1&12&1&13&-6&1&-1&-2&10\\ 1&1&1&-2&-4&3&-7&5&-3&1&1&5&1&-3&4&15&-1&-3&1&-5\\ 1&-1&5&30&-1&-1&1&1&-1&-2&55&-1&-1&-2&-2&4&1&1&-1&-2\\ 17&1&1&-24&-3&-31&-1&-4&1&1&1&1&1&-5&-3&-1&13&-2&1&2\\ 10&-36&2&1&1&-1&-4&2&1&5&1&-3&7&1&1&1&-2&1&-2&-1\\ 2&-1&1&3&39&-3&3&-1&3&3&2&-1&-1&-22&1&1&14&-3&6&-3\\ 1&13&2&-50&-3&-215&-4&3&-2&2&1&-1&1&1&3&-2&-1&-2&-3&1\\ 1&-1&1&-1&5&1&1&-1&-1&15&-13&-1&-1&-1&3&-6&3&-1&-1&1\\ 2&1&1&13&2&4&1&1&1&-1&7&1&-2&93&1&1&-4&1&-1&4\\ -5&-677&1&-17&3&-6&1&-2&-13&1&-2&-1&1&-1&2&62&1&5&-2&1\\ 1&1&3&-1&-4&9&-1&1&-6&-1&-4&1&5&-1&-2&-1&4&-1&33&-1\\ -1&1&-2&1&1&1&-114&1&6&1&4&-4&2&-2&21&-1&1&6&1&-1\\ 3&-1&-1&-2&-5&-3&-32&-1&12&1&-1&2&1&-2&2&1&-2&-1&4&1\\ 1&-1&-2&3&-18&1&2&-2&-4&12&33&-1&1&6&-2&-1&1&1&-1&-14\\ 1&-1&-1&9&6&1&-2&1&-1&1&1&1&-1&-1&3&4&1&-3&1&-2 \end{array}\right)
}}} {{{id=43| # Linear algebra -- charpoly # SAGE has the world's asymptotically fastest charpoly algorithm over ZZ, thanks # to Clement Pernet (and Linbox). time show(charpoly(a, 'x')) ///
x^{20} - 100x^{19} + 2013x^{18} - 264054x^{17} + 13872490x^{16} - 354509299x^{15} + 23345292100x^{14} + 2033338887488x^{13} - 29527579561703x^{12} - 1460200231407050x^{11} - 71660209227759x^{10} + 3396416299583629214x^{9} - 13052853327452442478x^{8} - 37561639793620750038x^{7} + 806851663014905752215x^{6} - 164559856632062009862843x^{5} + 420949034316780532797234x^{4} - 1617992298298942609118930x^{3} + 93064950980973598307892484x^{2} - 236793268658658477176673170x + 3400902393921235997950509300
Time: CPU 0.02 s, Wall: 0.20 s }}} {{{id=45| # Number theory -- I am a number theorist show(factor(902834028349082309482038498902832)) ///
2^{4} \cdot 3^{2} \cdot 7 \cdot 167 \cdot 76333 \cdot 88019 \cdot 798255932792263181
}}} {{{id=46| # Number theory -- fast quadratic sieve, thanks to Bill Hart n = next_prime(10^26)*next_prime(10^32) print n time qsieve(n) # good for factoring up to maybe 80 digit numbers -- getting much better... /// 10000000000000000000000006700004900000000000000000000003283 ([100000000000000000000000067, 100000000000000000000000000000049], '') }}} {{{id=56| # Number theory -- fast computation of Bernoull numbers (thanks to PARI) time k = bernoulli(10000) /// Time: CPU 0.33 s, Wall: 0.33 s }}} {{{id=58| len(str(k)) /// 27706 }}} {{{id=59| # Number theory -- all Bernoulli numbers up to a bound mod p # Fast implementation of Buhler et al.'s algorithm, due to David Harvey bernoulli_mod_p(23) /// [1, 4, 13, 17, 13, 6, 10, 5, 10, 9, 15] }}} {{{id=57| time b = bernoulli_mod_p(next_prime(10^5)) /// CPU time: 0.25 s, Wall time: 0.25 s }}} {{{id=60| len(b) /// 50001 }}} {{{id=62| }}} {{{id=48| # Number theory -- elliptic curves # SAGE has far more functions related to the Birch and Swinnerton-Dyer # conjecture than any other program. E = EllipticCurve('37a') show(E) E.rank() ///
y^2 + y = x^3 - x
1 }}} {{{id=49| E.gens() /// [(0 : 0 : 1)] }}} {{{id=50| E.L_ratio() /// 0 }}} {{{id=52| E.sha_an() /// 1 }}} {{{id=51| L = E.padic_lseries(5) }}} {{{id=54| L.series(3) /// O(5^5) + (1 + 4*5 + O(5^2))*T + (3 + O(5^2))*T^2 + (2 + 2*5 + O(5^2))*T^3 + (4 + 3*5 + O(5^2))*T^4 + O(T^5) }}} {{{id=55| }}} {{{id=63| # Numerical computation # A huge amount is available in scipy (http://www.scipy.org/), which is included in SAGE by default. import scipy.special scipy.special.gammainc(float(2),float(3)) /// 0.80085172652854419 }}} {{{id=67| }}} {{{id=66| # Convext Optimization # SAGE includes cvxopt -- http://abel.ee.ucla.edu/cvxopt # This is a powerful package for convex optimization >>> PROBLEM: Solve a linear program minimize 2*x1 + x2 subject to -x1 + x2 <= 1 x1 + x2 >= 2 x2 >= 0 x1 -2*x2 <= 4 }}} {{{id=69| # Optimization: Solve a linear program RealNumber = float # cvxopt doesn't yet work well with SAGE real numbers, so do this from cvxopt.base import matrix from cvxopt import solvers A = matrix([ [-1.0, -1.0, 0.0, 1.0], [1.0, -1.0, -1.0, -2.0] ]) b = matrix([ 1.0, -2.0, 0.0, 4.0 ]) c = matrix([ 2.0, 1.0 ]) sol=solvers.lp(c,A,b) restore('RealNumber') /// pcost dcost gap pres dres k/t 0: 2.6471e+00 1.2409e+00 1e+01 1e+00 7e-01 1e+00 1: 2.5517e+00 2.5403e+00 7e-01 1e-01 6e-02 2e-01 2: 2.4982e+00 2.4993e+00 1e-02 2e-03 1e-03 5e-03 3: 2.5000e+00 2.5000e+00 1e-04 2e-05 1e-05 5e-05 4: 2.5000e+00 2.5000e+00 1e-06 2e-07 1e-07 5e-07 5: 2.5000e+00 2.5000e+00 1e-08 2e-09 1e-09 5e-09 }}} {{{id=72| }}} {{{id=73| # Using other programs from within SAGE maple('2') /// 2 }}} {{{id=81| a = maple(3) + maple(24); a /// 27 }}} {{{id=122| a = maple(3); b = maple(24) }}} {{{id=123| a * b /// 72 }}} {{{id=124| a.ifactor() /// ``(3) }}} {{{id=125| %maple plot(sin(x), x=0..5) /// 1 + AAAAAAAAAAA + AAAA AAAA + AAA AAA + AA AA + AA AAA 0.5 AA AA + AAA AA + AA A + AA AA +AA A *--+--+--+--+--+--+--+--+--+--+--+--+-+--+--+-**--+--+--+--+--+--+--+--+--+ 0 + AAA + 1 2 3 AA 4 5 + AAA + AA -0.5 AAA + AAA + AAA + AAA + AAAA -1+ AAAAAAAAA }}} {{{id=129| }}} {{{id=126| %maple 1/0 /// Traceback (most recent call last): 1/0 File "/Users/was/s/local/lib/python2.5/site-packages/sage/server/support.py", line 255, in syseval return system.eval(cmd) File "/Users/was/s/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 523, in eval return '\n'.join([self._eval_line(L, **kwds) for L in code.split('\n') if L != '']) File "/Users/was/s/local/lib/python2.5/site-packages/sage/interfaces/maple.py", line 370, in _eval_line wait_for_prompt=wait_for_prompt).replace('\\\n','').strip() File "/Users/was/s/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 428, in _eval_line return self._eval_line_using_file(line, tmp) File "/Users/was/s/local/lib/python2.5/site-packages/sage/interfaces/expect.py", line 413, in _eval_line_using_file s = self._eval_line(self._read_in_file_command(tmp), allow_use_file=False) File "/Users/was/s/local/lib/python2.5/site-packages/sage/interfaces/maple.py", line 384, in _eval_line raise RuntimeError, "An error occured running a Maple command:\nINPUT:\n%s\nOUTPUT:\n%s"%(line, z) RuntimeError: An error occured running a Maple command: INPUT: read "/Users/was/.sage//temp/rank4.local/4506//interface//tmp"; OUTPUT: Error, numeric exception: division by zero }}} {{{id=82| type(a) /// }}} {{{id=84| a.ifactor() /// ``(3)^3 }}} {{{id=80| maple.fsolve('x^2=cos(x)+4', 'x=0..5') /// 1.914020619 }}} {{{id=74| %maple a := 10 /// a := 10 }}} {{{id=111| %maple a*a /// 100 }}} {{{id=110| # You can also write compiled Python (Cython) and FORTRAN directly in SAGE. }}} {{{id=112| # The same program in Python: def is2pow(n): while n != 0 and n%2 == 0: n = n >> 1 return n == 1 }}} {{{id=113| time [n for n in range(10^5) if is2pow(n)] /// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] CPU time: 1.96 s, Wall time: 1.96 s }}} {{{id=8| %cython def is2pow(unsigned int n): while n != 0 and n%2 == 0: n = n >> 1 return n == 1 }}} {{{id=11| time [n for n in range(10^5) if is2pow(n)] /// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] CPU time: 0.04 s, Wall time: 0.04 s }}} {{{id=115| 1.94/0.04 /// 48.5000000000000 }}} {{{id=114| }}} {{{id=17| # FORTRAN -- directly from SAGE }}} {{{id=118| %fortran C FILE: FIB1.F SUBROUTINE FIB(A,N) C C CALCULATE FIRST N FIBONACCI NUMBERS C INTEGER N REAL*8 A(N) DO I=1,N IF (I.EQ.1) THEN A(I) = 0.0D0 ELSEIF (I.EQ.2) THEN A(I) = 1.0D0 ELSE A(I) = A(I-1) + A(I-2) ENDIF ENDDO END C END FILE FIB1.F /// None }}} {{{id=117| import numpy m=numpy.array([0]*100,dtype=float) fib(m,100) print(m) /// [ 0.00000000e+00 1.00000000e+00 1.00000000e+00 2.00000000e+00 3.00000000e+00 5.00000000e+00 8.00000000e+00 1.30000000e+01 2.10000000e+01 3.40000000e+01 5.50000000e+01 8.90000000e+01 1.44000000e+02 2.33000000e+02 3.77000000e+02 6.10000000e+02 9.87000000e+02 1.59700000e+03 2.58400000e+03 4.18100000e+03 6.76500000e+03 1.09460000e+04 1.77110000e+04 2.86570000e+04 4.63680000e+04 7.50250000e+04 1.21393000e+05 1.96418000e+05 3.17811000e+05 5.14229000e+05 8.32040000e+05 1.34626900e+06 2.17830900e+06 3.52457800e+06 5.70288700e+06 9.22746500e+06 1.49303520e+07 2.41578170e+07 3.90881690e+07 6.32459860e+07 1.02334155e+08 1.65580141e+08 2.67914296e+08 4.33494437e+08 7.01408733e+08 1.13490317e+09 1.83631190e+09 2.97121507e+09 4.80752698e+09 7.77874205e+09 1.25862690e+10 2.03650111e+10 3.29512801e+10 5.33162912e+10 8.62675713e+10 1.39583862e+11 2.25851434e+11 3.65435296e+11 5.91286730e+11 9.56722026e+11 1.54800876e+12 2.50473078e+12 4.05273954e+12 6.55747032e+12 1.06102099e+13 1.71676802e+13 2.77778900e+13 4.49455702e+13 7.27234602e+13 1.17669030e+14 1.90392491e+14 3.08061521e+14 4.98454012e+14 8.06515533e+14 1.30496954e+15 2.11148508e+15 3.41645462e+15 5.52793970e+15 8.94439432e+15 1.44723340e+16 2.34167283e+16 3.78890624e+16 6.13057907e+16 9.91948531e+16 1.60500644e+17 2.59695497e+17 4.20196141e+17 6.79891638e+17 1.10008778e+18 1.77997942e+18 2.88006719e+18 4.66004661e+18 7.54011380e+18 1.22001604e+19 1.97402742e+19 3.19404346e+19 5.16807089e+19 8.36211435e+19 1.35301852e+20 2.18922996e+20] }}} {{{id=116| 2^2000 /// 114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376 }}} {{{id=127| latex(integrate(sin(x^2) + cos(x^2),x)) /// \frac{{\sqrt{ \pi } \cdot \left( {\left( {\sqrt{ 2 } \cdot i} + \sqrt{ 2 } \right) \cdot \left( \text{erf} \left( \frac{{\left( {\sqrt{ 2 } \cdot i} + \sqrt{ 2 } \right) \cdot x}}{2} \right) \right)} + {\left( {\sqrt{ 2 } \cdot i} - \sqrt{ 2 } \right) \cdot \left( \text{erf} \left( \frac{{\left( {\sqrt{ 2 } \cdot i} - \sqrt{ 2 } \right) \cdot x}}{2} \right) \right)} \right)}}{8} - \frac{{\sqrt{ \pi } \cdot \left( {\left( {\sqrt{ 2 } \cdot i} - \sqrt{ 2 } \right) \cdot \left( \text{erf} \left( \frac{{\left( {\sqrt{ 2 } \cdot i} + \sqrt{ 2 } \right) \cdot x}}{2} \right) \right)} + {\left( {\sqrt{ 2 } \cdot i} + \sqrt{ 2 } \right) \cdot \left( \text{erf} \left( \frac{{\left( {\sqrt{ 2 } \cdot i} - \sqrt{ 2 } \right) \cdot x}}{2} \right) \right)} \right)}}{8} }}} {{{id=128| }}}