Neven Sajko on Thu, 22 Jul 2021 20:07:55 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Making a polynomial and evaluating it many times from C++


Hello,

I want to make a certain polynomial in Pari (see script below) and
evaluate it millions of times (plus some other bits) from my C or C++
code.

I looked into gp2c, but it seems aimed at producing C code which will
again be used from gp, instead of from other C or C++ code.

I know that there's a "User’s Guide to the PARI library", but it's
currently a bit overwhelming.

So I hope you people here could help me translating my gp script
(below) into C or C++ code. I hope to at least get pointers to
specific parts of the documentation or to some ready-made example.

To recap, I want to translate my script to to C code that calls the
Pari library, and is in turn called from other C/C++ code. Here's the
script:


default(parisize, 2147483648);
default(realbitprecision, 131072);
default(seriesprecision, 2048);
default(strictargs, 1);

default(format, "g.20");

truncated_power_series = simplify(sum(n = 0, 767, sqr(binomial(1/2, n)) * h^n));

ellipse_perimeter(a, b) = {
        my(sum = a + b);
        my(x = sqr((a - b) / sum));
        Pi * sum * subst(truncated_power_series, h, x);
};

ellipse_perimeter(0.5, 0.5)
ellipse_perimeter(0.75, 0.25)
...


The arguments to the ellipse_perimeter function must be IEEE 754
binary64 numbers (known as 'double' in the C/C++ world), but they
should then be converted to arbitrary precision floats, as the point
of the computation is to get an accurate result.

Another question, perhaps subjective: would it perhaps make more sense
to just export the coefficients of the polynomial to my C++ code, and
then use MPFR directly to evaluate it? That way Pari does only the
symbolic manipulation, which it is more suited for, I guess?

Thanks,
Neven