4213 days ago by hirsthp

Supercomputing 2010

Computational Mathematics for Teachers

Neil Calkin, Holly Hirst, Dan Warner

Goals for this session:

  • to experiment with some "canned" tools available on the internet
  • to experience a more flexible environment for mathematics: Sage


PART I: Motivation

For the last 20 years, NCTM has worked to present a roadmap for teaching preK-12 mathematics. In the 2000 publication, Principles and Standards for School Mathematics, NCTM advocated the use of technology to explore mathematical ideas through its Technology Principle: Technology is essential in teaching and learning mathematics; it influences the mathematics that is taught and enhances students' learning.

The latest development in guidelines for school mathematics is the approval in June 2010 of  The Common Core Standards.

From the website: “The Common Core State Standards Initiative is a state-led effort, launched more than a year ago by state leaders, including governors and state commissioners of education. . .through their membership in the National Governors Association Center for Best Practices (NGA Center) and Council of Chief State School Officers (CCSSO). . . .

NCTM is working on a report to help teachers understand how the Common Core State Standards for Mathematics fit with NCTM’s Principles and Standards for School Mathematics, Curriculum Focal Points, and Focus in High School Mathematics. This new report, Making it Happen: A Guide to Interpreting and Implementing the Common Core State Standards for Mathematics, is expected to be published within four to six weeks.

In the Common Core Standards, there are six areas addressed for high school mathematics, and several have computational aspects:

Functions: Interpret functions that arise in applications in terms of the context.

4. For a function that models a relationship between two quantities, interpret key features of graphs and tables in terms of the quantities, and sketch graphs showing key features given a verbal description of the relationship. Key features include: intercepts; intervals where the function is increasing, decreasing, positive, or negative; relative maximums and minimums; symmetries; end behavior; and periodicity.

Functions: Analyze functions using different representations.

7. Graph functions expressed symbolically and show key features of the graph, by hand in simple cases and using technology for more complicated cases.

Geometry: Make Geometric Constructions

12. Make formal geometric constructions with a variety of tools and methods (compass and straightedge, string, reflective devices, paper folding, dynamic geometric software, etc.).

Statistics and Probability: Interpret linear models

8. Compute (using technology) and interpret the correlation coefficient of a linear fit.

Modeling: Modeling is the process of choosing and using appropriate mathematics and statistics to analyze empirical situations, to understand them better, and to improve decisions. . . .When making mathematical models, technology is valuable for varying assumptions, exploring consequences, and comparing predictions with data.

All aboard!

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

  1. Access the Sage Server at  
  2. Create an account; this account will be available for the next several months so you can access it even after SC2010.

PART II: Some Canned Examples

1. Function and Data Flyer

2. Regression Tool

3. Derivative Explorer





Introduction to Sage


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).
  • 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?

  • A unified self-contained distribution of open source free mathematical software.
  • 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 methods to link programs together.
  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 interface 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
  • Peer reviewed: get your code refereed
  • Web based: notebook interface



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 
# There are the usual built-in functions. Before you do the factorial calculation, try to figure out how many trailing zeros the answer should have. Notice the use of the word print. Take it out, re-evaluate, and watch what happens. What is Sage doing? print sqrt(100) print sqrt(3) factorial(100) 
# Sage knows about prime numbers and how to factor large integers print is_prime(12345678987654321) factor(12345678987654321) 
# and it can expand the previous result (denoted by the underscore "_" expand(_) 
# Sage knows about the Greatest Common Divisor print gcd(555,333666) # and the Least Common Multiple lcm(555,333666) 
# We can double check these results by factoring print factor(555) print factor(333666) 

Sage does Rational Arithmetic

# 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. # Notice that "n" is in the list when you tab after "q." and it allows us to ask for specific precision... 
print q.n(1000) print q.n(digits=1000) 
# Can you avoid this "n" notation? sure... sqrt(2.0) 


Sage does Algebra

# 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, and so it can print out the latex for you... latex(pf) 
# 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) 

Many commands are what you might expect...

html('<h2>Tangent line grapher</h2>') @interact def tangent_line(f = input_box(default=sin(x)), xbegin = slider(0,10,1/10,0), xend = slider(0,10,1/10,10), x0 = slider(0, 1, 1/100, 1/2)): prange = [xbegin, xend] x0i = xbegin + x0*(xend-xbegin) var('x') df = diff(f) tanf = f(x0i) + df(x0i)*(x-x0i) fplot = plot(f, prange[0], prange[1]) print 'Tangent line is y = ' + tanf._repr_() tanplot = plot(tanf, prange[0], prange[1], rgbcolor = (1,0,0)) fmax = f.find_maximum_on_interval(prange[0], prange[1])[0] fmin = f.find_minimum_on_interval(prange[0], prange[1])[0] show(fplot + tanplot, xmin = prange[0], xmax = prange[1], ymax = fmax, ymin = fmin) 

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

Interactivity in Sage

@interact is a facility within Sage to allow you to create an input area -- either a slider for a range of numbers or a text box for entering text such as a math expression.  There are a number on the website to copy and paste into a Sage notebook and experiment with.

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 "." or "(" generates a help display which can be used to # autocomplete the statement. Try it... pi. solve( 
# 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 even find the ugly answers eq3 = x^3 - 2 == x show(eq3) sol1 = solve([eq3],x) for k in range(3): show(sol1[k]) 

This example illustrated the for loop in Python. 

First, the for statement walking through a list of objects and 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 principal data structure workhorse.

# 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, which is the most sophisticated built-in data structure in Python.