1. ## Functions

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.

2. Eh, I assume that the obvious answer is:
Code:
```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.

3. I don't mean a C/C++ function in that sense. I mean a purely mathematical function, written as a class in C++.

4. 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)?
Code:
```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
{
};```
(A general idea)

5. So, are functions basically just expressions and domains, or am I missing something?

6. 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.

7. You could do something like this:
Code:
```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:
Code:
```function f(x) = 3x   if x > 2
function f(x) = -x   if x < 2```

8. 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,