#include <Function.h>
Public Member Functions | |
UserFunction (string cat_, string name_, string formula_, bool is_local=true, int argc_=-1, string title_="", string descr_="", int max_argc_=0, bool is_active=true) | |
UserFunction (const UserFunction *function) | |
void | set (const ExpressionItem *item) |
ExpressionItem * | copy () const |
string | formula () const |
string | internalFormula () const |
int | calculate (MathStructure &mstruct, const MathStructure &vargs, const EvaluationOptions &eo) |
void | setFormula (string new_formula, int argc_=-1, int max_argc_=0) |
void | addSubfunction (string subfunction, bool precalculate=true) |
void | setSubfunction (size_t index, string subfunction) |
void | delSubfunction (size_t index) |
void | clearSubfunctions () |
size_t | countSubfunctions () const |
void | setSubfunctionPrecalculated (size_t index, bool precalculate) |
const string & | getSubfunction (size_t index) const |
bool | subfunctionPrecalculated (size_t index) const |
int | subtype () const |
Protected Attributes | |
string | sformula |
string | sformula_calc |
vector< string > | v_subs |
vector< bool > | v_precalculate |
User functions are functions defined using expression strings, representing mathematical formulas.
The expression/formula of a function is basically a normal expression with placeholders for arguments. These placeholders consists of a backslash and a letter — x, y, z for the 1st, 2nd and 3rd arguments and a to u for argument 4 to 24. They are replaced by entered arguments when a function is calculated. The placeholders naturally also decide the number of arguments that a function requires. For example the function for triangle area ("base * height / 2") has the name triangle and the formula "(\x*\y)/2", which gives that "triangle(2, 3)" equals "(2*3) / 2" and returns "3" as result. An argument can be used more than one time and all arguments must not necessarily be in order in the formula.
Additionally, optional arguments can be put in the formula with upper-case (X, Y, Z, ...) instead of lower-case letters (x, y, z, ...). The default value can be put in brackets after the letter (ex. "\X{2}"). The default value may be omitted and is then zero. All additional arguments after an optional argument must also be optional.
To simplify the formula and make it more efficient, subfunctions can be used. These works just like the main formula, using the arguments of it. Subfunctions are referenced in the formula using ('', '', '', ...). Even though it would be quite meaningless, the formula for triangle function could for example have a subfunction "\x*\y" and the formula "\1/2". Subfunctions must be added before the main formula is set.
string UserFunction::formula | ( | ) | const |
Returns the external representation of the formula.
string UserFunction::internalFormula | ( | ) | const |
Returns the internal representation of the formula.
int UserFunction::calculate | ( | MathStructure & | mstruct, | |
const MathStructure & | vargs, | |||
const EvaluationOptions & | eo | |||
) | [virtual] |
The main function for subclasses to reimplement. Calculates a value from arguments in vargs and puts it in mstruct.
This function expects the number of arguments to be equal to the maximum number of arguments, and checked by the argument definitions.
If the return value is negative, then argument -(return value) has been evaluated in mstruct. If -(return value) is greater than max arguments, then mstruct is a vector of evaluated argument values.
[out] | mstruct | Structure that is set with the result of the calculation. |
vargs | Arguments passed to the mathematical function. | |
eo | Evaluation options. |
Reimplemented from MathFunction.
void UserFunction::setFormula | ( | string | new_formula, | |
int | argc_ = -1 , |
|||
int | max_argc_ = 0 | |||
) |
Sets the formula of the mathematical function.
new_formula | Formula/expression. | |
arc_ | Minimum number of arguments or -1 to read from formula. | |
max_argc_ | Maximum number of arguments (ignored if argc_ < 0) |
void UserFunction::setSubfunction | ( | size_t | index, | |
string | subfunction | |||
) |
Sets the formula for a subfunction.
index | Index (starting at 1). | |
subfunction | Formula/expression. |
int UserFunction::subtype | ( | ) | const [virtual] |
Returns the subtype of the mathematical function, corresponding to which subsubclass the object belongs to.
Reimplemented from MathFunction.