Jump to content

Best practices for computing user equations


Recommended Posts

A few years ago I wrote my own equation computing algorithm for my company's flagship software. The user will write equations using variable names and constants (for example a=2*b+3), and those equations run continuously every 100ms.

The pros

  • The user can add Min and Max expressions inside the equation. For example a=Min(b,c)+2.
  • The syntax supports parenthesis. For example a=3*(b+c).

The limitations

  • You can have several operators but their priority is not respected. For example the result of 1+2*3 will be 9 instead of 7. The user has to write 1+(2*3) or 2*3+1 to get the correct result.
  • You can't put an expression inside a "Power" calculation. For example, you can do a+b^c but you can't do a^(b+c). You would need to create a new variable d=b+c and then do a+d, so now you have 2 equations running in parallel all the time instead of 1.
  • There is no support (even though it wouldn't be hard to add) for sin, cos, tan, modulo, square root...

I am now thinking of using a built-in LabVIEW feature (or one the community might have created :ph34r:)  in order not to reinvent the wheel completely. Surely I am not the only person who needs to compute equations.

I looked at vi.lib\gmath\parser.llb\Eval Formula String.vi and it seems to answer 90% of my needs, it is simple to use, but it doesn't support Min and Max expressions and writing a hybrid system would be complicated.

What do people use out there?

If I need to reinvent the wheel, I found interesting resources such as https://en.wikipedia.org/wiki/Shunting-yard_algorithm and https://en.wikipedia.org/wiki/Operator-precedence_parser so I think I can pull it off, but it's going to be very time consuming!

Cheers :beer_mug:

Link to comment
9 hours ago, Manudelavega said:

I tried, but no, Eval Formula Node.vi doesn't support min and max. Only the formula node supports it (along with modulo and remainder and others that I also need).

Oh thats dumb. It looks like thats one of the few thats different from the formula node http://zone.ni.com/reference/en-XX/help/371361J-01/gmath/dif_pars_math_vis_formnode/

If you only need individual expressions and don't mind paying a little bit: http://sine.ni.com/nips/cds/view/p/lang/en/nid/21313

Link to comment

I have a bit of code that evaluates formulas. It has to run very fast, so the formula evaluation wasn't an option to me. The code takes the formula and converts it to a sequence of actions to perform... or, to put it another way it turns the "normal" infix formula into RPN. The evaluation is performed by iterating through the RPN formula using a "stack" (preallocated array of doubles). The initial infix to RPN is a time hit, but it's only once at the start.

Link to comment

We have had such a toolset for a couple of years: http://www.gpower.as/downloads/expression-parser-toolset

I have worked for almost 15 years writing math software for HP calculators, so I'm also keen on RPN ;-) It's not trivial to do a fully functional toolset like Expression Parser. Probably close to 1000 hours have gone into making it.

From our website:

Evaluate mathematical expressions, given as text strings, into numeric values:

Parse and evaluate

  • Build and change your math expressions at runtime.
  • More than 260 math functions and constants supported.
  • Very high performance.
  • Supports any number of variables of any name.
  • Supports VI Registers.
  • Reports overflow if that occurs during evaluation.
  • Supports all 14 numeric data types that LabVIEW offers, including complex evaluation.
  • Offers special expression control like conditionals, piecewise defined functions, pulse trains, and defining your own custom periodic functions.
  • Supported on desktop and real-time.


Edited by Steen Schmidt
Link to comment
12 hours ago, Manudelavega said:

I tried to open the Expression Tester but it can't find one of the dependency...



You need to upgrade the VI Register toolset to v2016.0.0.31. VIPM should have told you this (since that is a dependency of Expression Parser), but VIPM has been broken for a long while on many details. You can get VI Register v2016.0.0.31 either through VIPM (it's published on the LabVIEW Tools Network) or from our website here: http://www.gpower.as/images/downloads/viregister/gpower_lib_viregister-2016.0.0.31.vipc


Link to comment
1 hour ago, Manudelavega said:

Thanks, that fixed my issue, but now it is stuck on "starting expression tester, please wait" (or something similar) and I need to kill the LabVIEW process after a few minutes :(

Oh yes, that's a side effect of VIPM not making sure all dependencies are in order during the installation process. You want to either 1) uninstall and then re-install Expression Parser after its dependencies are in place (making sure mass compile is enabled in VIPM), or 2) manually mass compile the 'C:\Program Files (x86)\National Instruments\LabVIEW 2015\vi.lib\GPower\ExprParser' folder, whichever you prefer. All something VIPM should have done for you.

The reason is that since you have updated a dependency (VI Register) after you installed Expression Parser it makes LabVIEW need to compile the Expression Tester when you launch that. For most LabVIEW code compiling on load is merely an annoyance that takes an extra few seconds, but for Expression Parser the matter is different. It's very complex code, which takes around 5 minutes to load an uncompiled Expression Tester for instance. The entire Expression Parser VI package takes around half an hour to build :wacko: (so it's good only I have to do that). We made some improvements to the code architecture from the last version to this newest one, which improved VIP installation speed from 15 to 4 minutes. Still much too long for comfort. I have a much faster installing version here, but that one takes a 10% runtime performance hit which I'd like to get back before we release it.

Link to comment
  • 5 months later...
On 2/16/2017 at 7:37 PM, Manudelavega said:

I tried, but no, Eval Formula Node.vi doesn't support min and max.

Just mentioning, if not OT. Something else not supported are booleans (ok you could use 0 and 1 and + *). In a project of mine I ended up using this, which is fine but simplistic. I don't remember about performance; considering my application it may well be that simple expressions evaluated in less than a ms.


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.