+ All Categories
Home > Documents > Maxima, a component in Sage & Pexpect

Maxima, a component in Sage & Pexpect

Date post: 03-Feb-2022
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
40
Maxima, a component in Sage & Pexpect 1 Maxima in Sage the computer algebra system Maxima in Sage arithmetic in Maxima functions and Taylor series solvers in Maxima 2 Historical Development milestones and management problems Lisp is well suited for computer algebra 3 Pexpect dialogue with an interactive program pexpect commands playing a number guessing game MCS 507 Lecture 32 Mathematical, Statistical and Scientific Software Jan Verschelde, 9 November 2012 Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 1 / 40
Transcript

Maxima, a component in Sage & Pexpect1 Maxima in Sage

the computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

MCS 507 Lecture 32Mathematical, Statistical and Scientific Software

Jan Verschelde, 9 November 2012

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 1 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 2 / 40

Maxima in Sage

Maxima (formerly known as Macsyma) is

a GPL computer algebra system,

available as one of the components in Sage.

We run Maxima explicitly in Sage via1 the class Maxima, do help(maxima); or2 opening a Terminal Session with maxima.

We study a computer algebra system, considering1 arithmetic: use as a calculator;2 expression manipulation, polynomials, series;3 solvers: linear, polynomial, general, differential, etc.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 3 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 4 / 40

conversion to float

(%i1) a : 1/3;1

(%o1) -3

(%i2) b : float(a);(%o2) .3333333333333333(%i3) fpprec;(%o3) 16(%i4) a - b;(%o4) 0.0(%i5) c : .333333333333;(%o5) 0.333333333333(%i6) b - c;(%o6) 3.33288951992472e-13

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 5 / 40

using bigfloat

(%i1) a : 1/3;1

(%o1) -3

(%i2) b : bfloat(a);(%o2) 3.333333333333333b-1(%i3) a - b;(%o3) 0.0b0(%i4) a - b, fpprec:30;(%o4) - 2.31296463463570992334484843303b-18(%i5) bfloat(%pi), fpprec:30;(%o5) 3.14159265358979323846264338328b0

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 6 / 40

float to rational

(%i1) p : float(%pi);(%o1) 3.141592653589793(%i2) r : rat(p);

rat: replaced 3.141592653589793 by 103993/33102 = 3.1415926530119025103993

(%o2)/R/ ------33102

(%i3) p30 : bfloat(%pi), fpprec:30;(%o3) 3.14159265358979323846264338328b0(%i4) r30 : rat(p30), ratepsilon:1.e-30, fpprec:30;‘rat’ replaced 3.14159265358979323846264338328B0 by 6134899525417045/1952799169684491

6134899525417045(%o4)/R/ ----------------

1952799169684491(%i5) bfloat(r30 - %pi), fpprec:32;(%o5) - 4.9746290434154885284828108903425b-32

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 7 / 40

in a Sage session

sage: p = maxima("bfloat(%pi)")sage: p3.141592653589793b0sage: type(p)<class ’sage.interfaces.maxima.MaximaElement’>sage: s = str(p)sage: c = "rat(bfloat(%s)), ratepsilon:1.0e-18" % ssage: r = maxima(c)sage: r325994779/103767361sage: float(r)3.1415926535897931

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 8 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 9 / 40

defining functions

(%i1) f(x,y) := exp(-x)*sin(y);(%o1) f(x, y) := exp(- x) sin(y)(%i2) f(1,2);

- 1(%o2) %e sin(2)(%i3) f(1.0,2.0);(%o3) .33451182923926226(%i4) fundef(f);(%o4) f(x, y) := exp(- x) sin(y)

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 10 / 40

Taylor series

(%i1) f(x,y) := exp(-x)*sin(y);(%o1) f(x, y) := exp(- x) sin(y)(%i2) t : taylor(f(x,y),x,a,2,y,b,1);

sin(b) cos(b) (y - b)(%o2)/T/ ------ + -------------- + . . .

a a%e %e

sin(b) cos(b) (y - b)+ (- ------ - -------------- + . . .) (x - a)

a a%e %e

sin(b) cos(b) (y - b) 2+ (------ + -------------- + . . .) (x - a) + . . .

a a2 %e 2 %e

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 11 / 40

evaluating series

(%i3) e : ev(t);2

(%o3)/R/ ((cos(b) x + (- 2 a - 2) cos(b) x2

+ (a + 2 a + 2) cos(b)) y2

+ (sin(b) - b cos(b)) x + ((- 2 a - 2) sin(b)

+ (2 a + 2) b cos(b)) x2

+ (a + 2 a + 2) sin(b)2 a

+ (- a - 2 a - 2) b cos(b))/(2 %e )

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 12 / 40

making functions

(%i4) ev(e,x=1,y=2,b=0);2

a + 1(%o4)/R/ ------

a%e

(%i5) p(x,y) := ev(e,x=x,y=y,b=0);(%o5) p(x, y) := ev(e, x = x, y = y, b = 0)(%i6) p(1,2);

2a + 1

(%o6)/R/ ------a

%e

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 13 / 40

functional decomposition

We can decompose any polynomial p:

(%i1) polydecomp(x^210,x);7 5 3 2

(%o1) [x , x , x , x ](%i2) e : subst(x^7,x,x^5);

35(%o2) x(%i3) e : subst(e,x,x^3);

105(%o3) x(%i4) e : subst(e,x,x^2);

210(%o4) x

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 14 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 15 / 40

finding roots

(%i1) p : x^3 - x + 1;3

(%o1) x - x + 1(%i2) allroots(p);(%o2) [x = .5622795120623012 %i

+ 0.662358978622373,x = 0.662358978622373- .5622795120623012 %i,

x = - 1.324717957244746](%i3) realroots(p);

44450159(%o3) [x = - --------]

33554432

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 16 / 40

symbolic linear algebra

(%i1) m : matrix([a,b,c],[b,c,a],[c,a,b]);[ a b c ][ ]

(%o1) [ b c a ][ ][ c a b ]

(%i2) determinant(m);2 2

(%o2) c (a b - c ) + a (b c - a )2

- b (b - a c)

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 17 / 40

eigenvalues

(%i3) charpoly(m,L);2

(%o3) ((b - L) (c - L) - a ) (a - L)

+ c (a b - c (c - L)) - b (b (b - L) - a c)

(%i4) eigenvalues(m);2 2 2

(%o4) [[- sqrt(c - b c - a c + b - a b + a ),2 2 2

sqrt(c - b c - a c + b - a b + a ),c + b + a], [1, 1, 1]]

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 18 / 40

solving polynomial systems

(%i1) L : [4*x^2 - y^2 = 12, x*y - x = 2];2 2

(%o1) [4 x - y = 12, x y - x = 2](%i2) solve(L,[x,y]);(%o2) [[x = 2, y = 2], [x = .5202594388652008 %i

- .1331240357358706,y = .07678378523787788 - 3.608003221870287 %i],[x = - .5202594388652008 %i

- .1331240357358706,y = 3.608003221870287 %i + .07678378523787788],[x = - 1.733751846381093,y = - .15356757100196963]]

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 19 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 20 / 40

development of Macsyma

Some milestones:

1 Macsyma was developed at MIT from 1968 through 1982 in thelaboratory for computer science(named project MAC, Machine Aided Cognition).MIT turned Macsyma in 1982 over to DOE.By the work of Richard Fateman at U.C. Berkeley,Macsyma was modified for use under UNIX.

2 DOE Macsyma was maintained by Professor William F. Schelter atthe University of Texas from 1982 to 2001.In 1998 released under GNU Public License.

3 In 2000, the Maxima project at SourceForge started.DOE Macsyma is now called Maxima.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 21 / 40

problems of Macsyma

Why did Macsyma not become popular?

Symbolic Inc. (had exclusive license to Macsyma in 1982) tiedMacsyma to selling Lisp machines.

Wolfram released Mathematica in 1988 with an innovatingnotebook interface, what most users wanted.

Lisp systems had slow numerical analysis.LAPACK was added to Macsyma in 1996.

The development of Macsyma failed to attract funding fromacademic and government communities.

For more, read Macsyma: a personal historya paper by Joel Moses.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 22 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 23 / 40

Maxima is written in Lisp

Lisp is known as the second oldest programming language.

John McCarthy (winner of the 1971 A.M. Turing award, coined the termartificial intelligence) invented Lisp (1958).

In his paper Lisp – notes on its past and future – 1980 characterizesLisp by 15 items, starting with:

1 Computing with symbolic expressions rather than numbers.2 Representation of symbolic expressions and other information by

list structure in computer memory.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 24 / 40

Lisp in Sage

sage: maxima.lisp("(+ 2 17)")’ :lisp (+ 2 17)\r\n"";\r\n\r\n19\r\n(’sage: print _:lisp (+ 2 17)

"";

19(sage:

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 25 / 40

lisp mode in Maxima(%i1) f(x) := x^2 + x;

2(%o1) f(x) := x + x(%i2) to_lisp();

Type (to-maxima) to restart, ($quit) to quit Maxima.

MAXIMA> (describe ’$f)

$F - internal symbol in MAXIMA packageproperty MPROPS:(NIL MEXPR((LAMBDA) ((MLIST) $X)((MPLUS #1=(0 "stdin" SRC $F 0)) ((MEXPT #1#) $X 2) $X)))

property LINEINFO:((MPLUS #1=(0 "stdin" SRC $F 0)) ((MEXPT #1#) $X 2) $X)

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 26 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 27 / 40

dialogue with an interactive program

How does Sage interface with other computer algebra systems?

Pexpect is a pure Python module for spawning child application:the child application can be controlled as in typing commands.

Pexpect is free and open source software,available for download from sourceforge.Installing is easy: python setup.py install(requires root access).

Our first hello world experience with Pexect:launch a python2.6 session from the python 2.7 interpreter.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 28 / 40

launching python2.6

$ python pexpect_hello_python.pyspawning python2.6 session ...

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)[GCC 4.2.1 (Apple Inc. build 5646)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import sys>>> print sys.version2.6.1 (r261:67515, Jun 24 2010, 21:47:49)[GCC 4.2.1 (Apple Inc. build 5646)]>>> exit()

Bye, script ends.$

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 29 / 40

the script pexpect_hello_python.py

print ’spawning python2.6 session ...\n’import pexpectchild = pexpect.spawn(’/usr/bin/env python2.6’)# search the stream for the promptchild.expect(’>>>’)# print all data in the stream before the promptprint child.before,# print the data that was matchedprint child.after,# give control to interactive userchild.interact()# to end the child process before the parent endschild.close()print ’\nBye, script ends.’

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 30 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 31 / 40

pexpect commands

The class spawn of the module pexpect is the main interface:

1 The syntax for spawning a process is

>>> import pexpect>>> child = pexpect.spawn(command)

2 At the creation of an object of the class spawn, a stringis passed which contains the command to launch a program.

3 A child process is spawned and through this objectwe can interact with the application spawned.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 32 / 40

interacting with an application

To interact with the application, we apply the method expectto the object child of the class spawn.

The main argument of the expect method is a pattern,expressed as a regular expression (as defined by the re module).

After a match is found, the attributes ’before’, ’after’, and ’match’of the spawned object child are set:

child.before: all data before the match

child.after: the data that was matched

child.match: the re.MatchObject used in the re match

The interact gives control of the child process to the interactive user.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 33 / 40

Maxima, a component in Sage & Pexpect

1 Maxima in Sagethe computer algebra system Maxima in Sagearithmetic in Maximafunctions and Taylor seriessolvers in Maxima

2 Historical Developmentmilestones and management problemsLisp is well suited for computer algebra

3 Pexpectdialogue with an interactive programpexpect commandsplaying a number guessing game

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 34 / 40

a simple number guessing game

The script game_oracle_number.py:

from random import randintsecret = randint(0,9)print ’Welcome to our number guessing game!’while True:

guess = input(’give a natural number in [0,9] : ’)if guess == secret: breakif guess < secret:

print ’-> your guess is too low’else:

print ’-> your guess is too high’print ’Congratulations! You found the secret.’

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 35 / 40

the script pexpect_interactive_game.py

print ’spawning the oracle ...\n’import pexpectchild = pexpect.spawn\

(’/usr/bin/env python game_oracle_number.py’)# search the stream for the promptchild.expect(’:’)# print all data in the stream before the promptprint child.before,# print the data that was matchedprint child.after,# give control to interactive usertry:

child.interact()except:

print ’Child exited, game over.’print ’\nBye, script ends.’

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 36 / 40

playing the game interactively

$ python pexpect_interactive_game.pyspawning the oracle ...

Welcome to our number guessing game!give a natural number in [0,9] : 3-> your guess is too highgive a natural number in [0,9] : 2Congratulations! You found the secret.

Bye, script ends.$

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 37 / 40

the script pexpect_guessing_game.py

print ’spawning the oracle ...\n’import pexpectchild = pexpect.spawn\

(’/usr/bin/env python game_oracle_number.py’)for i in range(10):

child.expect(’:’)child.sendline(str(i))child.read(6)print ’reply of oracle’, child.afterif child.after[-2:] == ’Co’:

print ’We guessed the secret!’break

print ’\nBye, script ends.’

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 38 / 40

script plays the game

$ python pexpect_guessing_game.pyspawning the oracle ...

reply of oracle 0->reply of oracle 1->reply of oracle 2CoWe guessed the secret!

Bye, script ends.$

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 39 / 40

Summary + ExercisesThe Maxima Manual Ver. 5.25.0 has 1,026 pages (pdf).

With Pexpect we can communicate with child applications.

1 Compute a list of consecutive rational approximations of√

2 withaccuracy equal to 10−k , for k from 10 to 30.

2 Explore the solving of differential equations in Maxima.In particular, take the examples of Lecture 24 we solved withodeint of odepack.

3 Modify the pexpect_guessing_game.py so the program takesthe feedback of the oracle into account and applies a bisectionstrategy to guess the secret.

Homework due Friday 16 November at 10AM:exercises 1 and 3 of lecture 21; exercise 1 of lecture 22;exercise 2 of lecture 23; exercises 2 and 4 of lecture 24;exercises 1 and 2 of lecture 25; exercises 1 and 3 of lecture 26.

Scientific Software (MCS 507) Maxima and Pexpect 9 November 2012 40 / 40


Recommended