Unit MathExpr

DescriptionusesClasses, Interfaces, Objects and RecordsFunctions and ProceduresTypesConstantsVariables

Description

Typy i niezbedne funkcje do operowania na wyrazeniach matematycznych - obiektach klasy TMathExpr.

Prosty przyklad : deklaracja

   Expr := TMathFunction.Create(fkAdd, [
       TMathFunction.Create(fkSin, [TMathVar.Create('x')]),
       TMathConst.Create(10),
       TMathConst.Create(1)
     ]);
  

sprawia ze expr reprezentuje wyrazenie sin(x) + 10 + 1.

Mozna nastepnie wywolac expr.value(varfunc) jako parametr podajac funkcje ktora zamienia nazwy zmiennych na ich wartosci (jest oczywiscie gwarantowane ze ta funkcja zostanie zapytana tylko o zmienne obecne w danym wyrazeniu, w tym przypadku - - wiemy ze na pewno tylko o 'x'). Jako wynik otrzymamy wartosc tego wyrazenia.

Wazna uwaga - obiekt TMathFunction zapamietuje sobie wyrazenia argumentow ktore same sa obiektami TMathExpr i zwalnia je gdy sam jest zwalniany. Tym samym mozemy na koncu zrobic expr.Free i wszystkie obiekty ktore sa jego czescia zostana zwolnione automatyczne.

Jasne zastosowanie klasy TMathExpr jest do budowy parserow ktore musza zbudowac sobie wyrazenie matematyczne w czasie runtime. Patrz unit MathExprParser.

uses

Overview

Classes, Interfaces, Objects and Records

Name Description
Class TMathExpr  
Class TObjectsList_1  
Class TMathExprList  
Class TMathConst  
Class TMathVar  
Class TMathFunction InfixOperatorName: name of this function for use in expressions like "arg_1 InfixOperatorName arg_2 ...
Class EWrongMathExpr  
Class EWrongMathFunction  
Class EUndefinedVariable  

Functions and Procedures

function AddFloat(a, b: Float): Float;
function SubtractFloat(a, b: Float): Float;
function MultiplyFloat(a, b: Float): Float;
function DivideFloat(a, b: Float): Float;
function ReturnSingleVariable(const VariableName: string): Float;
function ReturnNoVariable(const VariableName: string): Float;

Types

TVariableValueFunc = function(const variable_name: string): Float;
TEvalMathExprFunc = function(a, b: Float): Float;
TObjectsListItem_1 = TMathExpr;
TObjectsListIsSmallerFunction_1 = function (const A, B: TObjectsListItem_1): boolean of object;
TFunctionKind = (...);

Constants

FunctionKinds : array[TFunctionKind]of record ArgsCount: integer; Name: string; FunctionName: string; InfixOperatorName: string; end = ( (ArgsCount:-1; Name:'add (+)'; FunctionName:''; InfixOperatorName:'+'), (ArgsCount:-1; Name:'subtract (-)'; FunctionName:''; InfixOperatorName:'-'), (ArgsCount:-1; Name:'multiply (*)'; FunctionName:''; InfixOperatorName:'*'), (ArgsCount:-1; Name:'division (/)'; FunctionName:''; InfixOperatorName:'/'), (ArgsCount:1; Name:'negation (unary -)'; FunctionName:''; InfixOperatorName:''), (ArgsCount:2; Name:'modulo (%)'; FunctionName:''; InfixOperatorName:'%'), (ArgsCount:1; Name:'sinus'; FunctionName:'sin'; InfixOperatorName:''), (ArgsCount:1; Name:'cosinus'; FunctionName:'cos'; InfixOperatorName:''), (ArgsCount:1; Name:'tangens'; FunctionName:'tan'; InfixOperatorName:''), (ArgsCount:1; Name:'cotangens'; FunctionName:'cotan'; InfixOperatorName:''), (ArgsCount:1; Name:'arcSinus'; FunctionName:'arcSin'; InfixOperatorName:''), (ArgsCount:1; Name:'arcCosinus'; FunctionName:'arcCos'; InfixOperatorName:''), (ArgsCount:1; Name:'arcTangens'; FunctionName:'arcTan'; InfixOperatorName:''), (ArgsCount:1; Name:'arcCotangens'; FunctionName:'arcCotan'; InfixOperatorName:''), (ArgsCount:1; Name:'sinh'; FunctionName:'sinh'; InfixOperatorName:''), (ArgsCount:1; Name:'cosh'; FunctionName:'cosh'; InfixOperatorName:''), (ArgsCount:1; Name:'tanh'; FunctionName:'tanh'; InfixOperatorName:''), (ArgsCount:1; Name:'cotanh'; FunctionName:'cotanh'; InfixOperatorName:''), (ArgsCount:1; Name:'logarithm (base 2)'; FunctionName:'log2'; InfixOperatorName:''), (ArgsCount:1; Name:'logarithm (base e)'; FunctionName:'ln'; InfixOperatorName:''), (ArgsCount:2; Name:'logarithm'; FunctionName:'log'; InfixOperatorName:''), (ArgsCount:1; Name:'power (base 2)'; FunctionName:'power2'; InfixOperatorName:''), (ArgsCount:1; Name:'power (base enat)'; FunctionName:'exp'; InfixOperatorName:''), (ArgsCount:2; Name:'power'; FunctionName:'power'; InfixOperatorName:'ˆ'), (ArgsCount:1; Name:'sqr (square)'; FunctionName:'sqr'; InfixOperatorName:''), (ArgsCount:1; Name:'sqrt (square root)'; FunctionName:'sqrt'; InfixOperatorName:''), (ArgsCount:1; Name:'signum'; FunctionName:'sgn'; InfixOperatorName:''), (ArgsCount:1; Name:'abs (absolute value)'; FunctionName:'abs'; InfixOperatorName:''), (ArgsCount:1; Name:'ceil'; FunctionName:'ceil'; InfixOperatorName:''), (ArgsCount:1; Name:'floor'; FunctionName:'floor'; InfixOperatorName:''), (ArgsCount:2; Name:'greater (>)'; FunctionName:'greater' ; InfixOperatorName:'>'), (ArgsCount:2; Name:'lesser (<)'; FunctionName:'lesser' ; InfixOperatorName:'<'), (ArgsCount:2; Name:'greaterEq (>=)'; FunctionName:'greaterEq'; InfixOperatorName:'>='), (ArgsCount:2; Name:'lesserEq (<=)'; FunctionName:'lesserEq' ; InfixOperatorName:'<='), (ArgsCount:2; Name:'equal (=)'; FunctionName:'equal' ; InfixOperatorName:'='), (ArgsCount:2; Name:'not equal (<>)'; FunctionName:'notEqual' ; InfixOperatorName:'<>'), (ArgsCount:2; Name:'or (alternative)'; FunctionName:'or'; InfixOperatorName:''), (ArgsCount:2; Name:'and (conjunction)'; FunctionName:'and'; InfixOperatorName:''), (ArgsCount:1; Name:'not (logical negation)'; FunctionName:'not'; InfixOperatorName:'') );

Variables

SingleVariable: Float;
SingleVariableName: string = 'x';

Description

Functions and Procedures

function AddFloat(a, b: Float): Float;
 
function SubtractFloat(a, b: Float): Float;
 
function MultiplyFloat(a, b: Float): Float;
 
function DivideFloat(a, b: Float): Float;
 
function ReturnSingleVariable(const VariableName: string): Float;
 
function ReturnNoVariable(const VariableName: string): Float;

ReturnNoVariable zawsze rzuca wyjatek EUndefinedVariable, bez wzgledu na VariableName. Przydatne gdy chcesz obliczyc wartosc wyrazenia bez zmiennych.

Types

TVariableValueFunc = function(const variable_name: string): Float;
 
TEvalMathExprFunc = function(a, b: Float): Float;
 
TObjectsListItem_1 = TMathExpr;
 
TObjectsListIsSmallerFunction_1 = function (const A, B: TObjectsListItem_1): boolean of object;
 
TFunctionKind = (...);

When you are extending TFunctionKind type, remember that you will also have to extend FunctionKinds array and implement this function type in TMathFunction.Value (this is important since failure to do the second thing will NOT be reported as compile-time error).

Values
  • fkAdd:
  • fkSubtract:
  • fkMultiply:
  • fkDivide:
  • fkNegate:
  • fkModulo:
  • fkSin:
  • fkCos:
  • fkTan:
  • fkCotan:
  • fkArcSin:
  • fkArcCos:
  • fkArcTan:
  • fkArcCotan:
  • fkSinH:
  • fkCosH:
  • fkTanH:
  • fkCotanH:
  • fkLog2:
  • fkLn:
  • fkLog:
  • fkPower2:
  • fkExp:
  • fkPower:
  • fkSqr:
  • fkSqrt:
  • fkSgn:
  • fkAbs:
  • fkCeil:
  • fkFloor:
  • fkGreater:
  • fkLesser:
  • fkGreaterEq:
  • fkLesserEq:
  • fkEqual:
  • fkNotEqual:
  • fkOr:
  • fkAnd:
  • fkNot:

Constants

FunctionKinds : array[TFunctionKind]of record ArgsCount: integer; Name: string; FunctionName: string; InfixOperatorName: string; end = ( (ArgsCount:-1; Name:'add (+)'; FunctionName:''; InfixOperatorName:'+'), (ArgsCount:-1; Name:'subtract (-)'; FunctionName:''; InfixOperatorName:'-'), (ArgsCount:-1; Name:'multiply (*)'; FunctionName:''; InfixOperatorName:'*'), (ArgsCount:-1; Name:'division (/)'; FunctionName:''; InfixOperatorName:'/'), (ArgsCount:1; Name:'negation (unary -)'; FunctionName:''; InfixOperatorName:''), (ArgsCount:2; Name:'modulo (%)'; FunctionName:''; InfixOperatorName:'%'), (ArgsCount:1; Name:'sinus'; FunctionName:'sin'; InfixOperatorName:''), (ArgsCount:1; Name:'cosinus'; FunctionName:'cos'; InfixOperatorName:''), (ArgsCount:1; Name:'tangens'; FunctionName:'tan'; InfixOperatorName:''), (ArgsCount:1; Name:'cotangens'; FunctionName:'cotan'; InfixOperatorName:''), (ArgsCount:1; Name:'arcSinus'; FunctionName:'arcSin'; InfixOperatorName:''), (ArgsCount:1; Name:'arcCosinus'; FunctionName:'arcCos'; InfixOperatorName:''), (ArgsCount:1; Name:'arcTangens'; FunctionName:'arcTan'; InfixOperatorName:''), (ArgsCount:1; Name:'arcCotangens'; FunctionName:'arcCotan'; InfixOperatorName:''), (ArgsCount:1; Name:'sinh'; FunctionName:'sinh'; InfixOperatorName:''), (ArgsCount:1; Name:'cosh'; FunctionName:'cosh'; InfixOperatorName:''), (ArgsCount:1; Name:'tanh'; FunctionName:'tanh'; InfixOperatorName:''), (ArgsCount:1; Name:'cotanh'; FunctionName:'cotanh'; InfixOperatorName:''), (ArgsCount:1; Name:'logarithm (base 2)'; FunctionName:'log2'; InfixOperatorName:''), (ArgsCount:1; Name:'logarithm (base e)'; FunctionName:'ln'; InfixOperatorName:''), (ArgsCount:2; Name:'logarithm'; FunctionName:'log'; InfixOperatorName:''), (ArgsCount:1; Name:'power (base 2)'; FunctionName:'power2'; InfixOperatorName:''), (ArgsCount:1; Name:'power (base enat)'; FunctionName:'exp'; InfixOperatorName:''), (ArgsCount:2; Name:'power'; FunctionName:'power'; InfixOperatorName:'ˆ'), (ArgsCount:1; Name:'sqr (square)'; FunctionName:'sqr'; InfixOperatorName:''), (ArgsCount:1; Name:'sqrt (square root)'; FunctionName:'sqrt'; InfixOperatorName:''), (ArgsCount:1; Name:'signum'; FunctionName:'sgn'; InfixOperatorName:''), (ArgsCount:1; Name:'abs (absolute value)'; FunctionName:'abs'; InfixOperatorName:''), (ArgsCount:1; Name:'ceil'; FunctionName:'ceil'; InfixOperatorName:''), (ArgsCount:1; Name:'floor'; FunctionName:'floor'; InfixOperatorName:''), (ArgsCount:2; Name:'greater (>)'; FunctionName:'greater' ; InfixOperatorName:'>'), (ArgsCount:2; Name:'lesser (<)'; FunctionName:'lesser' ; InfixOperatorName:'<'), (ArgsCount:2; Name:'greaterEq (>=)'; FunctionName:'greaterEq'; InfixOperatorName:'>='), (ArgsCount:2; Name:'lesserEq (<=)'; FunctionName:'lesserEq' ; InfixOperatorName:'<='), (ArgsCount:2; Name:'equal (=)'; FunctionName:'equal' ; InfixOperatorName:'='), (ArgsCount:2; Name:'not equal (<>)'; FunctionName:'notEqual' ; InfixOperatorName:'<>'), (ArgsCount:2; Name:'or (alternative)'; FunctionName:'or'; InfixOperatorName:''), (ArgsCount:2; Name:'and (conjunction)'; FunctionName:'and'; InfixOperatorName:''), (ArgsCount:1; Name:'not (logical negation)'; FunctionName:'not'; InfixOperatorName:'') );

This is an array that specifies some globally-useful properties of every function (defined as fkXxx constant inside TFunctionKind type).

It also specifies some things about how such function can be specified as a text – this is useful for both converting TMathExpr to a string (using TMathExpr.ToString method) and converting a string to TMathExpr (using ParseMathExpr function from MathExprParser).

Note that not every property needed by MathExprParser is encoded in this array. Stating it shortly, you cannot fully control behaviour of MathExprParser by only modifying FunctionKinds array. For some more advanced control over MathExprParser (e.g. adding new operators, changing priority of operators) you will just have to modify MathExprParser unit. I may improve this with time.

For now the only functions that can be fully controlled from FunctionKinds are functions with InfixOperatorName = '' and only FunctionName <> ''. When you add or modify such function, you don't have to do anything inside MathExprParser, everything will just work.

Variables

SingleVariable: Float;

funkcja ReturnVariableX zwraca SingleVariable gdy VariableName = SingleVariableName, wpp. rzuca wyjatek EUndefinedVariable. Przydatne i wygodne gdy chcesz napisac prosty program ktory uzywa Expression z tylko jedna zmienna, i nie przeszkadza ci ze SingleVariable[Name] to zmienne globalne (wiec mozesz naraz uzywac tylko jednego wyrazenia z ReturnSingleVariable).

SingleVariableName: string = 'x';
 

Generated by PasDoc 0.10.0 on 2008-02-25 00:00:36