[Date Index]
[Thread Index]
[Author Index]
Re: c code generation
*To*: mathgroup at smc.vnet.net
*Subject*: [mg4408] Re: c code generation
*From*: song at cs.purdue.edu (Chang-Hyeon Song)
*Date*: Fri, 19 Jul 1996 04:26:50 -0400
*Organization*: Purdue University
*Sender*: owner-wri-mathgroup at wolfram.com
George Jefferson (george at mech.seas.upenn.edu) wrote:
: :: The whole point of using a tool like Mathematica is to avoid writing C
: :: code. If you must do that, I recommend a standard reference like
: :: Numerical Recipies in C. In any event, Mathematica does not generate C
: :: code for you.
:
: hardly the "whole point". Mathematica is a good tool for preparing
: portions source code to send off to another application.
:
: :I am about to release automatic C program(code) generator from Mathematica
: :language(McCogen). Maybe after my defense of MS thesis.
:
: have you looked at MathSource item # 0205-254 ?
:
: bugger if I can recall the name of the package at the moment...all
: I have is that ref number.
If you are refering to Sofronious' Mathematica package that extends builtin
format rule, yes I know. It is even compared to McCogen in my thesis.
McCogen can produce entire(whole) C program that can be compled with any ANSI
C compiler with no modification at all. Generated C code includes easy-to-read
indentation, header files, automatic declaration of all variables, dynamic
allocation of memory, symbolically manipulated expressions.
Here is some sample usage.
In[28]:=
M2C[
f[r_Integer] := -2000 r^2;
g[x_Real] := Simplify[Integrate[Sin[x]+Cos[x],x] + Sum[1/x,{x,1,10}]];
x=1.0;
a=Simplify[D[f[x],x] + D[g[x],x]]
]
Out[28]=
#include <math.h>
/* Function prototypes */
int f( int );
double g( double );
/* Global variables */
double x, a;
int f (int r)
{
return ((-2000 * pow(r,2)) );
}
double g (double x)
{
return (((7381 / (double) 2520) + (-1 * cos(x)) + sin(x)) );
}
int main()
{
x = 1.000000000000000;
a = ((-4000 * x) + cos(x) + sin(x));
}
------------------------------------------------------
And here's Newton's method
In[1]:=
M2C[
bPrime[r_Double] := -2000/r^2 + 4 Pi r;
FSIZE = 0.00001;
x = 4.0;
While [ Abs[bPrime[x]//N] >= FSIZE,
x = N[x - bPrime[x] / bPrime'[x]];
Print["r = ", x, " bPrime = ", bPrime[x]//N]
];
Print["The root of bPrime is r = ", x]
]
Out[1]:=
#include <math.h>
#include <stdio.h>
/* Function prototypes */
double bPrime( double );
/* Global variables */
double FSIZE, x;
double bPrime (double r)
{
return (((-2000 / pow(r,2)) + (4 * M_PI * r)) );
}
int main()
{
FSIZE = 0.000010000000000;
x = 4.000000000000000;
while(fabs(bPrime(x)) >= FSIZE)
{
x = (x + (-1 * bPrime(x) / ((4 * M_PI) + (4000 * pow(x,-3)))));
printf("r = %f bPrime = %f\n", x, bPrime(x));
}
printf("The root of bPrime is r = %f\n", x);
}
---
Chang Song (song at cs.purdue.edu)
==== [MESSAGE SEPARATOR] ====
Prev by Date:
**Re: Is there a bug in FIT?**
Next by Date:
**Re: Problem with discontinuity in NDSolve[]**
Previous by thread:
**Re: c code generation**
Next by thread:
**Re: c code generation**
| |