FORMULA.ZIP

TFormula : - non-visual component
           - Delphi 2
           - Version 1.0
           - Freeware, NO warranty what so ever
             This means you can use and distribute this component free
             of any charge. If you do use it in your application you
             have to state my copyright notice, saying that you used
             TFormula and where to get it.

             Any comments and BUG reports welcome.

           - Copyright Frank Kroeger

Author      : Frank Kroeger
eMail       : Frank.Kroeger@pop.gun.de
BUG-Reports : BUG@salem.gun.de (subject TFORMULA)



Description :
-------------

With TFormula you can easily use mathematical functions in your applications.
You can change the formula at run time and define up to 254 parameters with
distinguishable names. 

for example : Formula1.Formula:='sin(5*x+3*sqr(y)/pi)';

Formula1.Calculate will then give you the result of this function.
(of course you have to define the parameternames and values first)


Operators and functions :
-------------------------

You can use following operators and functions :

 pi         : 3.1415926....
 +
 -
 *
 /
 ^
 sin()      : Sinus of argument
 cos()      : Cosinus of argument
 tan()      : Tangens of argument
 arcsin()   : Arcus-sinus of argument
 arccos()   : Arcus-cosinus of argument
 arctan()   : Arcus-tangens of argument
 cotan()    : Arcus-cotangens of argument
 log10()    : logarithm of argument to base 10
 log2()     : logarithm of argument to base 2
 ln()       : natural logarithm (logarithm of argument to base e)
 exp()      : e to the power of argument
 abs()      : absolute of argument |argument|
 step()     : step-function (argument <0 : step=0,  argument>=0 : step=1 )
 rect()     : rectangle-function ( abs(argument) <= 1/2 : rect=1, else 0 )
 sqrt()     : square root of argument
 sqr()      : square of argument
 sign()     : sign-function (arg<0 : sign=-1, arg=0 : sign=0, arg>0 : sign=1
 sinh()     : Sinus hyperbolicus of argument
 cosh()     : Cosinus hyperbolicus of argument
 tanh()     : Tangens hyperbolicus of argument
 arcsinh()  : Arcus sinus hyperbolicus of argument
 arccosh()  : Arcus cosinus hyperbolicus of argument
 arctanh()  : Arcus tangens hyperbolicus of argument
     

The argument of any function HAS to be in brackets, or else you will get
an exception/errorCode.
Any Spaces are ignored.
TFormula is not case sensitive.
Constants have to start with a optional minus and then a number.
e.g. : 0.982 is valid, -2.901 is valid, -.192 or .99 are not valid.



Properties :
------------

AngleMode : DEG or RAD

        This property affects the behaviour of trigonometric functions.
        If set to DEG the argument of sin,cos,tan and cotan is expected in
        degrees. If set to RAD the argument is expected in multiples of PI.
        The Result of arcsin,arccos,arctan will be in degrees if set to DEG,
        else in multiples of PI.


Formula : AnsiString

        This is where you store the formula.


FormulaRPN : AnsiString

        Read only. If the formula is intialized FormulaRPN holds the formula
        in reverse polish notation (RPN).


Parameters : TStrings

        Parameters holds the names of the parameters. You can use up to
        254 parameters. There is no limitation to the name, expect any
        combination of functionnames or operators, or leading numbers.
        There is no test if Parameters holds more than 254 names, if you
        do use more, it will probably end up in an exception or funny
        behaviour. (not tested)


Datatypes :
-----------

type TFormulaError = record
         Code : Byte;
         Position : LongInt;
         Text : AnsiString;
       end;


Public procedures/functions :
-----------------------------

procedure Setparametervalues(const A:array of Double);
function Initformula:TFormulaError;
function Calculate(const A:array of Double):Double;
function Calculate2:Double;
procedure ClearStack;


Setparametervalues :

        Use this procedure to set the values of the parameters.
        Where the values are ordered in the same way the parameternames are
        ordered.
        for example : Formula1.Setparametervalues( [1.0123,pi,99182.091] );


InitFormula :

        After assigning a formula and parameternames you can use this
        procedure to initialize the formula. If any errors occur during
        this process InitFormula returns a record of TFormulaError,
        where Code is the error-code, Position is the position in the
        formula-string where the error occured, Text is a small description
        of the error. (example below)


Calculate :

        If InitFormula is not used, Calculate will. Any errors will result
        in an exception.
        Calculate returns the result of formula using the values stored
        in the argument of Calculate.


Calculate2 :

        Calculate2 returns the result of formula using the values set by
        Setparametervalues.





Usage :
-------

Just put TFormula on your Form and set the properties.


Example of TFormula in an application :

procedure TForm1.Button1Click(Sender:TObject);
var
   x,y : Double;
   Error : TFormulaError;
begin
   Formula1.Formula:=Edit1.Text;    // for example : sin(x)
   Formula1.Parameters.Clear;
   Formula1.Parameters.Add('x');

   Error:=Formula1.InitFormula;

   if Error.Code<>0 then
     begin
      MessageDlg('Error : '+intToStr(Error.Code)+
                 ' at '+intToStr(Error.Position)+#1310+
                 'Text : '+Error.Text+#13#10,
                 mtError,[mbok],0);
     end else
      MessageDlg('x = 2*pi'+#13#10+
                 Edit1.Text+' = '+
                 FloatToStrF(Formula1.Calculate([2*pi]),ffFixed,20,4),
                 mtInformation,[mbok],0);
end;
