Why You Will Like an Open-Source Computer Algebra System
Kansas City Regional Mathematics Technology Expo

Robert A. Beezer
University of Puget Sound
October 5, 2012

1 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.




1.1 An Overview

Some of the 100 packages included:

2 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.

2.1 Algebra

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

{{{id=1| expression = (3*x+4)^5 expanded = expression.expand() expanded /// }}} {{{id=2| 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}\]

{{{id=4| 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.

{{{id=6| f(x) = x^3 - 2*x^2 - 11*x + 12 plot(f, (x, -4, 5)) /// }}}

And style it.

{{{id=8| plot(f, (x, -4, 5), color="red", thickness=4, linestyle="--") /// }}}

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

{{{id=10| 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 /// }}}

2.2 Calculus

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

{{{id=12| f(x) = x^3*e^-x df = f.derivative() df /// }}}

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

{{{id=14| slope = df(4) slope /// }}}

Arbitrary precision numerical values on request (from MPmath).

{{{id=16| N(slope, digits=20) /// }}}

And a plot (from matplotlib).

{{{id=18| 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\).

{{{id=20| var('x y z') integral(integral(y^2-10*x^2, (y, 0, x^2)), (x, -4, 4)) /// }}} {{{id=21| surface = plot3d(y^2-10*x^2, (x, -4, 4), (y, 0, 16)) show(surface) /// }}} {{{id=22| region = implicit_plot3d(y-x^2, (x, -4, 4), (y, 0, 16), (z, 0, 98), color='red', opacity=0.20) show(surface+region) /// }}}

2.3 Linear Algebra

Many possible fields and rings: finite fields, field extensions, algebraic numbers.
Over the integers and rationals powered by Integer Matrix Library (IML).

{{{id=24| 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 /// }}} {{{id=25| A.rref() /// }}} {{{id=26| b = vector(QQ, [2, -1, 3, 4, -3]) A.solve_right(b) /// }}}

Subspaces act like vector spaces.

{{{id=28| null_space = A.right_kernel() null_space /// }}} {{{id=29| null_space.dimension() /// }}} {{{id=30| null_space.basis() /// }}}

And it is fast.

{{{id=32| A = random_matrix(ZZ, 500, 500) time A.determinant() /// }}}

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

{{{id=34| A. /// }}}

3 What Makes Sage Different?

3.1 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:

{{{id=36| primes = [4*n + 1 for n in range(1, 25) if (4*n+1).is_prime()] primes /// }}}

Now a brute-force check.

{{{id=38| [(x, y, p) for p in primes for x in range(p) for y in range(x) if x^2 + y^2 == p] /// }}}

3.2 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).

{{{id=40| @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[1]) 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.

{{{id=42| @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.

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

3.3 LaTeX Integration

$\dots$is superb.

3.3.1 LaTeX versions of mathematical objects
{{{id=46| integrate(tan(x)^6, x) /// }}} {{{id=47| show(integrate(tan(x)^6, x)) /// }}} {{{id=48| latex(integrate(tan(x)^6, x)) /// }}} {{{id=49| A = random_matrix(QQ, 6, num_bound=9, den_bound=9) show(A) /// }}} {{{id=50| latex(A) /// }}}
3.3.2 LaTeX versions of graphs
{{{id=52| P = graphs.PetersenGraph() plot(P) /// }}} {{{id=53| P.set_latex_options(vertex_shape='circle', vertex_color='red', vertex_label_color='green', edge_color='blue') latex(P) /// }}}

3.3.3 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

{{{id=55| /// }}}

Can also embed images via the embedded word processor.

3.3.4 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\).

3.4 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.''

{{{id=57| 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.)

{{{id=59| M. /// }}}











4 Why is Open Important?











5 Examples

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

6 Links

This worksheet available at: http://buzzard.ups.edu/talks.html

{{{id=63| /// }}}