DEFUNC(3) C LIBRARY FUNCTIONS DEFUNC(3)
NAME
defunc -- a portable C library package for runtime function
constructing.
DESCRIPTION
defunc (Dynamic Expressible Function Constructing) is a
portable C library package for constructing functions from
runtime inputted expressions.
defunc library function dfopen() accepts an expression
string and, on success, returns a function pointer. e.g.
#include <defunc.h>
foo()
{
double (*fnctptr)();
....
fnctptr = dfopen("(x*x+y*y)^0.5");
....
};
The function dfopen() is reentrant. That is, you can repeat-
edly call dfopen() without worry about the new result may
overlap or damage the old one, provided they are assigned to
different lvalues. e.g.
double (*fnctptr1)();
double (*fnctptr2)();
...
fnctptr1 = dfopen("exp(-x*x)");
fnctptr2 = dfopen("exp(-x)*sin(x)");
...
Then, fnctptr1 and fnctptr2 will point to two different
functions.
Function dfopen() parses the expression be passed based on
tokens it recognized. Except for numerical string constant
tokens(i.e. anonymous constant tokens) and 14 build in
tokens "+", "-", "*", "/", "^", "**", "(", ")", ",", "=",
";", "[", "]" and "$", all other tokens are external.
External tokens include argument tokens, function tokens and
named constant tokens. They are stored in a global token
table. They can be set/reset or appended/deleted statically
as well as dynamically. e.g.
nameargu("re", "im");
This will reset argument tokens to "re" and "im"(the default
are "x" and "y").
defunc 1.3 Last change: January 1994 1
DEFUNC(3) C LIBRARY FUNCTIONS DEFUNC(3)
namefnct("log", log);
namefnct("ln" , log);
Here, the external function log() has been added to the
token list with 2 alias names "log" and "ln".
namecnst("pi", 2.0*asin(1.0));
namecnst("PI", 2.0*asin(1.0));
This will add a named constant token to the list with alias
names "pi" and "PI" and value 3.1415926... .
clrcnst("PI");
This will delete the constant token "PI" from the global
token table. After an external token has been put into the
global token list, it can be used in expressions passed to
dfopen(). e.g. After you put the exponential function exp()
into token table with
namefnct("exp", exp);
/* add external function exp() to the token table */
Than you can use it to construct a gaussian function dynami-
cally as
double (*gauss)();
...
gauss = dfopen("exp(-x*x)");
Argument tokens, named constant tokens and dynamically con-
structed functions can be reset or added to the token list
dynamically. This can be done directly from the expression
passed to dfopen() function. e.g.
fnctptr = dfopen("Rho(re, im)=(re*re+im*im)^0.5");
This will reset the argument tokens to "re" and "im", return
a duplex function pointer and add this function pointer to
the global token list with name "Rho".
dfopen("gauss(x)=exp(-x*x)");
This sets the first argument tokens to "x" and put the
return function pointer to the global token table with name
"gauss" while the return function pointer is not assigned to
any lvalue. Now, the token "gauss" is recognizable by defunc
and you can in turn use it to construct new function. Such
as
dfopen("gauss2d(x,y)=gauss(x)*gauss(y)");
defunc 1.3 Last change: January 1994 2
DEFUNC(3) C LIBRARY FUNCTIONS DEFUNC(3)
will return a 2 dimension gaussian function and put it to
token table with name "gauss2d". If you don't want this 2
dimension gaussian function be putted into the token table
but you still want reset the argument tokens, for example to
"a", "b", you can use
fnctptr = dfopen("$(a, b) = gauss(a)*gauss(b)");
or
fnctptr = dfopen("$gauss(a, b) = gauss(a)*gauss(b)");
Here, fnctptr is a function pointer to accept the returned
value from dfopen().
Named constant tokens can also be added to the token list
from expression passed to dfopen(). e.g.
fnctptr = dfopen("pi = 4*atan2(1,1)");
This will return a constant function and add a named con-
stant token to the token list with value 3.1415926... and
name "pi".
SEE ALSO
dfopen(3)
AUTHOR
Ke Jin
Physics Department
Queen's University
Kingston, Ontario
Canada K7L 3N6
jinke@sparky.phy.queensu.ca
BUGS
Report bugs of defunc library to the author by email.
defunc 1.3 Last change: January 1994 3