Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mix Complex and SymbolicExpression? #68

Open
sadqiang opened this issue Dec 18, 2018 · 5 comments
Open

How to mix Complex and SymbolicExpression? #68

sadqiang opened this issue Dec 18, 2018 · 5 comments

Comments

@sadqiang
Copy link

sadqiang commented Dec 18, 2018

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
Copy link
Contributor

diluculo commented Jan 8, 2019

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 added a commit to diluculo/mathnet-symbolics that referenced this issue Jan 8, 2019
@diluculo
Copy link
Contributor

@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
Copy link
Author

sadqiang commented Mar 8, 2019

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

@sadqiang
Copy link
Author

sadqiang commented Apr 13, 2019

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants