Formula Class Reference

[libkorigin Index] [libkorigin Heirarchy]


Representation of a Formula More...

#include </home/mh/korigin/korigin/doc/tmp/formula.h>

Public Members


Detailed Description

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.


Formula(const char*)

Construct formula from string/expression. The expression may not contain spaces!

Formula(const Formula&)

Copy constructor. Creates a deep copy, i.e. copies the expression and all local variables of the r.h.s. formula.

~Formula()

Destruct formula and local variables. Frees all local variables; global variables remain untouched.

Formula& operator=(const char*)

Assign string expression to a formula; local variables remain. This just changes the expression of a formula. The same as setExpression

Formula& operator=(const Formula&)

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.

double operator ()()

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.

Returns:
value of the expression.
See Also:
state

void setExpression(const char*)

Set the expression. This function redefines the expression, leaving all variables as they are.

See Also:
Formula::operator

const char* expression() const

Retrieve the expression.

Returns:
the expression of the formula.

static void setGlobalVariable(Variable)

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

See Also:
setLocalVariablegetGlobalVariables

void setLocalVariable(Variable)

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

See Also:
setGlobalVariablegetLocalVariables

static int getGlobalVariables(int max, Variable* v)

Retrieve all global variables. "v" must point to an (already allocated) array of variables, and "max" must give the size of that array.

Parameters:
max Size of buffer "v".
v Buffer to store the global variables in.
Returns:
Number of global variables in use.
See Also:
getLocalVariablessetGlobalVariable

int getLocalVariables(int max, Variable* v)

Retrieve all local variables. "v" must point to an (already allocated) array of variables, and "max" must give the size of that array.

Parameters:
max Size of buffer "v".
v Buffer to store the local variables in.
Returns:
Number of local variables in use.
See Also:
getGlobalVariablessetLocalVariable

static FI_LookupFunc setLookupFunction(FI_LookupFunc)

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.

Returns:
Pointer to old lookup function.

static FI_ErrorFunc setErrorFunction(FI_ErrorFunc)

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.

Returns:
Pointer to old error function.

static bool caseSensitive

Toggle case sensitivity for expressions and variables. Note that this won't toggle case sensitivity for table lookup, since this is implemented completely different.

int state()

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:-)

See Also:
Formula::operator

  • Author: Patrick Schemitz
  • Documentation generated by mh@jeff_clever on Thu Feb 5 14:15:25 MET 1998
Kdoc