Sage Days 20.5 : Demo

 

The Sage notebook: an interface to your software!

{{{id=28| 2+3 /// }}} {{{id=27| /// }}} {{{id=65| /// }}} {{{id=64| /// }}} {{{id=26| /// }}}

Interface with Magma (you need to have Magma installed)

We use Sage to construct a random $150\times150$ matrix over the integers with entries of size up to $2^{128}$.

{{{id=1| a = random_matrix(ZZ,150,x=-2^128,y=2^128) /// }}}

A random entry of the matrix:

{{{id=73| a[72,17] /// }}}

We use Magma to compute the determinant of the matrix:

{{{id=3| b = magma(a) magma.eval('time e := Determinant(%s);'%b.name()) /// }}}

We can also do this with Sage, and it's faster!

{{{id=71| time d = a.determinant() /// }}}

And we get the same answer:

{{{id=4| d == magma('e') /// }}}

Here's an example of Sage and Magma computing the Hermite normal form of a random matrix of size $100\times100$ over the integers. 

{{{id=5| a = random_matrix(ZZ,100,x=-2^64,y=2^64) time h = a.hermite_form() /// }}} {{{id=6| b = magma(a) magma.eval('time e := HermiteForm(%s);'%b.name()) /// }}} {{{id=7| h == magma('e') /// }}} {{{id=12| /// }}} {{{id=8| /// }}}

Interface with Maple (you need to have Maple installed)

Below we use Maple and Sage to compute the number of partitions of $27020$.

{{{id=16| time maple('combinat[numbpart](27020)') /// }}} {{{id=20| time number_of_partitions(27020) /// }}}

Not only is Sage faster, it also returns the correct answer: http://www.research.att.com/~njas/sequences/A110375.

{{{id=19| /// }}} {{{id=21| /// }}} {{{id=56| /// }}}

Embed Java Applets into the Sage Notebook

{{{id=76| load DATA+'EuclideanK44Mech.sage' html(jsp_string) /// }}} {{{id=82| /// }}} {{{id=83| /// }}}

Beautiful documentation

Type plot( and then press the TAB key.

{{{id=54| plot( /// }}} {{{id=53| /// }}}

Click on Help at the top of this page.

{{{id=52| /// }}}

There is also Live Documentation in which you can evaluate the examples.

{{{id=37| /// }}}

Interact with your mathematics by creating interactive widgets

interact: curves of pursuit

{{{id=36| npi = RDF(pi) def rot(t): from math import cos,sin return matrix([[cos(t),sin(t)],[-sin(t),cos(t)]]) def pursuit(n,x0,y0,lamb,steps = 100, threshold = .01): paths = [[[x0,y0]]] for i in range(1,n): rx,ry = list(rot(2*npi*i/n)*vector([x0,y0])) paths.append([[rx,ry]]) oldpath = [x[-1] for x in paths] for q in range(steps): diffs = [[oldpath[(j+1)%n][0]-oldpath[j][0],oldpath[(j+1)%n][1]-oldpath[j][1]] for j in range(n)] npath = [[oldpath[j][0]+lamb*diffs[j][0],oldpath[j][1]+lamb*diffs[j][1]] for j in range(n)] for j in range(n): paths[j].append(npath[j]) oldpath = npath return paths html('

Curves of Pursuit

') @interact def curves_of_pursuit(n = slider([2..20],default = 5, label="# of points"),steps = slider([floor(1.4^i) for i in range(2,18)],default = 10, label="# of steps"), stepsize = slider(srange(.01,1,.01),default = .2, label="stepsize"), colorize = selector(['BW','Line color', 'Filled'],default = 'BW')): outpaths = pursuit(n,0,1,stepsize, steps = steps) mcolor = (0,0,0) outer = line([q[0] for q in outpaths]+[outpaths[0][0]], rgbcolor = mcolor) polys = Graphics() if colorize=='Line color': colors = [hue(j/steps,1,1) for j in range(len(outpaths[0]))] elif colorize == 'BW': colors = [(0,0,0) for j in range(len(outpaths[0]))] else: colors = [hue(j/steps,1,1) for j in range(len(outpaths[0]))] polys = sum([polygon([outpaths[(i+1)%n][j+1],outpaths[(i+1)%n][j], outpaths[i][j+1]], rgbcolor = colors[j]) for i in range(n) for j in range(len(outpaths[0])-1)]) #polys = polys[0] colors = [(0,0,0) for j in range(len(outpaths[0]))] nested = sum([line([q[j] for q in outpaths]+[outpaths[0][j]], rgbcolor = colors[j]) for j in range(len(outpaths[0]))]) lpaths = [line(x, rgbcolor = mcolor) for x in outpaths] show(sum(lpaths)+nested+polys, axes = False, figsize = [5,5], xmin = -1, xmax = 1, ymin = -1, ymax =1) /// }}}

interact: Taylor polynomials

{{{id=38| var('x') @interact def g(f=sin(x)*e^(-x), c=0, n=(1..30), xinterval=range_slider(-5, 5, 1, default=(-1,4), label="x-interval"), yinterval=range_slider(-50, 50, 1, default=(-1,1), label="y-interval")): x0 = c degree = n xmin,xmax = xinterval ymin,ymax = yinterval p = plot(f, xmin, xmax, thickness=4) dot = point((x0,f(x=x0)),pointsize=80,rgbcolor=(1,0,0)) ft = f.taylor(x,x0,degree) pt = plot(ft, xmin, xmax, color='red', thickness=2, fill=f) show(dot + p + pt, ymin=ymin, ymax=ymax, xmin=xmin, xmax=xmax) html('$f(x)\;=\;%s$'%latex(f)) html('$P_{%s}(x)\;=\;%s+R_{%s}(x)$'%(degree,latex(ft),degree)) /// }}}

interact: Taylor polynomials

{{{id=35| var('x') @interact def _(f=arctan(x), c=0, n=(0..25), xinterval=range_slider(-5, 5, 1, default=(-1,1), label="x-interval"), yinterval=range_slider(-50, 50, 1, default=(-1,1), label="y-interval")): x0 = c degree = n xmin,xmax = xinterval ymin,ymax = yinterval p = plot(f, xmin, xmax, thickness=4) dot = point((x0,f(x=x0)),pointsize=80,rgbcolor=(1,0,0)) ft = f.taylor(x,x0,degree) pt = plot(ft, xmin, xmax, color='red', thickness=2, fill=f) show(dot + p + pt, ymin = ymin, ymax = ymax, xmin=xmin, xmax=xmax) html('$f(x)\;=\;%s$'%latex(f)) html('$P_{%s}(x)\;=\;%s+R_{%s}(x)$'%(degree,latex(ft),degree)) /// }}} {{{id=34| /// }}} {{{id=60| /// }}} {{{id=59| /// }}}

Cython: Python $\longmapsto$ C

Here is a function that computes $\sum_{k=0}^N k$ in pure Python.

{{{id=63| def mysum(N): s = int(0) for k in range(1,N): s += k return s /// }}} {{{id=62| time mysum(10^7) /// }}} {{{id=61| /// }}}

Let us compare this with the Cython version.

{{{id=58| %cython def mysum_cython(N): cdef long long s = 0 cdef int k for k in range(1,N): s += k return s /// }}} {{{id=57| time mysum_cython(10^7) /// }}} {{{id=33| /// }}} {{{id=32| /// }}}

Drawing graphs

{{{id=31| HS = graphs.HyperStarGraph(6,3) /// }}} {{{id=30| HS.plot() /// }}} {{{id=24| HS.plot3d() /// }}} {{{id=39| graph_editor(HS) /// }}} {{{id=40| /// }}} {{{id=46| /// }}}

Use the source!

{{{id=44| import scipy from scipy import io x=io.loadmat('%s/yodapose.mat'%DATA) # you can change it to yodapose/yodapose_low to use the high/low res version from sage.plot.plot3d.index_face_set import IndexFaceSet V=x['V'] F3=x['F3']-1 F4=x['F4']-1 yoda=IndexFaceSet(F3,V,color='green')+IndexFaceSet(F4,V,color='green') yoda.show(figsize=5, frame=False) /// }}} {{{id=45| /// }}} {{{id=43| /// }}} {{{id=42| /// }}}