mathnet / mathnet-symbolics

Math.NET Symbolics
http://symbolics.mathdotnet.com
MIT License
349 stars 66 forks source link

Format->Parse of Infix-Expression fails #30

Closed patham9 closed 7 years ago

patham9 commented 7 years ago

Infix.parse has troubles parsing the E-notation Infix.Format (and also FormatStrict) generates:

2*soll_14_0_0 + 0.05*soll_14_1_0 + (-6)*x_0_0_0 + (-6.40869140625E-05)*x_0_1_0                                                                  
                                                                 ^Expecting: infix operator or ')'

So the issue is that it is currently not possible to format and then re-parse infix expressions.

Is there a faster or at least different way to serialize expressions to workaround the issue in the meanwhile?

Best regards, Patricck

patham9 commented 7 years ago

A dirty hack solution I used for now (Infix.fs lline 176):

| Approximation (Approximation.Real fp) ->
            if fp >= 0.0 then write (fp.ToString("0.#########").Replace(".","§"))
            else
                if priority > 0 then write "("
                write (fp.ToString("0.#########").Replace(".","§"));
                if priority > 0 then write ")"

On the other side then § has to be replaced back to . again of course.

This shows one way to deal with this issue, namely to force it to not use the exponential notation. However this often results in much longer string lengths, so I guess a change in the parser that takes into account the format of the Invariant culture will be necessary in the end.

Best regards, Patrick

cdrnet commented 7 years ago

Thanks for reporting this. Yes, format and parse are supposed to be symmetric (expect for some loss of precision when using approximations - but that's ok since they are approximations after all).

cdrnet commented 7 years ago

(slightly related: #29)

FoggyFinder commented 7 years ago

It seems, it's can be easy to fix if add one parameter for options:

Just use

        let options = 
            NumberLiteralOptions.AllowFraction 
            ||| NumberLiteralOptions.AllowFractionWOIntegerPart 
            ||| NumberLiteralOptions.AllowInfinity
            ||| NumberLiteralOptions.AllowExponent

instead of

let options = NumberLiteralOptions.AllowFraction ||| NumberLiteralOptions.AllowFractionWOIntegerPart ||| NumberLiteralOptions.AllowInfinity

in the Infix.fs#L57 (not tested yet)

patham9 commented 7 years ago

Indeed, this is elegant and also fixes it. Thank you!

cdrnet commented 7 years ago

This seems to be fixed now in master and the next release. Thanks!