2
$\begingroup$

I noticed that Python does not have a good datatype for rational numbers, certainly not for algebraic numbers like $\tfrac{1 + \sqrt{2}}{3}$ or the real root of $x^3 - 5x + 7$. They have the fractions data type and I was able to hack a solution to my particular problem from there using elaborate arrangements of matrices.

It would be really nice to take advance of the PARI library which is written in C. Since GP is an interface to the PARI, it seems possible to write a Python interface to those libraries.

I can't seem to install Pari-Python so this is a chance to learn to import C libraries directly.


At the heart of it, I just want to learn how to import C++ libraries into Python. Specifically, libraries from the PARI-GP source code.

$\endgroup$
  • 1
    $\begingroup$ Would using Sage work, or are you looking to avoid Sage? $\endgroup$ – Geoff Oxberry May 9 '14 at 18:29
  • $\begingroup$ I would prefer not to use Sage $\endgroup$ – john mangual May 9 '14 at 18:49
1
$\begingroup$

I noticed that Python does not have a good datatype for rational numbers, certainly not for algebraic numbers like $\tfrac{1 + \sqrt{2}}{3}$ or the real root of $x^3 - 5x + 7$. They have the fractions data type and I was able to hack a solution to my particular problem from there using elaborate arrangements of matrices.

If the reason you'd prefer not to use Sage is that you don't want to install kitchen sinks, you can do this in Python using sympy.

$\endgroup$
1
$\begingroup$

To import C or C++ code into Python, please see SWIG.

However, it might be easier to try to fix the errors you're seeing with installing or importing the pari-python library, so maybe post the error(s) you're seeing as a separate question and get them resolved.

$\endgroup$
0
$\begingroup$

An alternative to SWIG is Cython. In Cython vs SWIG, fight! you can see a comparison of the syntax for both of them (TL;DW: both need you to write some boating code, each one on a different corner).

I think the main advantage of Cython is the syntax, closer to Python, and that you can easily and progressively convert Python code to C. It is under heavy development, but it is quite stable. It is what most major scientific libraries are using (Numpy, Scipy, scikits...), with a lot of documentation oriented to the scientific stack.

SWIG is a more mature project, also maintained and compatible with other programming languages. I know of some research projects that make heavy use of it; but as far as I can remember, none of the libraries I have ever used in detail.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.