Get REAL and IMAG from complex number in parsed equation
Asked Answered
G

0

6

I am evaluating Math.NET Symbolics for use in our application where we want a simple math parser for the user to calculate custom equations from our measurement data. Our data is in the form of Complex numbers.

To test I use the following code-snippet in LINQPad:

var complexA = new Complex(1, 1);
var complexB = new Complex(1, 2);
var symbols = new Dictionary<string, FloatingPoint>()
    {
        { "a", complexA },
        { "b", complexB }
    };

Evaluate.Evaluate(symbols, Infix.ParseOrUndefined("1/(a*b)+cos(b)")).ComplexValue.Dump();

This works great, but the following operators I could not find:
How can we get the REAL or IMAGINARY part of the symbols?

I tried real(b) and imag(b), but that did not work.

As a workaround I could also do the following, but I would prefer an operator like abs(b) gets me the magnitude:

var complexA = new Complex(1, 1);
var complexB = new Complex(1, 2);
var symbols = new Dictionary<string, FloatingPoint>()
    {
        { "a", complexA },
        { "b", complexB },
        { "b_REAL", complexB.Real },
        { "b_IMAG", complexB.Imaginary }
    };

Evaluate.Evaluate(symbols, Infix.ParseOrUndefined("1/(a*b)+b_REAL")).ComplexValue.Dump();

P.S.: Bonus points for an operator to get me also the phase without calculating it manually.

Ghiselin answered 7/11, 2017 at 9:51 Comment(1)
As far as I know, this is just not supported. Here is the list of supported functions: github.com/mathnet/mathnet-symbolics/blob/master/src/Symbolics/…. And here is the issue about extensibility of that library (allow adding more functions without modifying source): github.com/mathnet/mathnet-symbolics/issues/8 which is 2 years oldPiscatorial

© 2022 - 2024 — McMap. All rights reserved.