Infix.Parse throws error on underscore?

(Byron Wall) #1

I am trying to parse a set of expressions that involve underscores in names. I am getting an error thrown on the Infix.ParseOrThrow call.

System.Exception: Error in Ln: 1 Col: 9
Expecting: infix operator or ')'

   at static Expression MathNet.Symbolics.Infix.ParseOrThrow(System.String infix)

Is this a known limitation on the parser? Is there a way to allow the underscore to be treated as a valid character for a symbol name? Currently I am removing the underscores to allow the parsing to proceed, but it would simpler to not do this.

The line in question looks like this: var exp = Infix.ParseOrThrow("(TESTING_UNDER)*(2)");. The actual expressions I am messing with are more complicated and I would like to use Math.Net to simplify them.

I am using MathNet.Symbolics from NuGet.


It is limitation of the library. I’m not sure whether there was any reasons to disallow underscore character. So I would like to know the opinion of @cdrnet

(Christoph Rüegg) #3

One aspect we would need to clarify is how such names should be treated in LaTeX, where the underscore has a special meaning (it renders the right part as subscript). See also discussion in Greek (and special) symbols in LaTex. It seems to me you wouldn’t want to render the UNDER part as subscript in your example.

(Byron Wall) #4

That’s correct regarding the underscore not indicating a subscript. For this particular application, I am immediately turning around and doing Infix.Format(exp) on the exp that was just generated so I am not expecting LaTeX output here. I am using Math.Net purely for its ability to simplify an algebraic expression. I understand the general concern regarding LaTeX output though.

For comparison, Sympy assumes that the underscore was meant to be a subscript when generating its LaTeX output for the expression. This probably matches what most people expect to happen when using underscores in variable names.

from sympy import *


Added the ability to use underscores in the names of identifiers. So you can build .dll from source and use it.

(Byron Wall) #6

Thanks for knocking that out. I’ll build it on Monday and give things a shot. This will replace a nasty chunk of code where I was substituting in 6 random characters and then replacing those once Math.Net was done.

(Christoph Rüegg) #7

Released as v0.14.0.

(Byron Wall) #8

Thanks for making the change and pushing a release. I was able to remove the kludgy underscore handling, and the symbolics work as expected.