PDA

View Full Version : Functions



ygfperson
04-16-2003, 05:27 PM
I'm struggling to think up a good way to define a function in C++. Currently I'm thinking of defining a function as a set of Expressions, and a domain for each.

Ie: 3x for x > 2
-x for x < 2

Which would imply that x=2 is undefined

Are there any holes in this argument? I want my function class to be upgradable.

Magos
04-16-2003, 07:42 PM
Eh, I assume that the obvious answer is:


int MyFunc(int Arg)
{
if(Arg > 2)
{
return (3 * Arg);
}
else if(Arg < 2)
{
return (-Arg);
}
else
{
return UNDEFINED;
}
}

But since you posted in GD and I guess you could've come up with that solution yourself, I guess I didn't quite get your question.

ygfperson
04-17-2003, 03:15 PM
I don't mean a C/C++ function in that sense. I mean a purely mathematical function, written as a class in C++.

Magos
04-17-2003, 04:15 PM
Originally posted by ygfperson
I don't mean a C/C++ function in that sense. I mean a purely mathematical function, written as a class in C++.
Now I'm even more confused.

Do you mean a class that can handle a general (mathematical) function for different domains, not just a predefined one (like mine above)?


typedef struct
{
int Start;
int End;
}DOMAIN;

typedef struct
{
char* ExpressionInStringFormOrWhatever;
}EXPRESSION;

typedef struct _FUNCTION_NODE
{
_FUNCTION_NODE* NextNode;
DOMAIN Domain;
EXPRESSION Expression;
}FUNCTION_NODE;

class FUNCTION
{
LinkedList<FUNCTION_NODE> AllDomain'sExpressions;
};

(A general idea)

ygfperson
04-17-2003, 05:11 PM
So, are functions basically just expressions and domains, or am I missing something?

Magos
04-18-2003, 04:48 AM
Originally posted by ygfperson
So, are functions basically just expressions and domains, or am I missing something?

Currently I'm thinking of defining a function as a set of Expressions, and a domain for each
Since you said you would define them like that I assumed that would be enough...

And that should be enough to define a (mathematical) function. You could add if the intervals are open or closed though.

Sang-drax
04-18-2003, 05:45 AM
You could do something like this:


double function1(double x)
{
return 3*x;
}
bool condition1(double x)
{
return x > 2;
}

double function2(double x)
{
return -x;
}
bool condition2(double x)
{
return x < 2;
}

typedef double (*SingleFunction)(double);
typedef bool (*Condition)(double);

typedef std::list< std::pair<SingleFunction , Condition> > Function;

And then when the function is called traverse the list for a matching condition.

It's very easy to do in Omicron:


function f(x) = 3x if x > 2
function f(x) = -x if x < 2

rahaydenuk
04-19-2003, 08:31 AM
A function (in mathematics) is a relation which uniquely associates members of one set with members of another set. More formally, a function from A to B is an object f such that every a of the set A is uniquely associated with an object f(a) of the set B. A function is therefore a many-to-one (or sometimes one-to-one) relation. The set A of values at which a function is defined is called its domain, while the set B of values that the function can produce is called its range.

So, in order to fully define a function you need to provide a definition of the set A (the domain), along with the mapping between it and the set B (the range), which is f (the function). You don't really need to explicitly provide a definition of B (the range) as it is defined implicitly by A and f. This is what you have already come up with yourself anyway.

So, in your class, you need some kind of expression syntax to define the domain of your function, which could be for example x < 4, or 'x of the set of Z+'. Does whatever you're programming support just real-valued functions, or does it support complex functions also? Does it support vector functions, matrix functions, tensor functions? You need to consider this before you design some sort of syntax for defining the domain of your functions and for defining the functions themselves.

Good Luck,