Sage Days 20.5: Demo
system:sage


<h1 style="text-align: center;">Sage Days 20.5 : Demo</h1>
<p>&nbsp;</p>
<h2>The Sage notebook: an interface to your software!</h2>

{{{id=28|
2+3
///
}}}

{{{id=27|

///
}}}

{{{id=65|

///
}}}

{{{id=64|

///
}}}

{{{id=26|

///
}}}

<h2>Interface with Magma (you need to have Magma installed)</h2>
<p>We use Sage to construct a random $150\times150$ matrix over the integers with entries of size up to $2^{128}$.</p>

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

<p>A random entry of the matrix:</p>

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

<p>We use Magma to compute the determinant of the matrix:</p>

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

<p>We can also do this with Sage, and it's faster!</p>

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

<p>And we get the same answer:</p>

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

<p>Here's an example of Sage and Magma computing the <a title="Wikipedia: Hermite normal form" href="http://en.wikipedia.org/wiki/Hermite_normal_form" target="_blank">Hermite normal form</a> of a random matrix of size $100\times100$ over the integers.&nbsp;</p>

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

///
}}}

<h2>Interface with Maple (you need to have Maple installed)</h2>

<p>Below we use Maple and Sage to compute the number of <a title="Wikipedia: Partition (number theory)" href="http://en.wikipedia.org/wiki/Partition_(number_theory)" target="_blank">partitions</a> of $27020$.</p>

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

{{{id=20|
time number_of_partitions(27020)
///
}}}

<p>Not only is Sage faster, it also returns the correct answer: <a href="http://www.research.att.com/~njas/sequences/A110375">http://www.research.att.com/~njas/sequences/A110375</a>.</p>

{{{id=19|

///
}}}

{{{id=21|

///
}}}

{{{id=56|

///
}}}

<h2>Embed Java Applets into the Sage Notebook</h2>

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

{{{id=82|

///
}}}

{{{id=83|

///
}}}

<h2>Beautiful documentation</h2>
<p>Type <strong>plot(</strong> and then press the <strong>TAB</strong> key.</p>

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

{{{id=53|

///
}}}

<p>Click on <a title="documentation" href="../../../help" target="_blank">Help</a> at the top of this page.</p>

{{{id=52|

///
}}}

<p>There is also <a title="live documentation" href="../../../doc/live/reference/index.html" target="_blank">Live Documentation</a> in which you can evaluate the examples.</p>

{{{id=37|

///
}}}

<h2>Interact with your mathematics by creating interactive widgets</h2>

<h2>interact: curves of pursuit</h2>

{{{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('<h3>Curves of Pursuit</h3>')
@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)
///
}}}

<h2>interact: Taylor polynomials</h2>

{{{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))
///
}}}

<h2>interact: Taylor polynomials</h2>

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

///
}}}

<h2>Cython: Python $\longmapsto$ C</h2>
<p>Here is a function that computes $\sum_{k=0}^N k$ in pure Python.</p>

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

///
}}}

<p>Let us compare this with the Cython version.</p>

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

///
}}}

<h2>Drawing graphs</h2>

{{{id=31|
HS = graphs.HyperStarGraph(6,3)
///
}}}

{{{id=30|
HS.plot()
///
}}}

{{{id=24|
HS.plot3d()
///
}}}

{{{id=39|
graph_editor(HS)
///
}}}

{{{id=40|

///
}}}

{{{id=46|

///
}}}

<h2>Use the source!</h2>

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

///
}}}