Recent

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

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Math Expression parser(s)
« on: June 17, 2018, 10:53:03 am »
Hello

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.

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: Math Expression parser(s)
« Reply #1 on: June 17, 2018, 11:42:33 am »
I once wrote this: http://wiki.lazarus.freepascal.org/How_To_Use_TFPExpressionParser

Where I could find a list of internal operators
I fear nowhere. It closely follows Pascal notation, except for "power" which has the symbol '^' while in FPC it has the symbol '**' (but your fpc must not be too old for the exponent, maybe 3.0 and younger, 3.04 definietely works).

it do recognize (sin(), sqrt(),sum() etc..)
The parser is very general and supports only feature groups added explictly. For doing math you must add "bcMath" to the BuiltIns of the parser. By default it handles only the math routines of the system unit, i.e. "sum()" won't work this way. In order to get access to other routines, like those in the unit math, you must write a corresponding procedure for the parser and register it as described in the wiki. In my "MathPlotter" (https://sourceforge.net/p/wp-laz/code/HEAD/tree/MathPlotter/trunk/) you find a unit "mpmath" which adapts all "math" and "numlib" functions to the FPExpressionParser.

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
You mean symbolic solution? AFAIK, there is none, at least not within fpc. Marco's "symbolic" maybe, but I've never worked with it.
Numberic solution? You may find routines in "numlib" - see http://wiki.lazarus.freepascal.org/NumLib_Documentation.

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
Maybe, I don't know it. The main advantage of FpExpressionParser in my eyes is that it can be extended to accept any user-provided function. AFAIK, no other math parser is able to do this.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #2 on: June 17, 2018, 12:23:26 pm »
Thank you a lot WP! I do have read your wiki article and somehow I did forget that there is a good explanation about adding functions, my bad on that.  :-[ However I do need to ask a small clarification, this might sound a bit stupid, but is the definition of new function done in the source where the FParser is used or in some other unit (like in TFPExpressionParser source).

It seems that I did panic too early.  :)

No I'm not asking about symbolic solver, but a numerical solver. Multiple predefined variables (=numbers) and one to solve for, basically the only difference to basic TFPExpressionParser task (as shown in your wiki article) is that it does have a =-sign somewhere between expression. So just basic equation solving 5=a/2, to know at which value of unknown variable a, the two sides are the same ... just basics. Since all the other variables will have user defined value and the equation will be solved only for one unknown. Maybe I'm too early on this also, maybe the TFPExpressionParser do handle this case also**.  I did mention Marcos "symbolic" because I know there is at least some form of root solver implemented. I'm just trying to get a picture if I need to build my own, which is wasted effort if it turns to be already done in some unit of FPC/LCL.


**Now I noticed two things on the TFPExpressionParser source. There is token for "is equal" and finally I did find also the short list of what I assume is the definitions the [bMath].

PS. I'm using Lazarus 1.6 with FPC 3.0.0 so I might need to update.
« Last Edit: June 17, 2018, 12:41:58 pm by ArtLogi »

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: Math Expression parser(s)
« Reply #3 on: June 17, 2018, 12:44:42 pm »
I do need to ask a small clarification, this might sound a bit stupid, but is the definition of new function done in the source where the FParser is used or in some other unit (like in TFPExpressionParser source).
Never change the sources of fpc or Lazarus, your changes will be lost when y new version is released. If you have good reason to change fpexprpars copy it into your project and rename the unit to avoid conflicts with the original fpc unit. This is possible because no other unit of fpc depends on it.

Back to your question: you can declare your own function anywhere. If you looked at my "MathPlotter" you see that the user-provided functions are declared in their own unit (mpMath), but if you don't need too many of them you can also put it into the unit in which the expression parser is called.

No I'm not asking about symbolic solver, but a numerical solver. Multiple predefined variables (=numbers) and one to solve for, basically the only difference to basic TFPExpressionParser task is that it does have a =-sign somewhere between expression. So just basic equation solving 5=a/2, to know at which value of unknown variable a, the two sides are the same ... just basics.
I don't understand. When you want to solve an equation numerically then why do you need the expression parser for it? Solve it in Pascal. The "numlib" of fpc contains some solvers in the unit "roo": http://wiki.lazarus.freepascal.org/NumLib_Documentation#Finding_the_roots_of_a_function_.28unit_roo.29
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #4 on: June 17, 2018, 12:58:04 pm »
Thank you again.

Well I need the expression parser as the equation that will be solved will be user defined and stored to be used again some other time. The solver is needed because the expression will be always an equation with user selected unknown variable. The equation can be anything (in the limits of the expression parser and solver), ie something like shown in the attachment (Source: HP 50g - advanced user manual).

That said I need to look at your MathPlotter, I'm sure it will answer a many questions I have right now and maybe generate a few new ones.

.. But now, time to go out to meet the sun.  :)

PS. It seems the MathPlotter refuges to compile in win7, Laz1.6 and FPC3.0.0 (https://sourceforge.net/p/wp-laz/code/HEAD/tree/MathPlotter/trunk/source/).  See attachment.
« Last Edit: June 17, 2018, 01:12:49 pm by ArtLogi »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7453
Re: Math Expression parser(s)
« Reply #5 on: June 17, 2018, 02:12:00 pm »
Symbolic does some symbolic manipulation, but basically it is similar to TFPExpression.

It can do symbolic derivation and taylor polynomal and has some minimal rearranging to eliminate constants (needed to simplify after derivation because  3x^2 becomes 3*2*x  which can be changed to 6x to optimize for repeated calculation.)

Numlib is a separate library and does include rootsolvers, specially for polynomals.
 But it is numeric, not symbolic in nature.

I'm also a HP fan, and symbolic contains RPN<->infix routines, and the internal evaluator is HP lilke.  See also the symbolic demo "rpnthing" which is an extremely basic RPN calculator/editor frontend for symbolic

 

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: Math Expression parser(s)
« Reply #6 on: June 17, 2018, 03:27:53 pm »
The equation can be anything (in the limits of the expression parser and solver), ie something like shown in the attachment
Then it is a "symbolic" problem. Not chance with fpexpressionparser, with "symbolic" maybe (but I doubt if the equation really can be "anything").

It seems the MathPlotter refuges to compile in win7, Laz1.6 and FPC3.0.0 (https://sourceforge.net/p/wp-laz/code/HEAD/tree/MathPlotter/trunk/source/).
Cannot confirm. I tested back to Laz 1.4.4/fpc 2.6.4 - all fine. Are there any special compilation parameters that you are applying? ATM I cannot imagine anything which could trigger the "Illegal type conversion" error here. I am on Win 10, though, but the program was developed on Win 7.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #7 on: June 17, 2018, 04:48:41 pm »
Well, I'm doomed to be total noob what comes to fine programming.  :D I do use general programming languages so rarely that I always forget most of what I have learned. The problem with mathplot compilation is most probably in my end.

Well the equation of that sort can be numerically solved by some iterative manner by just splitting the equation to half from the "is equal" operator (or ie. is greater than) and then using ie. expression parser to retrieve the value of both expressions until the value is the same in some predefined accuracy window or give an error if no solution is found in certain time interval. I don't know if it is then a symbolic problem as no symbolic manipulation is needed. More of an successive numerical approximations solving. This of course requires that the user doesn't enter total garbage as an equation.

@Marcov in the past I have looked your RPN implementation, it is nice.  :) I assume you are familiar with kind solver / equation manager I'm after. Basically the same kind of as the age old hp-solve found at least past 30-years in many calculator models and few connection kits.

I need to do more background search on this and look up all the information you two have now directed me to look, so I'll return with new set of (idiotic) questions in the future.  :o :P

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7453
Re: Math Expression parser(s)
« Reply #8 on: June 19, 2018, 12:08:50 pm »
Things like mathlab or hpsolve are batteries of different solvers and problem examiners and rearrangers.

This to optimize solving speed, avoid bad conditioned cases for certain solvers, and rearranging the equation so that the solvers like it (often =0 or in a pseudo polynomal syntax)

FPC's solutions are not that far. There are only solvers for certain situations, and then only in a certain representation (like a vector with coefficients of a polynomal)

It might be able to blend in some solver lib, but I've out of numerics for 15 years now, so I don't any of them by heart.

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #9 on: June 25, 2018, 10:11:40 pm »
Things like mathlab or hpsolve are batteries of different solvers and problem examiners and rearrangers.

This to optimize solving speed, avoid bad conditioned cases for certain solvers, and rearranging the equation so that the solvers like it (often =0 or in a pseudo polynomal syntax)

FPC's solutions are not that far. There are only solvers for certain situations, and then only in a certain representation (like a vector with coefficients of a polynomal)

It might be able to blend in some solver lib, but I've out of numerics for 15 years now, so I don't any of them by heart.
You are absolutely correct about the sophisticated structure of the matlab and many other math software like many HP calculators. Ie. the first HP solvers by Dr. William Kahan (The IEEE Float Kahan for ones who do not know), there is many interesting articles on those.

However my goals aren't as high, I might have written and falsely gave impression as those were at extremely high, but no the minimum level is rather low in only a few significant digits accuracy. Later on one might start to hone the amount of accurate significant digits up and the complexity of the equations the solver can handle. Again if minimum level is engineering (sizing type) calculations then what comes to the optimization of the equations. It isn't that much a priority either as most engineering equations are already optimized for slide rules and pen and paper. Speed wouldn't be that much of an issue neither since the plenty of calculating power in modern laptops and PCs. However the optimization would come in handy if at some point the application would be extended to some series type of equations like summations or integrals and/or system of equations, but single rather simple equation the optimization with modern HW it is not issue (while of course not technically sophisticated nor beautiful  >:D ).

I suppose that satisfactory results can be obtained with some form of iterative method like Newton-Rhapson combined with "ExpressionParser" to calculate approximate after the equation is split to half from the "is equal" operator with ie. RTL ExtractWord function and zero is shifted. Even more so if the application also do have a simple graph view so the user can see if and how the equation behaves, but those are nuances spinning out of the core idea.

Solver lib to FPC would be of course awesome, but...

Time will show what I end up using, for now my time have gone to other tasks again.  :-\
« Last Edit: June 25, 2018, 10:14:14 pm by ArtLogi »

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #10 on: September 11, 2019, 10:51:04 pm »
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
Thank you for the information. Yes that is what I was after, but at the time starting this thread I weren't sure what solutions were already made. My original idea was to use TFPExpression to kind of (pre)process the user entered equation in string type. Then maybe extend it with build custom numerical solver routine around that. It seems now that if not everything, but at least a great deal, is already build to TFPExpression unit.

Re-arranging is not needed if equation is cut to two pieces and solved until valid convergence is found for both sides (both sides are equal). in example:

a/sqrt(b)=sqrt(c), where  a=1 b=2 and where left side is: a/sqrt(b) = X and right side is: sqrt(c) = Z then

Loop and iterate with escape conditions until X ≈ Z
X ≈ 0.707 (calculation from user entered string containing mathematical expressions) then iterate Z with ie. newtons method (which seems to be competitive (IIRC) even if it is brute force) from seeding value of 0 upwards until condition is met and inform user that when c≈0.5 the equilibrium point (0.707. ≈ 0.707. ) is found.

Not much is done though from my part unfortunately, day job and other hobbies are eating all the time. Some lazy reading of some algorithm related articles etc. However MathPlotter compiles, but only after I do comment out some parts of the code related to these "Tool Strip buttons", some kind of overloading issue? with INT64 type.

PS. Somehow the wiki article now seems to be clearer, if someone have updated it then he/she deserves my gratitude.
« Last Edit: September 11, 2019, 11:05:01 pm by ArtLogi »

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: Math Expression parser(s)
« Reply #11 on: September 11, 2019, 11:02:28 pm »
MathPlotter compiles, but only after I do comment out some parts of the code related to these "Tool Strip buttons", some kind of overloading issue? with INT64 type.
Which code? What do you mean with "Tool Strip Buttons"?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ArtLogi

  • Full Member
  • ***
  • Posts: 144
Re: Math Expression parser(s)
« Reply #12 on: September 11, 2019, 11:07:15 pm »
MathPlotter compiles, but only after I do comment out some parts of the code related to these "Tool Strip buttons", some kind of overloading issue? with INT64 type.
Which code? What do you mean with "Tool Strip Buttons"?
Hello WP, these small ones. I can provide more information through PM is you wish.

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: Math Expression parser(s)
« Reply #13 on: September 11, 2019, 11:10:58 pm »
I can provide more information through PM is you wish.
Yes please.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10