Why You Will Like an Open-Source Computer Algebra System Robert A. Beezer University of Puget Sound Kansas City Regional Mathematics Technology Expo October 5, 2012
What is Sage?

1. An open source system for advanced mathematics.
2. An open source mathematics distribution (like Linux) with Python as the glue.
3. More than just a CAS.
4. A tool for mathematics research.
5. Industrial Stength: New NIST approved SHA-3 hash function developed with Sage.
6. A tool for learning and teaching mathematics. Mission Statement
Create a viable free open source alternative to Magma, Maple, Mathematica, and Matlab.

An Overview

• Created in 2005 by William Stein.
• Free and open, GPL license.
• Includes about 100 open source packages.
• Now has around 500,000 lines of new code, by several hundred mathematician-programmers.

Some of the 100 packages included:

• Groups, Algorithms, Programming (GAP) - group theory
• PARI - rings, finite fields, field extensions
• Singular - commutative algebra
• SciPy/NumPy - scientific computing, numerical linear algebra
• Integer Matrix Library (IML) - integer, rational matrices
• CVXOPT - linear programming, optimization
• NetworkX - graph theory
• Pynac - symbolic manipulation
• Maxima - calculus, differential equations

The Sage Notebook

The Sage Notebook is a web application that provides a convenient interface to Sage commands, components and features. This separates the computational engine from the user's device and operating system.

Algebra

Simple manipulation of $$(3x+4)^5$$.

expression = (3*x+4)^5 expanded = expression.expand() expanded factored = expanded.factor() factored

Partial fraction decomposition of $\frac{x^{3} + 47 \, x^{2} + 122 \, x + 424}{x^{4} - 5 \, x^{3} - 12 \, x^{2} - 76 \, x - 160}$

expression = (x^3 + 47*x^2 + 122*x + 424)/(x^4 - 5*x^3 - 12*x^2 - 76*x - 160) expression.partial_fraction()

2-D plotting is what you would expect.

f(x) = x^3 - 2*x^2 - 11*x + 12 plot(f, (x, -4, 5))

And style it.

plot(f, (x, -4, 5), color="red", thickness=4, linestyle="--")

Plus a shifted copy (which passes through the origin).

g(x) = f(x)-12 P1 = plot(f, (x, -4, 5), color="red", axes_labels=['$x$','$y$'],legend_label='$f(x)$') P2 = plot(g, (x, -4, 5), color="blue", axes_labels=['$x$','$y$'],legend_label='$g(x)$') P1+P2
Calculus

A symbolic derivative of $$f(x)=x^3e^{-x}$$. (from Maxima)

f(x) = x^3*e^-x df = f.derivative() df

Derivative of a function is again a function, and can be evaluated.

slope = df(4) slope

Arbitrary precision numerical values on request (from MPmath).

N(slope, digits=20)

And a plot (from matplotlib).

curve = plot(f, (x, 0, 10), color="blue") line = plot(f(4) + slope*(x-4), (x, 2, 8), color="red") tangency = point((4,f(4)), size=50, color="black") curve + line + tangency

Study the multivariate integral $$\displaystyle\int_{-4}^4\int_0^{x^2} y^2-10x^2\,dy\,dx$$.

var('x y z') integral(integral(y^2-10*x^2, (y, 0, x^2)), (x, -4, 4)) surface = plot3d(y^2-10*x^2, (x, -4, 4), (y, 0, 16)) show(surface) region = implicit_plot3d(y-x^2, (x, -4, 4), (y, 0, 16), (z, 0, 98), color='red', opacity=0.20) show(surface+region)
Linear Algebra

Many possible fields and rings: finite fields, field extensions, algebraic numbers.

A = matrix(QQ, [[1, -2, 3, 2, -1, -4, -3, 4], [3, -2, 2, 5, 0, 6, -5, -5], [0, -1, 2, 1, -2, -4, -1, 4], [-3, 2, -1, -1, -6, -3, 5, 3], [3, -4, 4, 0, 7, -7, -7, 6]]) A A.rref() b = vector(QQ, [2, -1, 3, 4, -3]) A.solve_right(b)

Subspaces act like vector spaces.

null_space = A.right_kernel() null_space null_space.dimension() null_space.basis()

And it is fast.

A = random_matrix(ZZ, 500, 500) time A.determinant()

Many, many more functions for matrices (tab-completion on A, ]]>).

A.
What Makes Sage Different? Python

Sage uses a popular language as its own programming language. Python is used to knit together the many different packages in Sage, written in many different languages. New code is typically written in Python, and automatically optimized by translation to C via Cython. A by-product is that Python is always available and integrated.

Most importantly, students learn Python coincidentally.

Fermat's $$4n+1$$ Theorem (Gauss): Every prime of the form $$4n+1$$ can be written as a sum of two squares (uniquely).

Python's list comprehensions entirely mimic standard mathematical notation. The prime numbers of the form $$4n + 1$$ less than 100. (Perhaps to study Fermat's 4n+1 Theorem.) Mathematically: $\{4n+1\mid n\in{\mathbb N}, 4n+1\text{ is prime}, 4n+1\lt 100\}$ Pythonically:

primes = [4*n + 1 for n in range(1, 25) if (4*n+1).is_prime()] primes

Now a brute-force check.

[(x, y, p) for p in primes for x in range(p) for y in range(x) if x^2 + y^2 == p]
Interactive Explorations

Interactive demonstrations are easy to create with the interact decorator and modified function arguments. Python is the language of Sage, and the language for building interacts.

Behavior of $$f(x)=x^n$$ on $$(0,1)$$ for $$1\leq n\leq 40$$ (from a student).

@interact def plotter(maxdegree=range(2,41)): import sage.plot.colors colors = sage.plot.colors.rainbow(maxdegree+1) var('x') wholeplot = plot(x^1, (x, 0, 1), color=colors) for i in range(2, maxdegree+1): newplot = plot(x^i, (x, 0, 1), color=colors[i]) wholeplot = wholeplot + newplot show(wholeplot)

The canonical example.

@interact def taylor(order=slider(1, 12, 1, default=Integer(2), label="Degree of polynomial $\hat{f}$")): var('x') x0 = 0 f = sin(x)*e^(-x) p = plot(f, -1, 5, thickness=2) dot = point((x0,f(x=x0)), pointsize=80, rgbcolor=(1,0,0)) ft = f.taylor(x, x0 ,order) pt = plot(ft, -1, 5, color='green', thickness=2) html("Taylor series approximation to ${0}$ of degree {1} at $x={2}$".format(latex(f), Integer(order), x0) ) show(dot + p + pt, ymin = -0.5, ymax = 1)

Digital compression with the singular value decomposition. Note the simple use of an existing Python library.

import pylab A_image = pylab.mean(pylab.imread(DATA + 'mystery.png'), 2) @interact def svd_image(i=(1,(1..50)), display_axes=True): u,s,v = pylab.linalg.svd(A_image) A = sum(s[j]*pylab.outer(u[0:,j], v[j,0:]) for j in range(i)) # g = graphics_array([matrix_plot(A),matrix_plot(A_image)]) show(matrix_plot(A), axes=display_axes, figsize=(6,8)) html('

## Compressed using %s singular values

'%i)
LaTeX Integration

$\dots$is superb.

LaTeX versions of mathematical objects integrate(tan(x)^6, x) show(integrate(tan(x)^6, x)) latex(integrate(tan(x)^6, x)) A = random_matrix(QQ, 6, num_bound=9, den_bound=9) show(A) latex(A) LaTeX versions of graphs P = graphs.PetersenGraph() plot(P) P.set_latex_options(vertex_shape='circle', vertex_color='red', vertex_label_color='green', edge_color='blue') latex(P) Embedded word processor

Shift-Click on blue bar to activate. TeX-aware.

We can add text to our notebooks using TeX syntax and dollar signs. Previous multivariate integral:

\int_0^4\int_0^{x^2} y^2-10x^2\,dy\,dx

Can also embed images via the embedded word processor.

SageTeX

SageTeX allows you to call Sage to compute values, expressions or plots for automatic inclusion in your LaTeX document. (And will write your papers for you!)

In your LaTeX source: Today's date, 2012-10-05 = 20121005, factors as \sage{factor(20121005)}.

Final Document: Today's date, 2012-10-05 = 20121005, factors as $$5 \cdot 281 \cdot 14321$$.

Help, Doctests, Source Code

A huge number of examples are provided for (a) learning to use Sage commands, and (b) to test Sage commands. We call these doctests.''

M = matrix(QQ, [[1, -2, 2], [-4, 5, 6], [1, 2, 4]]) M

Illustrate tab-completion, help, doctests, source code for .columns() method. (Using ., ?, ?? plus TAB.)

M.

Why is Open Important?

• Standards have fueled the growth of the Internet
• Open-ness encourages innovation
• Education is important, students are poor

Examples

• Sage Notebook Interface
• William Stein, Tom Boothby, Alex Clemesha et alia, University of Washington (NSF support)
• This worksheet is an example
• Sage Cell Server
• Jason Grout and students, Drake Univesity (NSF support)
• Example:  plot(x^2, (x, -3, 2), color='red')
• Page Source for Cell Server
• The crowd-sourced Interact Database
• Another project from Drake University (NSF support)
• Pizza Demo, by Bruce Cohen, Lowell High School, San Francisco
• Sage Cells embedded in Textbooks
• Print QR codes for Smartphones
• Sang-Gu Lee and students, Sungkyunkwan University, Korea
• • Loaded Sage Cell for Example 3
• Can embed cell content in the QR code
• Sage Cells embedded in WeBWorK
• Freemium Sage Computing Power
• William Stein, University of Washington (NSF support)
• Salvus at Salv.us
• Pro version: high-availability, reliable, redundant and fast ("in the cloud")
• Free version: spare cycles
• A = random_matrix(ZZ, 100, 100); A.determinant()
• Geogebra in a Sage Notebook
• Bruce Cohen, Lowell High School, San Francisco
• Geogebra demonstrations can be embedded in a Sage worksheet, or even a Sage cell

html.iframe("http://boxen.math.washington.edu/home/bic/empty_geogebra.html", width=800, height=600)