mathnet/mathnet-symbolics

Format->Parse of Infix-Expression fails

patham9 opened this issue · 6 comments

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

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

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

(slightly related: #29)

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)

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

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