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 )  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

I've never used it, but doesn't this do that? http://zone.ni.com/reference/en-XX/help/371361J-01/gmath/eval_formula_node/

It says it supports the same syntax as the formula node which seems to include min max sine cosine etc. Might be worth trying it.

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).

My experience is also only with the evaluate formulas.  I ended up augmenting it a bit with a few pre and post function calls for doing custom things.

Have you taken a look at MuParser yet?

I haven't had great experiences with DLL in the past so I wanted to stay away from it

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

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.

I started implementing exactly what you're describing Tim. This RPN stuff is pretty neat

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:

• 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.

Cheers,
Steen

Edited by Steen Schmidt
On 2/22/2017 at 11:15 PM, Steen Schmidt said:

This is really nice! Koodos!

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

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

Cheers,
Steen

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

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  (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.

Did the mass compile and everything works fine now

• 5 months later...

Anyone interested in trying this out?

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.

Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
• Similar Content

• By Porter
View File LV muParser
LV-muParser provides a simple LabVIEW API for muParser fast math expression parser.
A modified version of muParser v2.2.5 is included. It will be installed to your "<LabVIEW>\resource" directory. I have added support for the "!" (not) operator as well as added ":" as a valid character for variable names.
You will find the muParser API in the functions palette under "Addons > LAVA > muParser"
muParser: http://beltoforion.de/article.php?a=muparser
LV-muParser source on github: https://github.com/rfporter/LV-muParser
Submitter Porter Submitted 08/25/2017 Category General LabVIEW Version 2015 License Type BSD (Most common)

• Hi, I'm new in Labview.  I work as an intern. I have a formula for fitting a spectrum. The left side of the spectrum is higher than the right side. There's a background on the left side.I need to lower the level of the left side to the right by applying this formula. It should be a Gaussian curve.  I need help. Who can help me?

• By Porter
LV-muParser provides a simple LabVIEW API for muParser fast math expression parser.
A modified version of muParser v2.2.5 is included. It will be installed to your "<LabVIEW>\resource" directory. I have added support for the "!" (not) operator as well as added ":" as a valid character for variable names.
You will find the muParser API in the functions palette under "Addons > LAVA > muParser"
muParser: http://beltoforion.de/article.php?a=muparser
LV-muParser source on github: https://github.com/rfporter/LV-muParser

• After two years "leeching" content every now and then from the Lava community I think it's time to contribute a little bit.
Right now, I'm working on a project that involves lots of data mining operations through a neurofuzzy controller to predict future values from some inputs. For this reason, the code needs to be as optimized as possible. With that idea in mind I've tried to implement the same controller using both a Formula Node structure and Standard 1D Array Operators inside an inlined SubVI.
Well... the results have been impressive for me. I've thought the SubVI with the Formula Node would perform a little bit better than the other one with standard array operators. In fact, it was quite the opposite. The inlined SubVI was consistently around 26% faster.
Inlined Std SubVI

Formula Node SubVI

evalSugenoFnode.vi
evalSugenoInline.vi
perfComp.vi
PerfCompProject.zip
×
×
• Create New...