Recent

Author Topic: Math Expression parser(s)  (Read 534 times)

guest64429

  • Guest
Math Expression parser(s)
« on: August 23, 2019, 09:48:21 am »
Hello everyone,,
I have playing a bit (read a very little) with the TFPExpressionParser. I have a few open questions about it.

Where I could find a list of internal operators it do recognize (sin(), sqrt(),sum() etc..) since for my surprise I can not figure out how it handles the exponent (so far search is not my friend on this). I was in wrong impression that it do parse all the unit: math operators.
Or is there a bug that crashes it with the exponent. Which is so basic mathematical operation that I'm really surprised I need to ask.  :o

I'm also in the impression that the TFPExpressionParser do have a build in support for user defined mathematical operations (sin(x), cot(x) etc.) is this a false assumption.

Is there a mature unit that does have a solver which can handle numerically equations in form of:
EQ: a/sqrt(b)=sqrt(c) where a=1, b=1 solve unknown c
Marcos symbolic unit do something, but I'm in impression that it is really in in definitive beta or POC phase, or is it just a wrong impression.

At the moment Michel Deslierres "Simple Parser" seems to be much more mature and better documented than TFPExpression parser (I just did find it late last night so I have only read the documentation PDF).  https://www.sigmdel.ca/michel/program/fpl/parser/parser_fpc_en.html 

My intentions is a build a small equation library and solver, maybe open source project if I get it rolling. Those familiar with the HPs RPL machines do know what I'm after. The basic concept I'm decided so far is that the equations are handled as a files, there will be two storage files per equation one ascii file containing the variables, units, equation, description and "copyright" information. The second file will be a description picture.
I'm not decided how the variables will be handled, do I use a separate file per directory or some other method. However the values need to be persistent (for chained solving). The database will be filesystem based because I do want a system that is fully transparent to end user and the application needs to be fully standalone.
« Last Edit: September 06, 2019, 10:43:10 am by marcov »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7499
Re: Math Expression parser(s)
« Reply #1 on: August 23, 2019, 10:58:28 am »
Parsers and solvers are different things. One can use the output of the other, but still.

The original symbolic (2002-2003)was just about building a rpn evaluable expression, to make repeatedly evaluating that faster (to draw graphs). Also with an ability to generate dynamically gui to ask for the constants (hence the options to get a list of symbols etc)

Later I got a bit into the same matter as you (solvers), and since I already had the derivation routine (as gadget side effect of walking the parse tree and playing with recursion), so I looked into it.

Generic solvers, that just have one entry point for the function, and auto-determine which internal solver to call (depending on the form of the EQ) are even rarer. Both for speed as for extremes and other boundary condition checks. Most will have solvers for specific types of eqs, with some general solver as fall back.

But doing this naively will be slow, and also error handling is a problem. To ease this I decided to focus on a first general solver first, e.g. Newton. I decided to  calc the newton iteration eq (X-f/f' ) analytically and then first optimize/simplify it. IOW I had hoped to do function analysis and extremes symbolically first. Maybe even codegeneration in a later stage.

But this was all play after the original task (graphs) that I really needed finished, I got stuck in all that (the simplifier most notably) and shelved it.

Note that's also why initially it didn't have user functions. You can't differentiate user functions, making solving more difficult to optimize.

Later (2008-2009) I needed user funcs and particularly boolean logic support, I went to do a cleaned up second version with user functions. But the need went away, so I never finished it.

This is all more than a decade before TPFExpressionParser, which I don't know. Never used it.

P.s. apropos knowing HP calcs, why do you think symbolic has RPN support? :-)
« Last Edit: August 23, 2019, 11:10:01 am by marcov »

dbannon

  • Hero Member
  • *****
  • Posts: 754
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Math Expression parser(s)
« Reply #2 on: August 23, 2019, 11:01:20 am »
I am not sure the expression parser handles replacable var directly.  Its a parser, not a solver.

Your expression => a/sqrt(b)=sqrt(c)     a=1 b=1
Can be transposed to => (a/sqrt(b))^2 = c
Substitute in your vars => (1/sqrt(1))^2 = c

I have used the Expression Parser in tomboy-ng notes, if I paste that expression into it, it calculates c to be 1.

So, the parser can handle (1/sqrt(1))^2 without problems, you need to provide the code to put values to a and b

David
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7499
Re: Math Expression parser(s)
« Reply #3 on: August 23, 2019, 11:07:50 am »
Ah ok. missed that it was a relative simple case (I was thinking rootfinders).

For that eq indeed you don't need a rootfinder, just a simplifier that moves all constants. to one side. Maybe you don't even need that.

That might be within the realm of an expression solver indeed. Still getting the x= <expression> notation is already not simple for even moderately complex eqs.


wp

  • Hero Member
  • *****
  • Posts: 6319
Re: Math Expression parser(s)
« Reply #4 on: August 23, 2019, 06:42:11 pm »
I was in wrong impression that it do parse all the unit: math operators.
Yes. TFPExpressionParser is very general, not only for math expressions. Therefore it supports only the math functions of the system unit, e. g. abs, sqr, sqrt, sin, arctan, exp, ln, but not cos, not arcsin. In particular, it has not built-in support of the functions in the math unit. But no problem: the parser can be extended by user functions -- I described this in the tutorial https://wiki.lazarus.freepascal.org/How_To_Use_TFPExpressionParser. On https://sourceforge.net/p/wp-laz/code/HEAD/tree/MathPlotter/trunk/source/mpmath.pas you can find an (I think: self-contained) unit which makes all functions of math and numlib available to fpexprpars.

Or is there a bug that crashes it with the exponent. Which is so basic mathematical operation that I'm really surprised I need to ask.
I don't know what you are doing. Any way, the function exp() is built-in and should work out of the box (of course, you must add bcMath to the "BuiltIns" of the parser - https://wiki.lazarus.freepascal.org/How_To_Use_TFPExpressionParser#Built-in_categories).

To convince you, try this:
Code: Pascal  [Select]
  1. program Project1;
  2. uses
  3.   SysUtils, fpexprpars;
  4. var
  5.   parser: TFPExpressionParser;
  6.   res: TFPExpressionResult;
  7. begin
  8.   parser := TFPExpressionParser.Create(nil);
  9.   try
  10.     parser.BuiltIns := [bcMath];
  11.     parser.Expression := 'exp(-1)';
  12.     res := parser.Evaluate;
  13.     WriteLn(parser.Expression + ' = ' + FloatToStr(res.ResFloat));
  14.   finally
  15.     parser.Free;
  16.   end;
  17. end.

Is there a mature unit that does have a solver which can handle numerically equations in form of:
EQ: a/sqrt(b)=sqrt(c) where a=1, b=1 solve unknown c
I don't know in general, but fpexprpars certainly has no solver. BTW, for the problem that you mention you don't need a solver: just take the square of both sides of the equation and you got the solution.
« Last Edit: August 23, 2019, 06:43:56 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #5 on: September 11, 2019, 10:20:27 pm »
Hmm, odd same topic with my original 1st post from a year ago...  :o  ..or my day job is driving me nuts and I have started to develop alter egos.  :D 
https://forum.lazarus.freepascal.org/index.php?topic=41588.0


Lock maybe? This 2019 created topic have better basic question and answer arrangement than original I must say.