PYML Introduction

PYML is a python interface to Mathematica which uses MathLink. It allows the Python programmer to evaluate Mathematica expressions. Expressions are passed to PYML as a Python object, which is processed into MathLink calls. PYML calls MathLink to evaluate the expression and prints the result. Currently, PYML supports Mathematica 2.2 and 3.0. The program is evolving continuously.

The original interface was written by David Konerding (dek@cgl.ucsf.edu). Contact the author with bugfixes and ideas for improvements.

The program is available from PYML page

PYML Installation

PYML is composed of a python module with some additional C code. It is necessary to compile the C code into a dynamically linked lipary which is loaded by the PYML Python code. PYML was originally developed on Windows 95 using Visual C++, so there is a VC++ project file distributed with it. Additionally, a Setup file is supplied for UNIX users. Try "make -f Makefile.pre.in boot" to make a Makefile.

Once the DLL is compiled, it must be placed in a directory which is referenced by PYTHONPATH.

PYML Usage

Mathematica expressions are composed of functions, symbols, strings, integers and reals. Any Mathematica expression can be represented in its full form by a Python object. Examples of PYML usage are found in the file pyml.py file. Here is a basic tutorial.

Entering functions

Full Form

The basic form of all functions in Mathematica is:

head[arguments]

where head is the name of the function and arguments is a comma-limited list of arguments. The basic form of all functions in PYML is:

head([list of arguments])

The full form of the function x+1 in Mathematica:

Plus[1,x]

The full form of the function x+1:

function = MathematicaFunction("Plus",[MathematicaSymbol("x"),MathematicaInteger(1)])

The PYML string representation of the function is the Mathematica full form. It can be cut and pasted into Mathematica.

print function
Plus[x,1]

To evaluate the function, you need to wrap it in an expression then send it to Mathematica for evaluation: expression = MathematicaExpression(function)
e = m.evaluate(expression)
result= m.process()

The result is a PYML object representing a Mathematica Expression, suitable for further input to PYML or to be cut and pasted into Mathematica.

Python Compact Form

As you can see, the full form is rather tedious. You can usually enter expressions using simpler code. To simplify entering common expressions, operators on the mathematica objects generate Full Form expressions:

Plus = MathematicaFunction("Plus")
function = Plus("x"+1)

or

x=MathematicaSymbol("x")
function = x+1

In PYML expressions, quoted strings are interpreted as Mathematica symbols. Integers and reals are intepreted as Mathematica integers and reals. Strings must be explicitly created: string = MathematicaString("hello world")

More Complicated Functions

More complicated functions, such as integrals, are entered easily.

The integral of x^2 with x varying from 0 to 2:

function = Integrate(Power("x",2), List("x",0,2))

Plotting

You can plot (to a postscript file) functions just as you would in the front end. The output is saved to test.eps file.

function = Plot((Times(Power(Sin("x"),"x"),"Pi")),List("x",0,"Pi"))


David E. Konerding