evaltest.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {
  2. $ id: $
  3. Copyright (c) 2000 by Marco van de Voort ([email protected])
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright. (LGPL)
  7. Most basic test for TEvaluator class.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************
  12. }
  13. program EvalTest;
  14. {$AppType Console}
  15. Uses Classes,Symbolic, SysUtils;
  16. VAR Expr : TExpression;
  17. SymVars : TStringList;
  18. I : Longint;
  19. VarName : TStringList;
  20. Eval : TEvaluator;
  21. Vars : Array[0..1] OF ArbFloat;
  22. begin
  23. {Lets create in a nice equation. Totally nonsense. Don't try to
  24. make sense of it}
  25. Expr:=TExpression.Create('pi*sin(x-x0)+x^(t-1)+exp(x*t)+5');
  26. Writeln('Expression after parsing :',Expr.InfixExpr);
  27. {Hmm. But the user could have typed that in. Let's check if he used
  28. symbolic values}
  29. SymVars:=Expr.SymbolicValueNames;
  30. If SymVars.Count>0 Then
  31. For I:=0 TO SymVars.Count-1 DO
  32. Writeln(I:5,' ',Symvars[I]);
  33. {Assume the user selected X and T from above stringlist as our variables}
  34. VarName:=TStringList.Create;
  35. VarName.Add('X');
  36. VarName.Add('T');
  37. {Create the Evaluator Object}
  38. Eval:=TEvaluator.Create(VarName,Expr);
  39. {My HP48g provided this value for PI:}
  40. IF Symvars.IndexOf('PI')<>-1 THEN {If PI exists, then assume it is the
  41. circle radius vs diameter ratio}
  42. Eval.SetConstant('PI',3.14159265359);
  43. IF Symvars.IndexOf('X0')<>-1 THEN {Set X0 to Douglas' number}
  44. Eval.SetConstant('X0',42);
  45. {All this above looks slow isn't? It probably even is. Unit symbolic has
  46. evaluations as plus, not as target. The evaluation is built for
  47. fast repeated evaluations, not just one.
  48. However the Evaluate method is hopefully reasonably fast.
  49. Under FPC TEvaluator.Evaluate is about 600-700 assembler instructions,
  50. without operation on pointer trees and without recursion.
  51. If your compiler (and hopefully FPC too) can inline the math unit functions,
  52. the speed gain could be dramatic.}
  53. Writeln('Stackdepth needed for evaluation: ',eval.EvalDepth);
  54. FOR I:=1 TO 50 DO
  55. begin
  56. Vars[0]:=1/I *1.1;
  57. Vars[1]:=1/I*2;
  58. Writeln(VarName.Strings[0] + '=' + FloatToStrF(Vars[0], ffFixed, 4, 4) + ' ' +
  59. VarName.Strings[1] + '=' + FloatToStrF(Vars[1], ffFixed, 4, 4) + ' = ' +
  60. FloatToStrF(Eval.Evaluate(Vars), ffFixed, 4, 4));
  61. end;
  62. Eval.Free;
  63. Expr.Free;
  64. SymVars.Free;
  65. // VarName.Free; {Is freed by TEvaluator.Destroy. Should TEvaluator copy it?}
  66. Readln;
  67. end.
  68. {
  69. $Log$
  70. Revision 1.1 2002/12/15 21:01:22 marco
  71. Initial revision
  72. }