Download HPC Python Tutorial: Introduction to SAGE Interactive Shell 4/23/2012

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
HPC Python Tutorial:
Introduction to SAGE Interactive Shell
4/23/2012
Instructor:
Yaakoub El Khamra, Research Associate, TACC
[email protected]
What is sage
• Sage is a free open-source mathematics
software system licensed under the GPL.
It combines the power of many existing opensource packages into a common Python-based
interface.
• Mission: Creating a viable free open source
alternative to Magma, Maple, Mathematica
and Matlab.
2
Sage Features
• Sage is built out of nearly 100 open-source packages with a unified
interface
• Sage can be used to study elementary and advanced, pure and applied
mathematics:
–
–
–
–
–
–
–
–
–
–
–
basic algebra
Calculus
elementary to very advanced number theory
Cryptography
numerical computation
commutative algebra
group theory
Combinatorics
graph theory
exact linear algebra
Much much more….
• Extensive documentation!
3
Sage and Python
• The Sage notebook interface is iPython with a
twist
• If you know Python, you pretty much know sage
already
• You do not need to know Python, but it cannot
hurt to learn it (very simple and easy syntax)
• Many tutorials available for learning Python
online:
– http://docs.python.org/tutorial/
– http://wiki.python.org/moin/BeginnersGuide/NonPro
grammers
4
How to use Sage
• Notebook graphical interface (what we will be
using)
• Interactive command line (directly from the
shell)
• Programs: By writing interpreted and
compiled programs in Sage
• Scripts: by writing stand-alone Python scripts
that use the sage library (write your own code
that import sage functionality)
5
Sage interface
• The user interface is a notebook:
– in a web browser
– command line
– Using the notebook, Sage connects either locally to your
own Sage installation or to a Sage server on the network
• Inside the Sage notebook you can
– create embedded graphics
– Math expressures
– add and delete input and share your work
• This is your Mathematica workbook… on steroids…. For
free
6
Starting Sage
●
●
When you start sage, usually you will get something like this:
Obviously you will get your own prompt on your own machine the version
of sage you are running
Quitting Sage, History
●
●
●
●
The “quit/exit” function or Ctrl-D will terminate
the sage instance
To launch the web interface in sage, call the
“notebook()” function
In the shell interface (aka the iPython interface)
you can get a list of all input lines typed so far
with “%hist”
You can also get help on ipython by typing “?” at
the sage shell
Sage Session Information
●
●
The following GLOBAL variables always exist (so don’t overwrite
them!):
Example:
Macros
●
You can combine “%hist” with “%macro” to
create shortcuts to scripts from previous
commands
Shell Commands
●
To execute *NIX shell commands from the
sage interactive shell (not the notebook()
interface) use the “!” operator
Logging Input & Output
●
●
●
●
You can use “logstart <log name>” to start
logging a session
You can use this command to log all input you
type, all output, and even play back that input in
a future session (by simply reloading the log file)
To reload the log file: load “<log file name>”
This is different from saving complete sessions
obviously
Saving & Loading Complete Sessions
●
The command “save_session(sessionname)” saves all the variables
defined in the current session as a dictionary with the name:
“sessionname”
●
This creates a .sobj file that can be loaded
●
E.g define some variables
●
Save the session
Saving & Loading (cont)
●
●
Now you can restart Sage and load the saved
session:
Whenever possible, I prefer to use the
notebook() interface since it saves worksheets
that you can “share” with collaborators
Saving & Loading Objects
●
Almost all Sage objects x can be saved in
compressed form to disk using
“save(x,filename)” (or in many cases
“x.save(filename)”)
To load the object back in, use “load(filename)”
●
This is pickling (Python's cPickle module)
●
Timing Commands
●
●
●
Since this is HPC, we will need to have some profiling
The “%time” command at the beginning of an input line, the time the
command takes to run will be displayed after its output
Different interfaces have different performance
●
Python integers
●
Sage Native (Cython) integers
●
PARI C-library integers
Loading and Attaching Files
●
You can create sage scripts (ASCII files containing sage statements)
that can be loaded using the “load” command:
load “filename.sage”
this will read and execute the filename.sage file
●
You can also attach files to a running session with the “attach”
command:
attach “filename.sage”
this will read and execute the filename.sage file, and will reload
the file if its contents are changed (and you hit
enter/shift+enter in sage)
●
The attach command is great for debugging: open your scripts in
your favorite editor and have sage continuously evaluate them
Timing Commands (cont)
●
●
You can also use the “cputime()” function
Use “walltime()” instead of “cputime()” for
more meaningful output
Profiling
●
●
Because at some point, you just have to do it
(just not prematurely)
Use the “prun” command in the interactive
shell
●
Example:
●
Now:
Profiling (cont)
Getting Help
●
You have seen this before:
<funkyFunction>? Will return the help file for
funkyFunction
<funkyFunction>?? will return the source code
●
You can also use help(funkyFunction)
Interfaces
●
●
●
A central facet of Sage is that it supports
computation with objects in many different
computer algebra systems “under one roof”
using a common interface and clean
programming language
You can access these packages from sage,
even communicate variables back and forth
between sage and individual packages
Sage does code integration!
Sage Standard Packages
Atlas, blas, boehm, boost-cropped, cddlib, cephes, cliquer,
conway_polynomials, cvxopt, cython, docutils, ecl, eclib, ecm,
elliptic_curves, examples, extcode, f2c, flint, flintqs, fortran,
freetype, gap, gd, gdmodule, genus2reduction, gfan, givaro, glpk,
gnutls, graphs, gsl, iconv, iml, ipython, jinja, lapack, lcalc, libfplll,
libgcrypt, libgpg_error, libm4ri, libpng, linbox, matplotlib, maxima,
mercurial, moin, mpfi, mpfr, mpir, mpmath, networkx, ntl, numpy,
opencdk, palp, pari, patch, pexpect, pil, polybori, polytopes_db,
pycrypto, pygments, pynac, python, python_gnutls, R, ratpoints,
readline, rubiks, scipy, scipy_sandbox, scons, singular, sphinx,
sqlalchemy, sqlite, symmetrica, sympow, sympy, tachyon, termcap,
twisted, weave, zlib, zn_poly, zodb
plenty more optional packages
GP/PARI
●
PARI is a compact, very mature, highly
optimized C program whose primary focus is
number theory. There are two very distinct
interfaces that you can use in Sage:
●
gp: the G oP ARI interpreter
●
pari: the PARI C library
GP/PARI (cont)
●
●
In the first case, a separate copy of the GP interpreter is started as a
server, and the string 'znprimroot(10007)' is sent to it, evaluated by GP,
and the result is assigned to a variable in GP (which takes up space in the
child GP processes memory that won’t be freed). Then the value of that
variable is displayed
In the second case, no separate program is started, and the string
'znprimroot(10007)' is evaluated by a certain PARI C library function. The
result is stored in a piece of memory on the Python heap, which is freed
when the variable is no longer referenced. The objects have different
types
GP/PARI (cont)
Which one to use?
The GP interface can do absolutely anything you could do in the
usual GP/PARI command line program, since it is running that
program. In particular, you can load complicated PARI
programs and run them. In contrast, the PARI interface (via
the C library) is much more restrictive. First, not all member
functions have been implemented. Second, a lot of code,
e.g., involving numerical integration, won’t work via the PARI
interface. That said, the PARI interface can be significantly
faster and more robust than the GP one
Maxima
●
●
●
Maxima is included with Sage, as well as a Lisp
implementation
Maxima does symbolic manipulation. Maxima can integrate
and differentiate functions symbolically, solve 1st order
ODEs, most linear 2nd order ODEs, and has implemented the
Laplace transform method for linear ODEs of any degree
Maxima also knows about a wide range of special functions,
has plotting capabilities via gnuplot, and has methods to
solve and manipulate matrices (such as row reduction,
eigenvalues and eigenvectors), and polynomial equations
Maxima (cont)
How about C code?
●
Yes, you can access C functions in sage (sage
will compile the code for you). Example:
●
●
●
The C code: (test.c)
The Cython code: (test.spyx) (be careful about
the path)
And attach:
Profile your C code:
Sage & Latex
●
Yes, it does Latex too
●
You can even render latex with jsMath
Sage & Latex
●
●
Do not worry about the <html> stuff, that will be
rendered in the notebook() interface
You can also customize the latex generation and
the latex processing
Sage & R
●
R is a free software environment for statistical computing and graphics
●
R is free and included with each copy of sage
●
You can install R packages from within sage:
r.eval("install.packages(c('deSolve'))")
●
You can pass data back and forth between Sage and R
●
Try the following commands:
x=r([2,2,3,5,6,6,6,9])
r.mean(x)
x.var()
r.summary(x)
Sage & R (cont)
●
●
EnKF.R is an R script that does EnKF calculations
(it uses a Lorentz model solved by deSolve that
we installed earlier)
Obviously more elegant Sage-R interfaces are
possible (coupling dependent)
NumPy: Arrays
●
●
●
●
If you want numerics, you want NumPy
NumPy is not imported into sage initially, you have to do the
import the old fashioned way
Since NumPy's strength is arrays, array creation is almost
unchanged
You can access elements of arrays like any list or take take
slices
NumPy:Arrays (cont)
●
You can do basic arithmetic operations
●
You can get elementwise and dot products
●
2D arrays
NumPy
●
●
The difference is that with numpy.array(), m is treated as just an
array of data. In particular m*m will multiply componentwise,
however with numpy.matrix(), m*m will do matrix multiplication.
We can also do matrix vector multiplication, and matrix addition
NumPy
●
●
You can also create an array and manipulate
the shape attribute
You can also slice arrays
NumPy
●
●
Sliced arrays are references to original:
changes to sliced array reflect on the original
Otherwise: use a copy:
NumPy
●
Useful function: arange (do numpy.arange? )
Octave
●
●
Octave is an open source MATLAB clone with
numerical routines for integrating, solving
systems of equations, special functions, and
solving (numerically) differential equations
Sage has an interface to Octave:
Octave (cont)
Many useful functions:
Octave (cont)
For example:
Octave (cont)
More examples
Mathematica & MATLAB
●
●
●
There are interfaces
We will not cover them (not installed on our
systems…. yet)
More information available here:
http://www.sagemath.org/doc/reference/sage/interfaces/mathematica.html
http://www.sagemath.org/doc/reference/sage/interfaces/matlab.html