On Feb 20, 1:53 pm, (e-mail address removed) wrote:
Then you will probably have to write a parser.
It's not clear from the text you replied to that he was referring to
the form of the function description and not merely to the content.
If he can get away with requiring the user to write it in a form like
--------
f1 := x1 1 +
f2 := x2 x2 3 ^ * 2 +
--------
then an RPN "parser" is nearly trivial to write.
If he does need to accept standard mathematical notation it gets a
little bit harder.
In either case it will probably be necessary to compile the input into
an internal representation that lets the computation be done for given
values of x_i. (Though it might be possible to do it by just stuffing
the values for x_i into the stored-variables table and then feeding the
original input to an expression evaluator that supports variables.)
As far as writing the code, if you want a single function (C function)
to handle functions (math functions) on arbitrary dimensional spaces,
it may be easiest to pass the argument vector as an array of values of
suitable type, something like:
--------
double eval_func(struct compiled_function *func,size_t ndim,double *vec)
{
double result;
if(func->arg_dimension != ndim)
{
whine("Dimension mismatch!");
return 1.0/0.0; /*attempt to generate NaN*/
}
/*Now x1 is vec[0], xN is vec[N-1]*/
/*Here's where the code to do the evaluation goes*/
return result;
}
--------
Then in your front-end code, you can get the dimension of the space
you're working on (either by asking the user directly or by working it
out from other information) and allocate space for an array of that
size for the input vector, and give that vector to eval_func as the vec
argument.
Then as long as you have a sensible way of identifying which names in
the function correspond to which component of the argument (using
x1...xN is a perfectly good way to do that), you can handle functions
(math functions) on as many dimensions as your user wants (and as you
have memory to allocate bookkeeping data for) and use the same code to
evaluate them.
(Using a variadic function, as I think I saw suggested elsethread,
allows a carefully written evaluator to handle arbitrary dimensions,
but a given call to the evaluator will always have the same number of
arguments, so the code that calls it can't accomodate dimensions that
aren't allowed for when you write it; using an array with dynamic
allocation lets it be (more or less) arbitrary.)
dave