mathnet / mathnet-symbolics

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

How to mix Complex and SymbolicExpression? #68

Open sadqiang opened 5 years ago

sadqiang commented 5 years ago

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!

diluculo commented 5 years ago

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 commented 5 years ago

@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

sadqiang commented 5 years ago

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

sadqiang commented 5 years ago

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));
        }
    }
pstricks-fans commented 2 years ago

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));
        }
    }