30
Introduction to Sage Introduction to Sage L. Felipe Martins October 22, 2010

Introduction to Sage - Cleveland State University · Introduction to Sage What is Sage What is Sage Sage is a freely available, open source computer algebra system. Created by William

  • Upload
    others

  • View
    32

  • Download
    1

Embed Size (px)

Citation preview

Introduction to Sage

Introduction to Sage

L. Felipe Martins

October 22, 2010

Introduction to Sage

Outline

1 What is Sage

2 Using Sage

3 Examples

4 SageTEX

5 Where and how is Sage being used

Introduction to Sage

What is Sage

What is Sage

Sage is a freely available, open source computer algebrasystem.

Created by William Stein, starting in 2005.

“Create a viable free open source alternative to Magma,Maple, Mathematica and MATLAB”.

Funded by Microsoft, University of Washington, NSF, DoD,Google, Sun, private donations. . .

Huge number of contributors:

Developers.Users: try the software, find bugs, make suggestions, requestfeatures.Changes to the software and proposals for new features can bemade and discussed by anyone.

Introduction to Sage

What is Sage

What is in Sage

“Build the car, not reinvent the wheel.”

Symbolic computation, calculus Maxima, sympy

Basic Arithmetic GMP, NTL, MPFR, PARI

Algebraic geometry Singular (libcf, libfactory)

Graphics Matplotlib, Tachyon, GD, jmol

Group theory and combinatorics GAP

Graph theory Networkx

Number Theory Pari

Numerical computation GSL, Numpy, SciPy

Statistics R

“More than 5 million of lines of code, developed by the bestmathematical minds in each field.”

Introduction to Sage

What is Sage

Computer Language

Based on Python, a stable and widely used programminglanguage.

Python is very expressive and powerful:

[p for p in prime_range(1000) if p % 4 == 1]

Extends and modifies Python to make it look moremathematical:

2^100 # 2**100 in Python (^ is xor)A[0,3] # Matrix indices. A[0][3] in Python.

Supports all Python features: dictionaries, functions, classes,etc.

If needed, can use cython to make scripts run faster.

Introduction to Sage

Using Sage

Installation

Available from http://sagemath.org

Binaries available for Linux, OS X.

Also for Windows, but doesn’t run natively. It actually runs avirtual Linux machine inside Windows.

Sage LiveCD: runs from a bootable CD.

Don’t install anything: notebook interface, runs on webbrowser (Firefox/Safari)Public server: http://sagenb.org (more than 3000 users).

Introduction to Sage

Using Sage

What is a Sage server

A Sage installation that is accessible through the internet.

Security issues: must be run inside a “virtual machine”.

Set up requires some knowledge of Linux.

May need “help” from the network people at your institution.

Installation guides are available:http://wiki.sagemath.org/DanDrake/JustEnoughSageServerhttp://wiki.sagemath.org/SageServer

Public server can be used for courses, but it tends to be slow.

Introduction to Sage

Using Sage

How to learn Sage

Sage documentation page: go to httl://sagemath.org,click Documentation link.

The Sage Tutorial:http://sagemath.org/doc/tutorial/index.html

Sage for newbies: http://sage.math.washington.edu/home/tkosan/newbies_book/

Sage wiki: http://wiki.sagemath.org/

Google groups:http://groups.google.com/group/sage-support. Alsosage-edu, sage-announce, sage-devel.

Introduction to Sage

Using Sage

The notebook interface

Introduction to Sage

Using Sage

Notebook features

Execute Sage commands, interfaces to other packages.

Mix Sage commands and formatted text, including TEXcommands.

Work periodically saved in server.

Download, upload, print worksheets.

Save, load data files.

Publish worksheets to the web.

Introduction to Sage

Examples

Sage prefers exact answers to approximations

pi

π

pi.n()

3.14159265358979

pi.n(digits=50)

3.1415926535897932384626433832795028841971693993751

euler_gamma

γE

euler_gamma.n(digits=50)

0.57721566490153286060651209008240243104215933593992

Introduction to Sage

Examples

Calculus

f(x)=sqrt(25-x^2)/xf(x)√−x2+25

x

diff(f(x),x)

− 1√−x2+25

−√−x2+25

x2

integral(f(x),x)√−x2 + 25− 5 log

(10√−x2+25abs(x) + 50 1

abs(x)

)integral(e^(-x^2),(x,-infinity,infinity))√π

Introduction to Sage

Examples

Plots

plot(sin(x),(x,-2*pi,2*pi))

-6 -4 -2 2 4 6

-1

-0.5

0.5

1

Introduction to Sage

Examples

More complex graphs

var(’x,y’)xmin,xmax,ymin,ymax=-3,3,-3,3G = implicit_plot(x^3+y^3-9/2*x*y,

(x,xmin,xmax),(y,ymin,ymax))G += line([(xmin,0),(xmax,0)], color=’gray’)G += line([(0,ymin),(0,ymax)], color=’gray’)G += line([(1,0),(1,2)])G += line([(0,2),(1,2)])G += line([[xmin,(4*xmin+6)/5],[2.3,(4*2.3+6)/5]],

color=’red’)G.show(aspect_ratio=1)

Introduction to Sage

Examples

More complex graphs

Introduction to Sage

Examples

3D plots

u, v = var(’u,v’)fx = (3+sin(v)+cos(u))*cos(2*v)fy = (3+sin(v)+cos(u))*sin(2*v)fz = sin(u)+2*cos(v)parametric_plot3d([fx, fy, fz],

(u, 0, 2*pi), (v, 0, 2*pi),frame=False, color="red")

Introduction to Sage

Examples

3D plots

Introduction to Sage

Examples

Tachyon ray tracer

t = Tachyon(xres=512,yres=512,camera_center=(5,0,0))

t.light((4,3,2), 0.2, (1,1,1))t.texture(’t0’, ambient=0.1, diffuse=0.9,

specular=0.5, opacity=1.0, color=(1.0,0,0))t.texture(’t1’, ambient=0.1, diffuse=0.9,

specular=0.3, opacity=1.0, color=(0,1.0,0))t.texture(’t2’, ambient=0.2, diffuse=0.7,

specular=0.5, opacity=0.7, color=(0,0,1.0))k=0for i in srange(-5,1.5,0.1):

k += 1t.sphere((i,i^2-0.5,i^3), 0.1, ’t%s’%(k%3))

t.show()

Introduction to Sage

Examples

Tachyon ray tracer

Introduction to Sage

Examples

Interact cells

Creates a small GUI inside a Sage worksheet that allows userinteraction.

Introduction to Sage

Examples

Arithmetic in Z/mZ

R = IntegerModRing(45)R

Z/45Z

n = randrange(10^600)R(43)^n

43

R(14)^(-1)

29

try:n = R(18)^(-1)

except:print ’Error: cannot invert modulo 45’

Error: cannot invert modulo 45

Introduction to Sage

Examples

More arithmetic in Z/mZ

Suppose we want to solve the equation:

18x = 27 in Z/45Z

18 is not invertible in Z/45Z. However:

g = gcd(18,45)print gprint 27%g

9

0Since gcd(18, 45) divides 27, the equation has solutions.Furthermore, the number of solutions is given by gcd(18, 45) = 9.How can we find these solutions?

Introduction to Sage

Examples

More arithmetic in Z/mZ (continued)

The solution to solve the equation in the ring of integers modulod = 45/9:

d = 45//gprint dS = IntegerModRing(d)

5The equation, when mapped to the ring S , has a unique solution:

solutionS = S(18)^(-1)*S(27)solutionS

4Now, lift this solution to R and find the other solutions:

solutionR = R(lift(solutionS))[solutionR + d*k for k in range(0,9)]

[4, 9, 14, 19, 24, 29, 34, 39, 44]

Introduction to Sage

SageTEX

What is SageTEX

Developed by Dan Drake(http://mathsci.kaist.ac.kr/~drake/index.html)

A package that lets Sage be called from LATEX documents.

Defines new environments and commands for evaluation ofSage code:

sageblock: Environment that executes and displays a blockof Sage commands.sagesilent: Environment that executes a block of Sagecommands without displaying them.\sage: Command that evaluates a single Sage expression anddisplays the result.

SageTEX was used to write this presentation.

Introduction to Sage

SageTEX

SageTEX Example – Code

Code:

\begin{sagesilent}var(’x’)a,b,c = 2,7,-6eq = 2*x^2+b*x+cdiscr = b^2-4*a*cx1 = (-b+sqrt(discr))/(2*a)x2 = (-b+sqrt(discr))/(2*a)\end{sagesilent}To solve the equation $\sage{eq}$, we first compute thediscriminant$D=\sage{b}^2-4(\sage{a})(\sage{c})=\sage{discr}$....

Introduction to Sage

SageTEX

SageTEX Example – Output

To solve the equation 2 x2 + 7 x − 6, we first compute thediscriminant D = 72 − 4(2)(−6) = 97. Since the discriminant ispositive, the equation has two real roots:

x1 = −7+√

9722 = 1

4

√97− 7

4 and x2 = −7−√

9722 = 1

4

√97− 7

4

Introduction to Sage

SageTEX

Automatic test generation

CSU now has all sections of calculus coordinated.

Problem: efficiently generate tests for all sections.

Solution: Use LATEX packages SageTEX and eqexam.

Wrote a driver script in Python to automate the process.

Generate random seed.Generate .tex files.Calls pdflatex.Calls sage.Calls pdflatex again.Do the same to generate solutions file.Repeat.

Still in early development. Has only one user.

Introduction to Sage

SageTEX

Example

Introduction to Sage

Where and how is Sage being used

Teaching and Research using Sage

Teaching:http://wiki.sagemath.org/Teaching_with_SAGECalculus, linear algebra, abstract algebra, differentialequations, statistics, number theory, graph theory, fluidmechanics, discrete mathematics, cryptography.

Research:http://sagemath.org/library-publications.htmlAlgebraic number theory, combinatorics, coding theory,cryptography. (57 articles, 18 theses, 13 books, 40 preprints)

Introduction to Sage

Where and how is Sage being used

That’s it

Slides available at:http://academic.csuohio.edu/fmartins/

Thank you