MAA talk on Sage

453 days ago by wstein

Demo: Factoring

Factoring an integer:

factor(2012) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}2^{2} \cdot 503
\newcommand{\Bold}[1]{\mathbf{#1}}2^{2} \cdot 503

Factoring a symbolic expression:

var('x,y') factor(x^8 - y^2*e^(2*x)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x^{4} - y e^{x}\right)} {\left(x^{4} + y e^{x}\right)}
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x^{4} - y e^{x}\right)} {\left(x^{4} + y e^{x}\right)}

Factoring a polynomial over a nonprime finite field:

F.<alpha> = GF(49) R.<x> = F[] factor(x^4 + x^3 - 2) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}(x + \alpha + 1) \cdot (x + 6 \alpha + 2) \cdot (x + 6)^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}(x + \alpha + 1) \cdot (x + 6 \alpha + 2) \cdot (x + 6)^{2}
a = random_matrix(F, 4,6); a 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr}
\alpha + 6 & 2 \alpha + 6 & 5 \alpha + 3 & 2 \alpha & 0 & 5 \alpha + 4 \\
2 \alpha + 3 & 2 \alpha + 5 & \alpha + 1 & 5 \alpha + 5 & 5 & 2 \alpha + 1 \\
4 \alpha + 2 & 3 \alpha + 3 & 4 \alpha & 4 & 2 \alpha + 5 & 6 \alpha \\
3 \alpha & \alpha + 6 & 4 \alpha & 6 \alpha + 3 & 3 \alpha & 4 \alpha + 5
\end{array}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr}
\alpha + 6 & 2 \alpha + 6 & 5 \alpha + 3 & 2 \alpha & 0 & 5 \alpha + 4 \\
2 \alpha + 3 & 2 \alpha + 5 & \alpha + 1 & 5 \alpha + 5 & 5 & 2 \alpha + 1 \\
4 \alpha + 2 & 3 \alpha + 3 & 4 \alpha & 4 & 2 \alpha + 5 & 6 \alpha \\
3 \alpha & \alpha + 6 & 4 \alpha & 6 \alpha + 3 & 3 \alpha & 4 \alpha + 5
\end{array}\right)
a.rref() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr}
1 & 0 & 0 & 0 & \alpha + 3 & 6 \alpha + 1 \\
0 & 1 & 0 & 0 & 3 & 3 \alpha + 1 \\
0 & 0 & 1 & 0 & 5 \alpha + 3 & 6 \\
0 & 0 & 0 & 1 & 5 \alpha + 5 & 3 \alpha + 6
\end{array}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr}
1 & 0 & 0 & 0 & \alpha + 3 & 6 \alpha + 1 \\
0 & 1 & 0 & 0 & 3 & 3 \alpha + 1 \\
0 & 0 & 1 & 0 & 5 \alpha + 3 & 6 \\
0 & 0 & 0 & 1 & 5 \alpha + 5 & 3 \alpha + 6
\end{array}\right)
 
       

Demo: Graph Theory

set_random_seed(1); G = graphs.RandomLobster(8, .6, .3); show(G) 
       
G.automorphism_group() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\langle (12,26) \rangle
\newcommand{\Bold}[1]{\mathbf{#1}}\langle (12,26) \rangle
graph_editor(G) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{}
G.is_planar() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}{\rm True}
\newcommand{\Bold}[1]{\mathbf{#1}}{\rm True}
 
       

Demo: Solving Equations

Solve a quadratic equation:

x = var('x'); solve(x^2 + 7*x == 5, x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, \sqrt{69} - \frac{7}{2}, x = \frac{1}{2} \, \sqrt{69} - \frac{7}{2}\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, \sqrt{69} - \frac{7}{2}, x = \frac{1}{2} \, \sqrt{69} - \frac{7}{2}\right]

Solve a system of two linear equations with one unknown coefficient \alpha:

var('alpha, y') solve([3*x + 7*y == 2, alpha*x + 3*y == 8], x,y) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \frac{50}{7 \, \alpha - 9}, y = \frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \frac{50}{7 \, \alpha - 9}, y = \frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right]\right]
A = matrix([[3,7], [alpha, 3]]); A 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
3 & 7 \\
\alpha & 3
\end{array}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
3 & 7 \\
\alpha & 3
\end{array}\right)
v = vector([2,8]); v 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(2,8\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(2,8\right)
A.solve_right(v) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(-\frac{14 \, {\left(\alpha - 12\right)}}{3 \, {\left(7 \, \alpha - 9\right)}} + \frac{2}{3},\frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(-\frac{14 \, {\left(\alpha - 12\right)}}{3 \, {\left(7 \, \alpha - 9\right)}} + \frac{2}{3},\frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right)
 
       

Demo: Computing Symbolic Integrals

f = 1/sqrt(x^2 + 2*x - 1); f.integrate(x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(2 \, x + 2 \, \sqrt{x^{2} + 2 \, x - 1} + 2\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(2 \, x + 2 \, \sqrt{x^{2} + 2 \, x - 1} + 2\right)
g = integrate(sin(x)*tan(x), x); g 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \log\left(\sin\left(x\right) - 1\right) + \frac{1}{2} \, \log\left(\sin\left(x\right) + 1\right) - \sin\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \log\left(\sin\left(x\right) - 1\right) + \frac{1}{2} \, \log\left(\sin\left(x\right) + 1\right) - \sin\left(x\right)
h = g.diff(x); h 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) - 1\right)}} + \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) + 1\right)}} - \cos\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) - 1\right)}} + \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) + 1\right)}} - \cos\left(x\right)
h - sin(x)*tan(x) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-\sin\left(x\right) \tan\left(x\right) - \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) - 1\right)}} + \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) + 1\right)}} - \cos\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}-\sin\left(x\right) \tan\left(x\right) - \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) - 1\right)}} + \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) + 1\right)}} - \cos\left(x\right)
(h - sin(x)*tan(x)).simplify_full() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}0
\newcommand{\Bold}[1]{\mathbf{#1}}0
 
       

Demo: Plotting 2D Functions

plot(sin(1/x^2), (x,.1,.5)) 
       
var('x') @interact def h(f=1/sqrt(x^2 + 2*x - 1), t=(1..20), grid=True): show(plot(f, (x, .6, 2), thickness=t, color='purple', fill=True, gridlines=grid)) 
       

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

 
       

Demo: Plotting a Function in 3D

f(x,y) = sin(x-y)*y*cos(x) G3d = (plot3d(f, (x,-3,3), (y,-3,3), opacity=.9, color='red') + icosahedron(color='blue')) @interact def _(viewer=['tachyon', 'canvas3d', 'jmol']): G3d.show(viewer=viewer) 
       

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

 
       
 
       

Demo: Interactive Image Compression

import pylab; import numpy A_image = numpy.mean(pylab.imread(DATA + 'santarosa.png'), 2) u,s,v = numpy.linalg.svd(A_image) S = numpy.zeros( A_image.shape ) S[:len(s),:len(s)] = numpy.diag(s) n = A_image.shape[0] @interact def svd_image(i = ("Eigenvalues (quality)",(20,(1..A_image.shape[0]//2)))): A_approx = numpy.dot(numpy.dot(u[:,:i], S[:i,:i]), v[:i,:]) g = graphics_array([matrix_plot(A_approx), matrix_plot(A_image)]) show(g, axes=False, figsize=(10,6)) html("Compressed to %.1f%% of size using %s eigenvalues."%( 100*(2.0*i*n+i)/(n*n), i)) 
       

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

 
       

Demo: What do you want to see?