[libkorigin Index] [libkorigin Heirarchy]
Representation of a Formula More...
#include </home/mh/korigin/korigin/doc/tmp/formula.h>
Representation of a Formula. A formula consists of an expression and a set of variables; some of the variables are global (shared by all formulas), others are local. Global variables are accessed via get/setGlobalVariable() and affect all formulas; local variables are accessed via get/setLocalVariable() and affect only the specified formula.
The expression which is to be evaluated can be accessed by the functions set/getExpression(). The expression is a math term, e.g. "sin(x)*cos(y)+0.5*z*z". Note that the expression may *not* contain spaces or tab stops!
A formula can be evaluated by the () operator, by placing a pair of brackets after an instance of class Formula.
Warning: internally, the formula interpreter uses exceptions to handle errors. The file formula.cc must be compiled with exception handling on.
Construct formula from string/expression. The expression may not contain spaces!
Copy constructor. Creates a deep copy, i.e. copies the expression and all local variables of the r.h.s. formula.
Destruct formula and local variables. Frees all local variables; global variables remain untouched.
Assign string expression to a formula; local variables remain. This just changes the expression of a formula. The same as setExpression
Assign a formula to another formula. Creates a deep copy, i.e. copies the expression and all local variables of the r.h.s. formula. The former left hand side formula is wiped out.
Evaluate formula. This operator evaluates the expression and returns the result. Note that the evaluation may take some time (depending on the expression, of course).
Two external functions are of interest here: the lookup function and the error function. While the latter is called on parse errors or in case an undefined variable is accessed, the lookup function is called when the parser finds a pattern "col(name,integer)". The name of the requested column is passed to the lookup function, along with the integer row number.
If the evaluation was successful, the state function will return zero.
Set the expression. This function redefines the expression, leaving all variables as they are.
Retrieve the expression.
Set global variable. This static member function defines/changes a global variable. If the variable already exists, it is redefined; if it doesn´t, it is newly defined. If the formula interpreter runs out of global variables, the error function is called.
A global variable is accessible for all formulas. (As opposed to global variables.)
Set local variable. This function defines/changes a local variable. If the variable already exists, it is redefined; if it doesn´t, it is newly defined. If the formula interpreter runs out of global variables, the error function is called.
A local variable is accessible only in the formula in which it is defined. (As opposed to global variables.)
Retrieve all global variables. "v" must point to an (already allocated) array of variables, and "max" must give the size of that array.
max | Size of buffer "v". |
v | Buffer to store the global variables in. |
Retrieve all local variables. "v" must point to an (already allocated) array of variables, and "max" must give the size of that array.
max | Size of buffer "v". |
v | Buffer to store the local variables in. |
Set lookup function for tables. The lookup function is invoked if the expression contains patterns "col(name,row)". Then, the lookup function is called, with "name" and "row" as parameters.
Note that the lookup function, in case it fails to look up the table's cell, should return NAN, not zero. This way, the function calling the evaluation operator can conclude from the result that something went wrong.
Set error report function. The error function is called by the formula evaluation. The default error handler prints out the message it gets passed, and terminates the program. Your error handler might instead open a message box, show the error message, and gracefully close down the evaluation. Note that the error handler may return (it does not *need* to terminate); in this case, the evaluation will be aborted, and state() function will return a non-zero value.
Toggle case sensitivity for expressions and variables. Note that this won't toggle case sensitivity for table lookup, since this is implemented completely different.
Check for errors during evaluation. If the expression could be evaluated successfully, the state() function returns zero; if there has been an error, a non-zero value is returned. (Unless, of course, the error handler function has terminated the program:-)
| Kdoc |