Pari Python

A

Anton Mellit

Hi,

I am working on a Pari-Python module (see about GP/PARI at
http://pari.math.u-bordeaux.fr/). Similar project was started by
Stefane Fermigier 12 years ago (you can find a post about it on this
newsgroup). You can see some screenshots on my blog (http://
mellit.wordpress.com/2007/10/28/pari-python/). I reproduce the text on
my blog here.

I finished some working version of the pari-python module. I tried to
make it is close as possible to the standard PARI shell, at the same
time using the standard python syntax. Here I put some screenshots. I
didn’t make a proper package distribution for python (the code still
looks ugly and I still don't know how to make proper distributions),
but if someone is interested to test it send me an email. This is in
early alpha stage. A lot of things are not working. I imported almost
all functions of gp by an automated perl script but I don’t know which
of them actually work and which not.

There are two special difficulties you may notice about python. The
first one is the power operation. In python it is ‘**’, whereas ‘^’ is
reserved for the bitwise xor. I made some little changes to the python
source so that ‘^’ and ‘^=’ now work as power, and ‘^^’ and ‘^^=’ work
as xor if you still want to use it. This is done by modifying about 30
lines of source code in several files: Include/token.h, Modules/
parsermodule.c, Parser/tokenizer.c, Python/ast.c, Grammar/Grammar.

The second difficulty is that expressions like ‘1/2′ produce 0 in
python. For this my module installs my own handler for the operation
‘divide’ for integers and longs. That’s it. In other respects it is a
normal python module.

I will greatly appreciate any feedback.

Anton
 
M

mensanator

Hi,

I am working on a Pari-Python module (see about GP/PARI athttp://pari.math.u-bordeaux.fr/). Similar project was started by
Stefane Fermigier 12 years ago (you can find a post about it on this
newsgroup). You can see some screenshots on my blog (http://
mellit.wordpress.com/2007/10/28/pari-python/). I reproduce the text on
my blog here.
Cool.


I finished some working version of the pari-python module. I tried to
make it is close as possible to the standard PARI shell, at the same
time using the standard python syntax.

Does your version use the GMP library (optional in PARI).
Does it support Windows? I couldn't tell if the PARI
Windows distribution has GMP, but I suspect not since GMP
doesn't support Windows.
Here I put some screenshots. I
didn't make a proper package distribution for python (the code still
looks ugly and I still don't know how to make proper distributions),
but if someone is interested to test it send me an email. This is in
early alpha stage. A lot of things are not working. I imported almost
all functions of gp by an automated perl script but I don't know which
of them actually work and which not.

There are two special difficulties you may notice about python. The
first one is the power operation. In python it is '**', whereas '^' is
reserved for the bitwise xor. I made some little changes to the python
source so that '^' and '^=' now work as power, and '^^' and '^^=' work
as xor if you still want to use it.

Oh, dear.
This is done by modifying about 30
lines of source code in several files: Include/token.h, Modules/
parsermodule.c, Parser/tokenizer.c, Python/ast.c, Grammar/Grammar.

Am I gonna have to re-compile Python?

That's imposible for ordinary Windows users.

Are you going to make a binary Windows distribution?

If not, it may as well be on the far side of the moon for
all the good it would be to me.
 
R

Robert Kern

Anton said:
Hi,

I am working on a Pari-Python module (see about GP/PARI at
http://pari.math.u-bordeaux.fr/). Similar project was started by
Stefane Fermigier 12 years ago (you can find a post about it on this
newsgroup). You can see some screenshots on my blog (http://
mellit.wordpress.com/2007/10/28/pari-python/). I reproduce the text on
my blog here.

I finished some working version of the pari-python module. I tried to
make it is close as possible to the standard PARI shell, at the same
time using the standard python syntax. Here I put some screenshots. I
didn’t make a proper package distribution for python (the code still
looks ugly and I still don't know how to make proper distributions),
but if someone is interested to test it send me an email. This is in
early alpha stage. A lot of things are not working. I imported almost
all functions of gp by an automated perl script but I don’t know which
of them actually work and which not.

There are two special difficulties you may notice about python. The
first one is the power operation. In python it is ‘**’, whereas ‘^’ is
reserved for the bitwise xor. I made some little changes to the python
source so that ‘^’ and ‘^=’ now work as power, and ‘^^’ and ‘^^=’ work
as xor if you still want to use it. This is done by modifying about 30
lines of source code in several files: Include/token.h, Modules/
parsermodule.c, Parser/tokenizer.c, Python/ast.c, Grammar/Grammar.

The second difficulty is that expressions like ‘1/2′ produce 0 in
python. For this my module installs my own handler for the operation
‘divide’ for integers and longs. That’s it. In other respects it is a
normal python module.

I will greatly appreciate any feedback.

Have you looked at SAGE at all? They already have wrappers for Pari.

http://www.sagemath.org/

I don't recommend continuing to modify core parts of Python just for your
module. It means that you will break other Python modules. If you can't use
other Python modules with your module, what's the point of using Python at all?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
A

Anton Mellit

Does your version use the GMP library (optional in PARI).
Does it support Windows? I couldn't tell if the PARI
Windows distribution has GMP, but I suspect not since GMP
doesn't support Windows.

No, I didn't try to use gmp. But this may be not a bad idea. I am
trying to compile gmp right now. I am also using Windows. Here there
are two ways to work. The first is cygwin. Actually the standard PARI
distribution uses cygwin. Problem is that in this case you must also
use cygwin version of Python. It is very easy in my case - I have a
full Cygwin system installed together with python, bash, gcc etc. It
is not bad actually - bash is much more powerful than cmd. And you can
start windows programs from bash, start cygwin problems from windows,
I mean it is having linux and windows simultaneously.

But if you are using windows python then you must compile everything
for windows because somehow cygwin1.dll (yes, just one dll is enough)
does not work with windows python. So there are no problems with
compiling my module on windows provided you managed to compile PARI
which does not use cygwin. It is possible (you have to use mingw32 +
msys to compile and remove '#define UNIX' from paricfg.h), I tried
this and it seem to work. Personally I don't like it so much because
on windows python does not support colors, everything is black and
white. There is a thing called ipython (google it) which has some
colors. So this may be an option. So there should be no problems to
make the whole thing purely windows. I haven't tried to compile GMP
under mingw. But I think there should be no problems as well. This is
just mathematical library, it shouldn't use too much from the system.
Oh, dear.


Am I gonna have to re-compile Python?

Yes, unfortunately this hack with '^' needs python to be recompiled.
You see, the problem is not just with symbol. The problem is that
normally ^ has precedence less than +. For example even if you
redefine '^' to be the power operation the following expression 2*2^2
will be 16, not 8 as you expect.
That's imposible for ordinary Windows users.

Well, this should not be impossible. I haven't tried, but I've heard
that the free version of Microsoft Visual C++ is enough
(http://msdn2.microsoft.com/en-us/express/aa975050.aspx).
Are you going to make a binary Windows distribution?

Definitely I can make a binary distribution as well. Just need some time :)
If not, it may as well be on the far side of the moon for
all the good it would be to me.

So when I make a binary distribution for Windows I can upload it
somewhere and post a link.
 
A

Anton Mellit

Have you looked at SAGE at all? They already have wrappers for Pari.

Well :) I expected that question. I tried. But the distribution for
windows is so big :( (2GB) and I could not build it on cygwin, where
it is also pretty big. And I so much like pari - it is so light and
simple and still can do almost everything. So my idea is to make
simple module for python so that you don't need to install 2GB program
with 2000 functions where it is difficult to find the one you need,
but just type 'import pari', and then help(pari), and then find a
function like 'ellinit' and create an elliptic curve, or type some
polynomial and factor, or expand some function into power series, or
integrate numerically...

I heard from some people that SAGE is good but i somehow don't
appreciate the approach.
I don't recommend continuing to modify core parts of Python just for your
module. It means that you will break other Python modules. If you can't use
other Python modules with your module, what's the point of using Python at all?

Of course you don't have to replace ** with ^ or making that hack with
division. The module would work with or without these changes. But I
think every mathematician agrees that x**2 is really ugly. It is so
often I have to type something like
(1+x)*(1+x^2)*(1+x^3), compare with (1+x)*(1+x**2)*(1+x**3)
or some polynomial (compare 1+x+x^2+x^3 and 1+x+x**2+x**3)
The same thing about the division. Having to write R*1/2 (where R
equals to one, but the type of R is the new number type) is also ugly.
If you write a program you don't have to do it too often, but if you
work 'interactively' then you have to type it more often, then read
the result from screen, or copy-paste a formula from somewhere.

And I think (correct me if I am wrong) that the ^ operator (xor) is
used very very infrequently. And it is not difficult to replace all ^
with say ^^. The division is probably used more often, but python has
this trend anyway - to replace division with 'true' division, so
people should use // when they divide integers and expect an integer.

Other than these two things (the second one does not require
recompilation, so it is not so bad probably) I don't need any changes
to python core. I agree that a standard must be standard.

Anton
 
M

Michael L Torrie

Anton said:
And I think (correct me if I am wrong) that the ^ operator (xor) is
used very very infrequently. And it is not difficult to replace all ^
with say ^^. The division is probably used more often, but python has
this trend anyway - to replace division with 'true' division, so
people should use // when they divide integers and expect an integer.

That's besides the point. It *breaks* python, which is a problem. How
can someone use your code with other third-party modules which may use
this operator?
Other than these two things (the second one does not require
recompilation, so it is not so bad probably) I don't need any changes
to python core. I agree that a standard must be standard.

With that in mind, why not implement your shell layer using pypy (python
on python). That way you can have a full python parser that you can
modify to your hearts content, running on an unmodified cpython core.
Or implement your own parser using pyparsing.

Or how about this: Implement a shell in python that uses regex to
replace and translate PariPython syntax into python syntax, and then
evaluate it on the unmodified core.

There's got to be some solution that doesn't break python. Of course
really you would be (and are) creating a new language. Call it
PariPython or something. Make a new file extension so people don't
confuse it with python. like ".ppy" or something.
 
R

Robert Kern

Anton said:
Well :) I expected that question. I tried. But the distribution for
windows is so big :( (2GB) and I could not build it on cygwin, where
it is also pretty big. And I so much like pari - it is so light and
simple and still can do almost everything. So my idea is to make
simple module for python so that you don't need to install 2GB program
with 2000 functions where it is difficult to find the one you need,
but just type 'import pari', and then help(pari), and then find a
function like 'ellinit' and create an elliptic curve, or type some
polynomial and factor, or expand some function into power series, or
integrate numerically...

I heard from some people that SAGE is good but i somehow don't
appreciate the approach.


Of course you don't have to replace ** with ^ or making that hack with
division. The module would work with or without these changes. But I
think every mathematician agrees that x**2 is really ugly.

So? There is no reason to make alterations to the core to achieve what you want.
You just need to parse your almost-Python code yourself before executing it.
That's what SAGE does. You really should take a closer look at it. It is
possible to take just part of SAGE and not install the whole thing.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

J. Cliff Dyer

Anton said:
And I think (correct me if I am wrong) that the ^ operator (xor) is
used very very infrequently. And it is not difficult to replace all ^
with say ^^.
Oh God! *Please* don't start that conversation again. We had a thread
about bitwise operators a few weeks back. Half the people will pipe up
and say, "Nope, I never use them," and the other half will say, "I can't
live without them." And the ones who can't live without them tend to be
the ones doing more math-heavy work.

It's not worth messing with the bitwise operators.
The division is probably used more often, but python has
this trend anyway - to replace division with 'true' division, so
people should use // when they divide integers and expect an integer

If you want to use true division, delay your release until python 3 or
import it from __future__ yourself. Don't break all current code by
recompiling python to do it. You'll alienate most of your potential
user base, and develop a reputation for not playing well with others.

Cheers,
Cliff
 
A

Anton Mellit

Hi,
thanks for the comments. I think I should target on making my module
as a normal python module without using any modifications and
additionally (only additionally) provide some optional workaround for
the '^' and '/' problem. I am going to look at what Michael proposed
With that in mind, why not implement your shell layer using pypy (python
on python). That way you can have a full python parser that you can
modify to your hearts content, running on an unmodified cpython core.
Or implement your own parser using pyparsing.
Or how about this: Implement a shell in python that uses regex to
replace and translate PariPython syntax into python syntax, and then
evaluate it on the unmodified core.

Also I will look at how it is done in SAGE. SAGE is very difficult to
work with, it is definitely not pythonic and it is not implemented as
a bunch of python modules like I would prefer it. I can't find out how
to eliminate all the garbage there and leave only the python core. Its
pari support seems superficial. In many cases you have to evaluate
strings using pari interpreter like 'gp("a = intnum(x=0,6,sin(x))")'
(excerpt from the documentation).

Actually it should be nice to have this workaround available
separately for use by other math packages.

Anton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top