Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf ·...

26
Welcome to MCS 507 1 About the Course content and organization expectations of the course 2 Python and Sage Python is a powerful scripting language Sage: a free open source mathematical software system 3 The PSLQ Integer Relation Algorithm calling pslq in sympy a hexadecimal expansion for π MCS 507 Lecture 1 Mathematical, Statistical and Scientific Software Jan Verschelde, 27 August 2012 Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 1 / 26

Transcript of Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf ·...

Page 1: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

MCS 507 Lecture 1Mathematical, Statistical and Scientific Software

Jan Verschelde, 27 August 2012

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 1 / 26

Page 2: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 2 / 26

Page 3: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Catalog Description

The design, analysis, and use of mathematical, statistical, andscientific software.

Prerequisite(s): the catalog lists “Grade of B or better in MCS 360 orthe equivalent or consent of instructor.”Examples of courses which could serve as “the equivalent”are MCS 320 (introduction to symbolic computation)and MCS 471 (numerical analysis).

MCS 507 fits in an interdisciplinary computational science andengineering (CSE) curriculum.We plan to add MCS 507 to the Computational Science Prelim.

MCS 507 prepares for MCS 572 (introduction to supercomputing).

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 3 / 26

Page 4: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Content of the Coursetext book and core software

An introduction to computational science with Python is inA Primer on Scientific Programming with Python. Second Edition,Springer-Verlag, 2011, by Hans Petter Langtangen.

http://vefur.simula.no/intro-programming/

The textbook is optional, not required for the course.

Solving problems in industry is a mix of mathematical modeling andcomputational experiments.

We have no time to develop our own software, but rely on

Sage: bundles many useful software systems;

Python: for rapid prototyping and modules as numpy.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 4 / 26

Page 5: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Software Requirements

Required software:1 Python (version 2.7)2 NumPy3 Matplotlib4 SciTools

Recommended software:1 IPython2 Gnuplot plotting program3 Gnuplot.py Python module for Gnuplot4 ScientificPython5 SciPy

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 5 / 26

Page 6: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 6 / 26

Page 7: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Organization and Expectations

Three parts of the course:

scientific scripting with Python;

introduction to Sage and some of its components:Maxima, GAP, PARI/GP, R, Singular;

software for your own research interests.

Activities throughout the semester:

several homework collections;

midterm exam could be take home;

three computer projects.

The first two computer projects will be prescribed by meand may be solve in pairs. The third project must be done individuallyand could form the basis for a project presentation at the end, ifeveryone opts out of a final exam.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 7 / 26

Page 8: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 8 / 26

Page 9: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Python

Python is a powerful scripting language.

Why Python? At least five reasons:

1 suitable as first programming language2 widespread use and good for rapid prototyping3 extendable via module interfaces to other software4 wraps mathematical software: numpy, scipy, sympy5 available as free and open source

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 9 / 26

Page 10: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

running Python

Two main ways to run code in Python:

1 in the interactive shell, launch at command prompt $:$ pythonPython 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more>>>

Use this interactive mode for short calculations,or to try out code snippets.

2 store code in file.py and then type at prompt $:

$ python file.py

It is good practice to build the code incrementally.IDLE is the Integrated Development Environment (IDE) for Python.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 10 / 26

Page 11: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

running Python like Octave

$ ipython --pylabYour PyGtk has set_interactive(), so you can use themore stable single-threaded Gtk mode.See https://bugs.launchpad.net/ipython/+bug/270856Python 2.6.6 (r266:84292, May 1 2012, 13:52:17)Type "copyright", "credits" or "license" for more information.

IPython 0.10 -- An enhanced Interactive Python.? -> Introduction and overview of IPython’s features.%quickref -> Quick reference.help -> Python’s own help system.object? -> Details about ’object’. ?object also works, ??

Welcome to pylab, a matplotlib-based Python environment.For more information, type ’help(pylab)’.

In [1]:

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 11 / 26

Page 12: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

session continued

In [1]: piOut[1]: 3.1415926535897931

In [2]: x = arange(-pi,pi,0.01)

In [3]: y = sin(x)

In [4]: plot(x,y)

A new window pops up containing

�4 �3 �2 �1 0 1 2 3 4�1.0

�0.5

0.0

0.5

1.0

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 12 / 26

Page 13: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 13 / 26

Page 14: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Sage

Sage aims to be a viable free and open source alternativeto the big M’s (Magma, Mathematica, MATLAB, Maple).

Why Sage? At least four reasons:

1 bundles various leading software systemsused for mathematical research and experimentation

2 Python as scripting language3 active and large community of developers4 Try it online! at www.sagemath.org

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 14 / 26

Page 15: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

running Sage

Two main modes of running Sage:

1 In a terminal window, which is similar as using the command lineinterface to interactive software systems.

This works fine for short calculations,when the right commands are knows in advance.

2 The notebook interface runs in a web browser.The help gives direct access to the reference manual and otherdocumentation.

The notebook interface is natural with cloud computing.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 15 / 26

Page 16: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

a terminal session with Sage

--------------------------------------------------------------| Sage Version 5.2, Release Date: 2012-07-25| Type "notebook()" for the browser-based notebook interface.| Type "help()" for help.--------------------------------------------------------------sage: pipisage: sin(pi)0sage: ’%.14e’ % pi’3.14159265358979e+00’sage: type(_)<type ’str’>sage: type(pi)<type ’sage.symbolic.expression.Expression’>sage: p = plot(sin,(-pi,+pi))sage: p

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 16 / 26

Page 17: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Sage Cell Server

The web page www.sagemath.org/eval.html containsan interactive Sage widget and a collection of examples.

To make a plot of sin(x) for x ∈ [−π,+π], enter:

var(’x’)plot(sin(x), (-pi, pi)).show()

in the box above the Evaluate button.

Clicking on this Evaluate button displays a plot of the sine functionover the requested interval.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 17 / 26

Page 18: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 18 / 26

Page 19: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

the PSLQ algorithm

Let x be a vector of n real numbers, x = (x1, x2, . . . , xn).An integer relation algorithm finds n integers a = (a1, a2, . . . , an):

a1x1 + a2x2 + · · · + anxn = 0.

The Euclidean algorithm solves this problem for n = 2.

The PSLQ algorithm by David Bailey and Helaman Ferguson is thebest-known integer relation algorithm. PSLQ was named one of ten“algorithms of the century” by Computing in Science and Engineering.

The algorithm requires high-precision arithmetic software: at leastd × n digits, where d is the size (in digits) of the largest integer ak .

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 19 / 26

Page 20: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

deciding whether a number is algebraic

One application of PSLQ is to find polynomials with integer coefficientsthat vanish at approximate roots.

If α is an algebraic number (the root of a polynomial, e.g.: α =3√

4),then RealField(53)(α) (in Sage) returns an approximation for thecorresponding root.

The output of PSLQ establishes the opposite direction: with thepolynomial with integer coefficients that vanishes at an approximateroot, we can declare a corresponding algebraic number.

To find polynomials which vanish at an approximate root r we give asequence of consecutive powers of r as input to PSLQ.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 20 / 26

Page 21: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

calling pslq in sympy

Starting from a floating point approximation for α =3√

4, computing thepolynomial that vanishes at this approximation shows its algebraicnumber representation.

>>> from sympy.mpmath import pslq>>> r = 4.0**(1/3.0); r1.5874010519681994>>> pslq([r**n for n in xrange(5)])[4, 0, 0, -1, 0]

The calculations above simulated the discovery of1.5874010519681994 as 3

√4, as a root of 4 − x3 = 0.

The commands above are identical in Sage.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 21 / 26

Page 22: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Welcome to MCS 507

1 About the Coursecontent and organizationexpectations of the course

2 Python and SagePython is a powerful scripting languageSage: a free open source mathematical software system

3 The PSLQ Integer Relation Algorithmcalling pslq in sympya hexadecimal expansion for π

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 22 / 26

Page 23: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

a hexadecimal expansion for π

One of the remarkable discoveries made by PSLQ is a simple formulathat allows to calculating any binary digit of π without calculating thedigits preceding it:

π =∞

i=0

116i

(

48i + 1

−2

8i + 4−

18i + 5

−1

8i + 6

)

.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 23 / 26

Page 24: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

a Python script using sympy

from sympy.mpmath import pslqS = [sum([1.0/(16**k*(8*k+j)) \

for k in xrange(8)]) \for j in xrange(1,8)]

print Simport mathS.append(math.pi)P = pslq(S)print P

The P printed is [-4, 0, 0, 2, 1, 1, 0, 1].

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 24 / 26

Page 25: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

some recommended reading

David H. Bailey: Integer Relation Detection.Computing in Science and Engineering 2(1):24–28, 2000.

David Joyner and William Stein:Open Source Mathematical Software. Opinion.Notices of the American Mathematical Society 54(10):1279, 2007.

Joel Moses: Macsyma: A personal history. Editorial.Journal of Symbolic Computation 47(2):123–130, 2012.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 25 / 26

Page 26: Welcome to MCS 507 - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs507f12/welcome507.pdf · and may be solve in pairs. ... and could form the basis for a project presentation

Summary + Exercises

We use Python and Sage to prototype solutionsto practical mathematical problems.

Exercises:1 Visit www.sagemath.org and take a tour.

What feature of Sage interests you the most?2 Visit www.python.org to install Python on your computer and

the software required by the text book.3 Consider r = 1.2599210498948732. Apply the PSLQ

algorithm to find the algebraic number that is closest to r.

A selection of these exercises will be collected at a date to beannounced soon.

Scientific Software (MCS 507) Welcome to MCS 507 27 Aug 2012 26 / 26