mathnet-symbolics icon indicating copy to clipboard operation
mathnet-symbolics copied to clipboard

How to mix Complex and SymbolicExpression?

Open sadqiang opened this issue 6 years ago • 5 comments

The following code snippet does not compile because there are no suitable operators for Complex and SymbolicExpression operands in z * z + i/2.

using System;
using static MathNet.Symbolics.SymbolicExpression;
using static System.Console;
using static System.Numerics.Complex;
using Complex = System.Numerics.Complex;

namespace MathNetSymbolicsCompile
{
    class Program
    {
        static readonly Complex i = ImaginaryOne;

        static void Main(string[] args)
        {
            var z = Variable("z");
            Func<Complex, Complex> f = Parse("z * z + i/2").CompileComplex("z");
            Complex c = 1 / 2 - i / 3;
            WriteLine(f(c));
        }
    }
}

Any comments are welcome!

sadqiang avatar Dec 18 '18 11:12 sadqiang

There is a bug: Linq.formatComplexLambda doesn't handle Constant.I

I think adding a line will fix the bug.

let formatComplexLambda ...
      let constant = function
            | I -> Some (Expression.Constant (complex 0.0 1.0) :> Expression)

And use j as ImaginaryOne in Parse.

      Func<Complex, Complex> f = Parse("z * z + j/2").CompileComplex("z");
      Complex c = 1.0 / 2.0 - i / 3.0;

diluculo avatar Jan 08 '19 04:01 diluculo

@cdrnet, I have questions: Are the functions at compilation searched in System.Math or System.Numerics.Complex? Is this why Trigonometric.simplify used? So to speak, to avoid missing functions such as Coth? Is it possible to call functions from MathNet.Numerics.Trig or SpecialFunctions? I'd like to use Coth itself and Bessel functions.

coth(sqrt(10000000 j)) = 1, but sinh(sqrt(10000000 j)) = oo - oo j and cosh(sqrt(10000000 j)) = oo - oo j

diluculo avatar Jan 11 '19 11:01 diluculo

@diluculo : Has your fixing be committed? I just test but the issue still exist.

sadqiang avatar Mar 08 '19 20:03 sadqiang

I found the following enlightenment while I was sleeping. However, this approach is risky because users can accidentally pass values other than i to the second argument.

using System;
using System.Numerics;

using static System.Console;
using static System.Numerics.Complex;
using static MathNet.Symbolics.SymbolicExpression;


    class Program
    {
        static readonly Complex i = ImaginaryOne;

        static void Main()
        {
            var z = Variable("z");
            Func<Complex, Complex, Complex> f = Parse("z * i").CompileComplex(nameof(z), "i");

            Complex c = 1 / 2.0 - i / 3;
            WriteLine(f(c, i));
        }
    }

sadqiang avatar Apr 13 '19 20:04 sadqiang

Why didn't you also use nameof(i)? :-)

using System;
using System.Numerics;

using static System.Console;
using static System.Numerics.Complex;
using static MathNet.Symbolics.SymbolicExpression;


    class Program
    {
        static readonly Complex i = ImaginaryOne;

        static void Main()
        {
            var z = Variable("z");
            Func<Complex, Complex, Complex> f = Parse("z * i").CompileComplex(nameof(z), nameof(i));

            Complex c = 1 / 2.0 - i / 3;
            WriteLine(f(c, i));
        }
    }

mauler2025 avatar Feb 28 '22 20:02 mauler2025