3670 days ago by SC2011


Introduction to SAGE

Neil Calkin, Holly Hirst, and Dan Warner

November, 2011



Mission Statement

Create a viable free open source alternative to Magma, Maple, Mathematica, and Matlab.

History of Sage

  • William Stein started Sage at Harvard in January 2005.

  • Sage-1.0 released February 2006 at Sage Days 1 (UC San Diego).

  • Nearly 30 Sage Days Workshops (!)  at UCLA, UW, Cambridge, Bristol, Austin, France, San Diego, Seattle, MSRI, ..., Barcelona, ...

  • Sage won first prize in the Trophees du Libre (November 2007)

  • Funding from Microsoft, Univ of Washington, UC San Diego, NSF, DoD, Google, Sun, private donations, etc.


  • Hundreds of other people subsequently got involved in Sage's development, and the scope of the project has widened to cover all mathematical computation. There is interest from all areas of mathematics, physics, engineering, etc.  (Developer mailing list has 1184 subscribers and about 50 messages/day... and sage-support has 1718 subscribers.)


What is Sage?

  • Sage = Python + Math
  • A unified self-contained distribution of open source free mathematical software.
  • Nearly a half million lines of new Python (and Cython) code that implements new capabilities and algorithms.
  • A "cloud" application like GMail or Google Docs:  (currently about 30,000 users);  Of course Sage also runs on your desktop and supercomputer.


Sage is about
building the car instead of reinventing the wheel

  1. Sage uses Python,  a mainstream programming language, instead of inventing a custom mathematics language
  2. Use straightforward method to link programs together -- C library and pseudotty's, instead of XML servers/OpenMath.  We implement all conversion routines, instead of expecting upstream to do it: we make them communicate with Sage, whether they want to or not.
  3. Give copious credit to contributors and be very developer friendly (easily build from source).
  4. Reuse, improve, and contribute to existing libraries and projects (e.g., Scipy, Numpy, R, ATLAS, CVXopt, GSL), instead of starting over and competing with them.
  5. Make the GUI using a web browser: the world of java and javascript is available and Sage integrates with the web.



Key take-away points: 

  • FOSS: 100% free open source software: good for clusters, sharing, research, collaboration
  • Python: very mainstream, scientific-computing friendly programming language
  • Cython: optimized Python to C/C++ compiler we develop with support for C/C++ datatypes
  • Interfaces: to control Matlab, Octave, Mathematica, etc., from Sage
  • Self contained: can have many copies of Sage at once, so no "dependency hell"
  • Peer reviewed: get your code refereed
  • Web based: notebook interface

All aboard!

We want to pause and make sure that everyone can access Sage.

  • Access the Sage Server at Shodor ( or at Clemson (
  • For further links and information go to SC2010 CE Talks.

And now a short demo...

# This is a simple introduction to Sage. # # The Sage notebook uses blocks of text, which are essentially Python statements that get executed. # Lines that start with the pound sign are comments and do not get echoed when the block is evaluated. # # A block is evaluated by clicking on the evaluate link or by typing shift-enter. 

# Sage does integer arithmetic with as many digits as needed.

Sage does Integer Arithmetic

(with as many digits as needed)

# Sage does arithmetic with the correct order of operations. 3+2*6^2 
# Sage can do more extensive calculations factorial(100) 

With a little thought you can answer the following questions without a calculator or computer or even pencil and paper.

How many trailing zeros should $100!$ have?  From the previous calculation it must be 24, but why?

How many trailing zeros should $200!$ have?  Hint, it is not 48.

# Sage knows about prime numbers and how to factor large integers print is_prime(12345678987654321) factor(12345678987654321) 
# and it can expand the previous result expand(_) 
# Sage knows about the Greatest Common Divisor print gcd(555,333666) # and the Least Common Multiple print lcm(555,333666) 
# We can double check these results by factoring print factor(555) print factor(333666) 

Sage does Rational Arithmetic

(with as many digits as needed)

# It can easily do the calculations involving rational numbers p = 1/555 print p q = 1/333666 print q p+q 
d = p*q print d print d*2*3^4*5*37^2 

Sage does Decimal Arithmetic

(with as many digits as you ask for)

q = sqrt(2) q 
# What is q? type(q) 
# Pressing tab will give auto-complete options, or function descriptions after a parens ( q. 
# or 1000 digits of the square root of pi sqrt(pi).n(digits=1000) 

Sage does Algebra

It can do similar calculations and plots with polynomials and rational functions.

# Sage can factor and expand algebraic expressions factor(x^5-1) 
p = expand(_) p 
# Algebraic expressions can be plotted P1 = plot( p , -0.5, 0.5, rgbcolor=(1,0,0) ) P2 = plot( p^3, -0.5, 0.5, rgbcolor=(0,1,0) ) show(P1+P2) 
# Sage handles rational functions with ease rf = x / (x^2 - 1) rf 
P3 = plot( rf , -0.5, 0.5, rgbcolor=(1,0,0) ) show(P3) 
# and partial fraction expansions pf = rf.partial_fraction(x) pf 
# Sage can display results in the more familiar mathematical format show(pf) 
# Sage uses LaTeX for the mathematical formatting latex(pf) 

Sage can solve algebraic equations.

# Sage can solve algebraic equations eq1 = x^2 - 2 == x show(eq1) solve([eq1],x) 
# Cubic polynomials are just as easy eq2 = x^3 + 2*x^2 - 5*x - 6 == 0 show(eq2) solve([eq2],x) 
# Sage can even find the ugly answers eq3 = x^3 - 2 == x show(eq3) solve([eq3],x) 

At this point we need to pause and look a little more carefully at how Sage works with Python.

First, Sage makes extensive use of classes

# Pressing tab will give auto-complete options, or function descriptions after a parens ( # A '?' after a command gives detailed information, e.g. 'plot?' tells you about plot # Sage can do simple calculations 
# What is pi? type(pi) 

# How do we tell Sage to compute pi to 4493 digits if we don't remember the command? # The tab key after "." of "(" generates a help display which can be used to # autocomplete the statement. pi.n(4493) 
# Sage can solve equations for one variable in terms of others. # var('x b c') show(solve([x^2 + b*x + c == 0],x)) 


This example illustrated the fact that Python variables by default are "programming variables" and must be initialized. 

To create a symbolic variable we must declare the variable with the var command. 

The variable x is a special case for convenience.

# Sage can solve equations for one variable in terms of others. var('x b c') show(solve([x^2 + b*x + c == 0],x)) 
# Sage can even find the ugly answers eq3 = x^3 - 2 == x show(eq3) sol = solve([eq3],x) for z in sol: show(z) 

This example illustrated the for loop in Python. 

First, the for statement which walks through a list of objects. It ends with the colon. 

Second, the block inside the loop is indented.  There are no begin ... end constructs in Python. 

Good programming style strongly suggests that you indent subordinate blocks of code. 

Python insists that you indent and rewards you with shorter programs.

# Sage can solve linear systems of algebraic equations # Note the introduction of the symbolic variable y var('y') eq4 = x-1 == y+1 eq5 = x+1 == 2*(y-1) eq4;eq5 

This example underscored the fact that the single "=" is assignment and the double "==" is equality.

It also showed that multiple statements can be on the same line when separated by ";"

sol2 = solve([eq4,eq5],x,y) sol2 

In this example, the solution is returned in a list.  Lists are the workhorse data structure in Python.

# Sage can also return the solutions in a dictionary # This provides access by name sol2 = solve([eq4,eq5],x,y,solution_dict=True) sd = sol2[0] print sd (sd[x].n(digits=2),sd[y].n(digits=3)) 

In this example we used the dictionary, the most sophisticated built-in data structure in Python.


Sage's graphics capabilities can provide helpful illustrations.

A simple nonlinear example consisting of a straight  line intersecting a circle.

eq6 = (x-1)^2 + (y-1)^2 == 4; show(eq6) eq7 = 4*y - 3*x == -1; show(eq7) 
sol3 = solve([eq6,eq7],x,y,solution_dict=True) show(sol3) [(s[x].n(digits=5),s[y].n(digits=5)) for s in sol3] 

We can easily create a nice illustration of this problem and its solution.

x0 = sol3[0][x] y0 = sol3[0][y] s0 = '(%5.2f,%5.2f)' % (x0,y0) p0 = (x0-0.47,y0) x1 = sol3[1][x] y1 = sol3[1][y] s1 = '(%5.2f,%5.2f)' % (x1,y1) p1 = (x1-0.47,y1) g = Graphics() g += circle((1,1),2) g += line([(-1,-1),(3,2)]) g += points([(s[x],s[y]) for s in sol3],rgbcolor=(1,0,0)) g += text(s0,p0) g += text(s1,p1) show(g,aspect_ratio=1) 

The following example by Jason Grout uses Sage to solve a system of three non-linear equations with 4 unknowns, where one of the unknowns, $u$, is treated as a parameter and set to 1.

# First, we solve the system symbolically: var('x y u v') eq8 = u+v==9; show(eq8) eq9 = v*y+u*x==-6; show(eq9) eq10 = v*y^2+u*x^2==24; show(eq10) show(solve([eq8,eq9,eq10,u==1],u,v,x,y)) 
# Second, we can solve it numerically solns = solve([eq8,eq9,eq10,u==1],u,v,x,y, solution_dict=True) [(s[u].n(digits=10), s[v].n(digits=10), s[x].n(digits=10), s[y].n(digits=10)) for s in solns] 

Sage can do Calculus

p = (x^5-1) diff(p^3,x) 

A slightly more complicated formula involving trigonometric functions with parameters.

var('a b c') q = (x-a)*(cos(b*x)+sin(c*x)) q 

Differentiating this expression illustrates that Sage knows the product rule, the chain rule, and the derivatives of sin and cos.


Integrating this expression illustrates that Sage knows integration by parts.


Sage can define and manipulate functions.

f(x) = sin(x)^2*cos(x)*exp(x) show(f) 
plot(f, 0, 3) 
x=4 print f(x) plot(f,0,3) 
# Surface plot example var('x,y') plot3d(cos(x^2 + y^2), (x, -2, 2), (y, -2, 2))