Example Program Drafts

Showing results for 
Search instead for 
Did you mean: 

Math Node - A new way to do math in LabVIEW

   Math Analysis Category Winner



My primary goal of this example is to demonstrate something cool in LabVIEW.  I hope to have accomplished a few things:  Demonstrate cool LV capabilities such as scripting and XNodes in new and interesting ways and to share my vision for a new type of math tool in LabVIEW that is based on G, but leverages the power of other tools such as Mathematica, Math Type, LaTeX, and others.  In short, I utilize the power of LaTeX and MathML to quickly and easily share equations between all of these programs (and even Wikipedia) and implement them in native G code in LabVIEW.

At the heart of the code is an expression parser which uses the Shunting Yard algorithm to convert math expressions to G code using scripting.  On top of this, I have written parsers which convert both MathML and LaTeX to regular expressions which can then be converted to G.  With this ability, it is very easy to transfer equations to LabVIEW.  In addition, with the use of freely available software (MikTeX and Ghostscript) this tool will automatically place beautifully formatted equations as labels next to the G code.  The generated code can be placed on the clipboard for pasting, put into a new VI, or saved as a Math Snippet.  The Math Snippets contain the LaTeX or MathML used for the equation and label and they can be loaded and edited by the tool, a great timesaver.

As a bonus, I have taken things to the next level.  While G code is beautiful for many things, complex equations can be difficult to follow and debug.  Therefore, I have included an XNode which encapsulates the G code and presents only a Typeset equation and a set of terminals.  Think formula node with LaTeX typesetting.

Finally, it may not be practical, but one of the flat-out coolest things I have done with LabVIEW is to use the Math Input Control of Windows 7 to write out an equation by hand (scribble really), and see that handwritten code turned automatically into G.  Now you can do that too if you have Windows 7.

The code is Windows only since I do not have Mac access.  Most of the Windows only functionality is in clipboard functions and system exec calls to execute the external tools, which should be fairly easy to port for those interested.


Three tools are included in this example.

Convert Math Expression to G:  Takes code written in MathML, LaTeX, or normal text (y=a+b) and converts it to G with a nicely typeset label (if you install LaTeX and Ghostscript).  You can also launch the Math Input Control and the result is automatically placed into the control, or go to a Wikipedia page and copy and equation and you can paste the code into the controls automatically.  With Wikipedia, you can either use the web image as the label or tweak the LaTeX as you desire.

Math Node.xnode:  When you drop this XNode onto a block diagram, a dialog box appears which is very similar in feel to the Convert Math Expression to G tool.  You can load math snippets, or generate code in many of the same ways.  When you are finished, you get a beautiful node powered by G under the hood.  My knowledge of XNodes is primarily from reverse engineering the ones in vi.lib and this code should be considered a hack.   It does however, demonstrate a Math Node I would like to see as a fully integrated tool in LV's arsenal.  Plus, with scripting fully supported, maybe we can get some more light shed onto XNode technology, the logical next step.  Note:  the terminals are fully type adapting, need an array, or cluster, just wire it in.  If you can wire it to the math primitives, you should be able to wire it up to the math node.  Much of the underlying code is the same as the Convert Math Expression VI to G, I just keep them namespaced since having an XNode in memory reserves the SubVIs and prevents editing.

G shortcut for Quick Drop:  Just a bonus tool I use to convert Greek symbols in control labels to Greek, so things like \alpha or \beta are converted to Symbol font.  Some LV bugginess with copy and paste of formatting in terminal labels, but the front panel looks good.  Shift-G undoes the greek-ification.

Steps to execute code:

  1. Download and unzip one or all of the tools.
  2. For LV2010, open ConvertMathExpressionToG.vi and then save after recompilation.  Then open Math Node Tree.vi and do the same thing.  This VI will load all subVIs into memory for recompilation.
  3. For full enjoyment and power, install MikTeX (http://miktex.org) and Ghostscript (http://pages.cs.wisc.edu/~ghost/).  The basic MikTeX version should be fine, both programs are free (and quite useful).  You can still use many features without these installed.
  4. I highly recommend adding Math Node.xnode to the palettes (Tools->Advanced->Edit Palette Set..)  Be sure to set file type to All Files to see the xnode.  LV seems to use the icon from the Initialize ability VI so I have made that one a bit snappier than the others.
  5. Add the code from the G shortcut to the QD shortcut folder LV20xx/resource/dialog/QuickDrop/plugins.
  6. Read the Formula Parsing Readme file to see what functions are supported.
  7. When you first run the code you will see a configuration screen asking for the paths to the directories containing the LaTeX and Ghostscript executables.  On my system, MikTeX is in Program Files/MikTeX 2.7/miktex/bin and Ghostscript is in Program Files/gs/gs8.xx/bin.  When both paths are checked out, you have the option to test the install.
  8. Try it out, if I have done my job, the UI should be fairly intuitive.  When copying equations from Wikipedia, drag to select the equation and use 'Copy' as opposed to 'Copy Image'.  I have tested in IE, Mozilla, Chrome and Safari.  In Safari you only get the copy option when the mouse is not over the selected equation when you right-click.



Here is the tool in action.


Copy from Wikipedia and create G code automatically


Use your favorite tool (this is MathType), and export the LaTeX or MathML to quickly create G code.


Here my chicken-scratch is turned into G with a much nicer looking label, how cool is that?

Math Node VI.png

Here is the Math Node XNode in action.  Need an array?  All of the inputs are fully adaptable.

VI Snippet:


A math snippet with embedded information about the equations used for the G and the label.  You can open this in either the ConvertMathExpressionToG.vi or the Math Node configuration dialog to edit the code.

Some tips for your enjoyment:

  • The LaTeX you need to know is pretty basic here.  Fractions are \frac{num}{den}, roots are \sqrt{root}, where n is optional, and exponents are x^{32}, the braces are used to group the values, otherwise LaTeX would see that at x^3 followed by 2.  The G parser is perfectly happy with x^32, but you may be surprised.  Most functions simply have a \ character in front, so sin(x) is \sin (x), it is only there so the type face is correct.  That about covers it, once you see a few examples, it is pretty easy.
  • LaTeX is much easier to deal with than MathML, use the conversion.  Cleaning the G code string removes superfluous formatting to help you out.
  • Implicit multiplication is very common in typeset equations, but very difficult for parsers so it is the most common bug you will find.  For now I leave it up to you to put a * symbol between two variables.  2x is ok, but xy should be x*y unless you really want a variable named xy.  The preview function is helpful here, look for misnamed variables.  The 'Clean *' button for the label will remove the * character from the label since it often looks better.  If you really want to show multiplication I suggest using \times, it looks better.
  • If you do not want to install LaTeX and Ghostscript, Wikipedia is a great place to find equations, and the tool will use the web image.  I like the special relativity page since it is full of roots and Greek characters.  I try to fake LaTeX a bit by converting Greek characters to Symbol font, but it isn't quite the same.
  • The Math Input Control may be finicky, it is a hidden app called from a shared library, and I don't use it often enough to fully debug the errors, just close and reopen the VIs and it should work.  Usually I only have trouble when I am editing VIs, and not when I just use the tools.
  • Finally, I am primarily a hobbyist in LV and this is written during late nights, early mornings, coffee breaks,etc.  Bugs are certainly there, as in most code of this scale and ambition.  Digging too deep into either the Shunting Yard, or scripting is not for the feint of heart, but I am happy to assist those up for the adventure. 
  • I could spend weeks adding cool features, and probably will anyway.

Links to videos:

Part 1 - Introduction :  http://www.screencast.com/t/WgKDsNzI0

Part 2 - Math Input Control and Math Node : http://www.screencast.com/t/jnW69fj1P

Part 3 - Multiple Equations from Wikipedia and Quick Drop Action : http://www.screencast.com/t/YzG9ojDPig3

VI attached below

Removed a few vestigial passwords.

Member vugie

Darin, I didn't test it much, but the idea is simply beautiful.

Knight of NI Knight of NI
Knight of NI


I am a big fan of all your work, but this is just magical!

Member wirebirdlabs

We're not worthy! We're not worthy!

Member shred444

alright, i'll consider you the 'scripting man'

nice demo.

Member herrlin

I got to admit I feel I am worthy.  I think you have an interesting idea here but I’m not a fan of the way you executed it.  In my opinion I find your BD to be messy and not well documented.  I think you over use local variables and do not have proper error handling in place.


Just trying to spread the LabVIEW love.
Member wirebirdlabs

>> I got to admit I feel I am worthy.  I think you have an interesting idea here but I’m not a fan of the way you executed it.  In my opinion I find your BD to be messy and not well documented.  I think you over use local variables and do not have proper error handling in place.

I won't disagree with you, but this is the most "a-holes and elbows" instance I have seen of someone not within R&D (read, not getting a salary, not getting benefits, not getting to work for a "100 Best Companies to Work For") hunkering down and fulfilling a need. I really hope R&D embraces this concept and releases it in the LabVIEW base package (after further development).
Active Participant chris.b
Active Participant

Phenomenal! Thanks so much for sharing

Chris Bolin
LabVIEW Partner Program, CLA
Member smiga

I am really impressed - amazing, absolutely brilliant idea, bravo !!!

follow your dreams
Member RoboticsME

Awesome!  This would've been so useful in my research days where I was constantly switching between LabVIEW, Mathematica, and LaTeX!

Member Rafa L

Making the engineers live easier

each time.

Active Participant wildcatherder
Active Participant

If you can't beat 'em, cojoin 'em.  Excellent bridge from formal math to LabVIEW.

Active Participant Elijah_K
Active Participant

This is really impressive - thank you for working on this and for sharing it with everyone.

Elijah Kerry
Principal Product Manager, Platform Software
Follow my Software Engineering for LabVIEW Blog
Active Participant 10Degree
Active Participant

Well, I suppose it's not bad...  It is somewhat un-worthless and completely non-useless.  If I close one eye and hold my hand up to partially cover the screen when viewing the block diagrams I can suppress the gagging reflex   All fun aside, fantastic code – not as cool as my guitar tuner, but fantastic nonetheless, thanks!

Active Participant gt3000
Active Participant

This is a great concept that will help a lot of engineers make the transition from theory to practice when programming.  Likewise, it looks like documentation in Block Diagrams with complex math will be greatly improved.  I can't wait until I have a need to use this in an application.

George T.
Applications Engineering Specialist
National Instruments UK and Ireland
Member topdevilx86

herrlin wrote:


I got to admit I feel I am worthy.  I think you have an interesting idea here but I’m not a fan of the way you executed it.  In my opinion I find your BD to be messy and not well documented.  I think you over use local variables and do not have proper error handling in place.


Negative comments without proof of contribution to the community is harmful, self-centered, egotistical.  If you think you could do better, then why aren't your contributions being displayed?  Why aren't you the one to submit this contribution?  Please, next time just keep your comments to yourself, or be more helpful on how things could be optimized for the next version of it.  This is a valid and useful tool no matter how you look at it.  Maybe not needed by a great many of people but the people that do need it are grateful they don't have to reinvent the wheel.


Great work, please keep up the contributions.  The community has been strengthened by your efforts.

Member mitaceed

It's very cool,very userful.Thank you very much!

Knight of NI Knight of NI
Knight of NI

topdevilx86 wrote:

Negative comments without proof of contribution to the community is ...

Everybody is entitled to his opinions, so let's not fall into the same trap and start name calling. Each reader can easily judge the "weight" of a comment based on other contributions of the same author.

I agree that Darin's code is absolutely stellar and very well organized. 99% of the users here in the forum could learn something about code structure! Considering that most of this was written under time pressure and in spare time makes it even more impressive.

Yes, there are locals, but they are confined to the toplevel UI loop where they belong. The locals only contain simple data structures (scalars, booleans). There are other ways to do this, but it is perfectly OK the way it is done here. It is also quite self-documenting.

Yes, there are very few diagram comments and they are really not needed! The program is well structured with simple modular subVIs that have descriptive names and have easy to understand  "bite sized" code. The subVIs are even color coded by functional group. (There are none of these overzealous redundant comments we see often elsewhere, e.g. "Here we read the binary file" right above the binary file read icon which is also showing its label Smiley Surprised).)

What is obvious from looking at the code is that it is written by somebody with lots of experience. Miles of wires were strung before this code was written!!

I could not find any flaws in the program structure and fully agree with the assessment of Jeff K over here.

Member RigilKumar

Congratulation..Its very great work.. I would like to know whether it will support the advanced Math functions like Bessel?.. Does it support complex datatypes?

Member herrlin


Please point me to your "contribution to the community."




Just trying to spread the LabVIEW love.
Member dhendrix11

Is it possible to create a version of the clipboard dll's that work with the 64bit version of LabVIEW?

Member James_Chou

Absolutely amazing! Wished I had this while doing my dissertation, no more retyping every single equation twice in LaTex and Matlab/Mathscript Node!


Its just amazing.  Great work!!

Member limpeh

Gd job bro...U rocks ..I really need this for development of an exoskeleton project..

Trusted Enthusiast
Trusted Enthusiast

Cool! But this is supposed to work with tek and ghostscript not installed, right? I tried the first video example (copy from Wikipedia) and can't seem to get anywhere (nothing appears in any of the panels when I try to copy from wikipedia or from any other source).

Then I typed \gamma = \frac{x}{y} as a Latex example (not expecting it to be rendered obviously) and tried to "Preview", which gave me the correct block diagram plus a bonus (see below).

I am using IE 8...

Internal Error.jpg

Trusted Enthusiast Trusted Enthusiast
Trusted Enthusiast

It should work in a limited fashion without TeX or GS, it looks like you are using a patched LV10 with IE8.  Not sure I tried it with that combo (I never use IE, and rarely use LV10), I will attempt to this weekend.  I should be ready to update one of these days as well.

One question: 32-bit or 64-bit?  I don't have LV on any 64-bit OS.

Trusted Enthusiast
Trusted Enthusiast

I tried with Firefox, and that seems to solve the Wikipedia issue. It still returns the internal error though (it's actually just a warning, LV does not crash).

Interestingly, I could not find a way to "copy" only in Chrome. My only options are to copy the image or the URL...

I am using LV 2010f2 32 bits.

Trusted Enthusiast Trusted Enthusiast
Trusted Enthusiast

Copy Image should work in Chrome, it puts the proper HTML fragment onto the clipboard (at least for me).

Trusted Enthusiast
Trusted Enthusiast

How do you do that (Chrome)? I tried to select, Copy image then use either <--Clipboard or <--Wikipedia but nothing happens :-(

Trusted Enthusiast Trusted Enthusiast
Trusted Enthusiast

For me (LV9 + Chrome+XP) I select, right-click 'Copy Image' then use '<-- Wikipedia' .  The LaTeX appears in the G Code string and the URL appears in the Label.

Trusted Enthusiast
Trusted Enthusiast

Yes, it seems to be a LV10 issue. I just tried all the above in LV9 SP1 and it works as described in IE8 and Chrome (I am using XP as well).

Trusted Enthusiast Trusted Enthusiast
Trusted Enthusiast

Thanks for the info, I'll try to look into the LV10 issue.  I very rarely use it these days, LV9 seems quite rugged by comparison.

Trusted Enthusiast
Trusted Enthusiast

A colleague of mine suggested providing the opposite functionality too: starting from a mess of wires supposed to implement an equation, generate the corresponding LaTeX formatted equation (you don't need to simplify it algebraically!). Don't hate me, I am just the messenger! But I admit that it would be cool, just to make sure that a formula is implemented properly...

Member Gyc

Kudos on this one!!!!

Member KleeD

Absolutely awesome! I am looking forward to try this out tonight.

Member milanC

Very nice, but there is problem with localized decimal point. For example when set 2,34(localized ,), program hung-up. When LV is set to local decimal point and I set in your tools decimal point(.) LV cut decimal point and number.

Member Drew.Rankin

This is pretty cool, Darin. I am thinking about how to use this in an application where the user would be entering a custom equation for data processing and the code to handle the equation would be created on the fly. Is this even possible? (I know my co-worker lives by the phrase "Anything is possible with LabVIEW", but even LV has it's limitations) ;-)

I know you just thought, "duh, that's what I just did." I'm talking about integrating this into an app where the LaTex translated G would be created and called from within the app at run-time, not as part of the development of the app.

Also, where can I get that "Edit Connector Pane..." add-on? That's pretty sweet as well.

Member transfererror

I made the same experience as X-). I'm also using Labview 10.0f2 and it doesn't work here either. Though I'm getting no error message, but Labview crashes completely.

Member George_G

Great tool!  There does seem to be an issue in order of operations in the "IsOperator.vi"  The default value of the array on the front panel has the subtraction as higher precedence than multiply and divide.

Trusted Enthusiast Trusted Enthusiast
Trusted Enthusiast

My version has +,-,/,*,^ in that order, precedence is given by the index so the last element has higher precedence.

In case you need to change it, you found the spot.

Member DrewPierce

Pure wizardry. I am blown away. -drew

Drew Pierce | District Manager, Central Texas | National Instruments
Member FilipeTaveiros

Superb addition to LabVIEW!! Awesome!

One question: Is it possible for us to add functionalities? For example I use the "Add array elements" very often, and I would like to incorporate a function named "sum()", which would be replaced by the aforementioned VI.


Member mavimi

I can pretty reliably chrash Labview, by entering this formula in the 'Converter', and try to convert it.

\frac{(R * K) + (R * K)}{K - R}

(I know this also could be shortened, but this was not what I needed, its just what I could boil the example down to, and still get LV to chrash.)

From the chrash report:

DAbort 0x1A7102DF in fpsane.cpp

Version: 14.0.1f3 (32-bit)

Trusted Enthusiast
Trusted Enthusiast

Any plans to release a 64 bit version of the tool?

There is a couple of DLLs which break the whole thing in LV 64 bit:



Trusted Enthusiast
Trusted Enthusiast

I installed LV 2015 32 bit to have a go at a text to G conversion I was not looking forward to implement manually and I managed to crash the VI (and LabVIEW: DAbort 0x1A7102DF in fpsane.cpp error, line 547) with the Preview button.

Here is the plain text formula I pasted in the String for G code control:

(-r + (-1 + y^(-1))^(1/6))/(6*exp((-r + (-1 + y^(-1))^(1/6))^2/(2*s^2))*sqrt(2*Pi)*s^3*(-1 + y^(-1))^(5/6)*y^2)

[It is a Wolfram Alpha plaintext output - from Mathematica, with a few edits of mine, such as replacing e^ by exp].

The diagram creation seems to work fine until the final steps of cleanup when the crash occurs.

These subparts of the expression work fine:

(-r + (-1 + y^(-1))^(1/6))/(6*exp((-r + (-1 + y^(-1))^(1/6))^2/(2*s^2)))

Sqrt(2*Pi)*s^3*(-1 + y^(-1))^(5/6)*y^2

Trying to multiply or divide them and the crash occurs.

I know this is not how you demo your code, but I thought you might be interested in this failure of expression handling.

BTW, I added \sqrt in the array of LaTeX Commands (in ToLaTeXFunctions.vi). Not sure whether this is necessary, but since the expressions above go through that step I had to take a look.

EDIT: I verified that miktex and ghostscript where properly installed by checking that the LaTeX output of Mathematica was interpreted correctly in the "label" control:


   e^{-\frac{\left(\sqrt[6]{\frac{1}{y}-1}-r\right)^2}{2 s^2}}}{6

   \sqrt{2 \pi } s^3 \left(\frac{1}{y}-1\right)^{5/6} y^2}


Screen Shot 2015-12-24 at 16.15.16.png

Using this LaTeX version (after cleaning) in the "string for G code" control results in the same crash...

Screen Shot 2015-12-24 at 15.49.46.png

Trusted Enthusiast
Trusted Enthusiast

Just for info, the toolset crashes in CleanBlockDiagram.vi.

I modified the above expression as z = ... to check whether this was due to a lack of output terminal, but that appears not to be the case.

Right before the call to that VI, the VI being built looks like this:

Convert Math Intermediate Step.png

Clearly, it has a lot of things wrong at this stage...