N-dimensional mathematical functions

U

user923005

(e-mail address removed) writes:
[...]> Eric:
   Ok, maybe it's not so complicated, but if there already is something
like that, why create it one more time? ;) And yes, I think you've
called it properly - "expression evaluator', I'll try to look for it..
   I'm not fixated on the "multi dimensional" aspect, that's just my
perspective of this problem. ;)
[...]
I'm still not 100% clear on what you mean by "multi dimensional".  Are
you merely referring to functions that take multiple arguments, or is
there more to it?
From his description, I think that he does not know how many
parameters will be passed from the end-user.
That is why I suggested either using arrays or a varadic function.

Aaa... We are mixing the ideas! I do not mean a _programming_
function, but _mathematical_ ! Like f(x)=x+1. This one, for example is
one-dimensional (one argument - x). f(x1, x2) = x1*x2^3 + 2 is a two-
dimensional, etc.

Then you will probably have to write a parser.
 
D

dj3vande

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top