Pre-processing large mathematical expressions for gmp?

Paul Zimmermann Paul.Zimmermann at loria.fr
Tue Jun 15 09:44:28 CEST 2010


> Date: Mon, 14 Jun 2010 21:32:02 -0300
> From: Jim Skea <jimskea at gmail.com>
> 
> Does anyone know of a pre-processor that would take a long mathematical
> expression and convert it into the equivalent set of gmp function calls,
> creating any temporary variables as necessary?
> 
> Something that would take, as a small example
> y = y + h6*(k1+2*k2+2*k3+k4)
> 
>  and produce
> 
>  mpf_mul_ui(t0, k3, 2);
>  mpf_mul_ui(t1, k2, 2);
>  mpf_add(t2,t0,t1);
>  mpf_add(t3,t2,k1);
>  mpf_add(t4,t3,k4);
>  mpf_mul(t5,t4,h6);
>  mpf_add(y, y, t5);
> 
>  Failing that, any idea about how to do this (using yacc? bison?) other than
> writing a dedicated program?
> 
> Thanks in advance.
> 
> Jim

I once wrote a Maple program for this, for the mpz class. It is very simple
so far (see attached). For example:

> l:=[t0=2*k3,t1=2*k2,t2=t0+t1,t3=t2+k1,t4=t3+k4,t5=t4*h6,y=y+t5]:
> for e in l do gmp_set(lhs(e),rhs(e)) od:
   mpz_mul_ui (t0, k3, 2);
   mpz_mul_ui (t1, k2, 2);
   mpz_set (t2, t0);
   mpz_set (tmp, t1);
   mpz_add (t2, t2, tmp);
   mpz_set (t3, t2);
   mpz_set (tmp, k1);
   mpz_add (t3, t3, tmp);
   mpz_set (t4, t3);
   mpz_set (tmp, k4);
   mpz_add (t4, t4, tmp);
   mpz_mul (t5, t4, h6);
   mpz_set (y, y);
   mpz_set (tmp, t5);
   mpz_add (y, y, tmp);

Maple has the advantage of having an optimize function (codegen[optimize])
which can factor out common subexpressions. Likely other computer algebra
systems have a similar functionality.

Paul Zimmermann
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/octet-stream
Size: 2004 bytes
Desc: not available
URL: <http://gmplib.org/list-archives/gmp-discuss/attachments/20100615/4a6e1df8/attachment.obj>


More information about the gmp-discuss mailing list