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.