[tv]
As part of some [maxima] ramblings, a little script to take a fortan expression, which is used to generate
efficiently computed list of function values.
A possible use for this is to generate heavy graphs from mathematica expressions, which can be converted into
fortran expressions with the fortran command:
fortran(diff(sin(x)/x,x));
COS(x)/x-SIN(x)/x**2
The idea is to have a easy to use C program call a little fortran subroutine with the fortran code
in it, and that the whole operation is done by a tcl proc, which also takes care of the
combined compilation of the fortran and C function value loop code, and the [exec] of the resulting
executable:
# Feed this Tcl proc a fortran expression
proc formake {e} {
global f
set t [subst -nocommand {
subroutine sayhello(x,r)
real a,r
r = $e
return
end
}]
set f [open sub.f w]
puts $f [string trim $t \n]
close $f
exec gcc -o fm sub.f main.c -lm
return [exec fm]
}
''([DKF]: Adapted the above to use [subst] for greater code clarity.)''
This, or an adapted, C program is needed in the current directory:
/* This is file: main.c */
#include
#include
extern void sayhello_(float *, float *);
int main(argc, argv)
int argc;
char *argv[];
{
float in, out;
float x, start, stop, incr;
if (argc == 1) {
start = -10.0;
stop = 10.0;
incr = 2.0;
} else exit(-1);
for (x=start; x<=stop; x+=incr) {
in = x;
sayhello_(&in,&out);
printf("%f %f\n", x, (float) out);
}
return(0);
}
Calling the formake routine in this case returns a formatted list of 21 X Y values, but it could return more.
Of course braces could be added to automatically create a Tcl list as result.
There is nothing against using these methods in [Bwise] blocks.
Note that this approach assumes running on a linux like or compatible (like maybe cygwin) system, with gcc present with fortran extensions, I tested on RH9, which worked fast.