Re: Automatic C code generation

jmccarro@world.std.com (James McCarron)
30 Apr 1999 22:58:22 -0400

          From comp.compilers

Related articles
Automatic C code generation yusan@isd.uni-stuttgart.de (Hakan Yusan) (1999-04-29)
Re: Automatic C code generation nr@labrador.cs.virginia.edu (Norman Ramsey) (1999-04-30)
Re: Automatic C code generation jmccarro@world.std.com (1999-04-30)
| List of all articles for this month |
From: jmccarro@world.std.com (James McCarron)
Newsgroups: comp.compilers
Date: 30 Apr 1999 22:58:22 -0400
Organization: The World, Public Access Internet, Brookline, MA
References: 99-04-106
Keywords: C

Hakan Yusan <yusan@isd.uni-stuttgart.de> wrote:
>Hi, I am looking for some help or ideas how to handle the task of
>generating automatically C codes for the solution of equations that
>can be solved by symbolic computation. In other words, I have to write
>a code that will generate a C code from the symbolic solution of a
>system of equations. An example may be using Maple V or Mathematica
>for both symbolic computation and C code generation.


Hi:


This facility exists in Maple (I would expect that other CAS have
something similar). In Maple, you can use the "C" command in the
codegen package (there is also a "fortran" command that generates
Fortran code). For example,


> e := Pi * ln( x^2 ) - sqrt( 2 ) * ln( x^2 )^2 - cot( x^3 );
                                                                  2 1/2 2 2 3
                                          e := Pi ln(x ) - 2 ln(x ) - cot(x )


> codegen[C]( e );
            t0 = 0.3141592653589793E1*log(x*x)-sqrt(2.0)*pow(log(x*x),2.0)-1/tan(x*x*
x);
> codegen[C]( e, 'optimized' ); # do CSE
            t1 = x*x;
            t2 = log(t1);
            t4 = sqrt(2.0);
            t5 = t2*t2;
            t8 = 1/tan(t1*x);
            t9 = 0.3141592653589793E1*t2-t4*t5-t8;


The intent is that you should cut-and-paste the output into your C
source. You can also ask that the output be written on a named
disk file. It can also deal with (some) Maple procedures


> f := proc( x )
> if x < 0 then
> sin( x )
> elif x > 0 then
> cos( x )
> else
> x
> fi
> end;
            f := proc(x) if x < 0 then sin(x) elif 0 < x then cos(x) else x fi end


> with(codegen):
> C( f );
#include <math.h>
double f(x)
double x;
{
    {
        if( x < 0.0 )
            return(sin(x));
        else
            if( 0.0 < x )
                return(cos(x));
            else
                return(x);
    }
}


There are also facilities for handling arrays, and controlling the precision
(single vs. double), declarations etc. Lots of examples can be found
in the online help.


To use this with solutions of equations, you could do something like this
(intermediate output suppressed for brevity):


> sols := { solve( sin(x) - cos(2*x) = 1, x ) }:
> f,g,u,v := op( map( unapply, sols, x ) ):
> map( C, [ f, g, u, v ], 'optimized' );
/* The options were : operatorarrow */
#include <math.h>
double f(x)
double x;
{
    double t1;
    double t2;
    double t5;
    {
        t1 = sqrt(17.0);
        t2 = -1.0+t1;
        t5 = sqrt(2.0)*sqrt(t2);
        return(atan(t2/t5));
    }
}


bytes used=4016148, alloc=1965720, time=8.14
/* The options were : operatorarrow */
#include <math.h>
double g(x)
double x;
{
    double t1;
    double t2;
    double t5;
    double t9;
    {
        t1 = sqrt(17.0);
        t2 = -1.0+t1;
        t5 = sqrt(2.0)*sqrt(t2);
        t9 = atan(t2/t5);
        return(-t9+0.3141592653589793E1);
    }
}


/* The options were : operatorarrow */
#include <math.h>
double u(x)
double x;
{
    double t1;
    double t2;
    double t5;
    {
        t1 = sqrt(17.0);
        t2 = -1.0-t1;
        t5 = sqrt(2.0)*sqrt(t2);
        return(atan2(t2/4.0,t5/4.0));
    }
}


/* The options were : operatorarrow */
#include <math.h>
double v(x)
double x;
{
    double t1;
    double t2;
    double t5;
    {
        t1 = sqrt(17.0);
        t2 = -1.0-t1;
        t5 = sqrt(2.0)*sqrt(t2);
        return(atan2(t2/4.0,-t5/4.0));
    }
}


I hope this helps you get started.


Cheers,


James
--
James McCarron


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.