1

I have run into this issue on Community Edition (version 2.14), but recently I have installed a trial version of ILNumerics ver. 6.0.127 and see that the issue still persists.

The code below describes the problem: I start with the Matlab asin(z) example for several input complex z values (provided here: https://www.mathworks.com/help/matlab/ref/asin.html) and show that the ILNumerics ILMath.asin() function produces incorrect results (complex conjugate ones), while the C# Numerics Asin() function provides correct answers.

As a result, in ILNumerics: sin(asin(z)) != z, while in Matlab and in C# Numerics: Sin(Asin(z)) == z.

To obtain the correct results, one should override the ILMath.asin() function and use ILMath.asin(ILMath.conj(z)), as shown in example below:

using System.Numerics;
using ILNumerics;
public class Example
{
    public static void Main()
    {   // start from Matlab example ( https://www.mathworks.com/help/matlab/ref/asin.html ):
        //     x = [ 0.5i,   1 + 3i,   - 2.2 + i ];
        //     y = asin(x)
        //     0 + 0.4812i       0.3076 + 1.8642i        - 1.1091 + 1.5480i

        Console.WriteLine("     C# Complex_values from Matlab example:");
        Complex[] values = { new Complex(0, 0.5), new Complex(1, 3), new Complex(-2.2, 1) };
        foreach (Complex value in values) Console.WriteLine($"value = {value}");
        Console.WriteLine();
        foreach (Complex value in values) Console.WriteLine($"Asin({value}) = {Complex.Asin(value)}");
        Console.WriteLine("\n     Now try to check that Sin(Asin(z)) == z: ");
        foreach (Complex value in values) Console.WriteLine($"Sin(Asin({value})) = {Complex.Sin(Complex.Asin(value))}");
        Console.WriteLine("\n     So, C# gives correct results for Asin() function (+)\n");

        Console.WriteLine("\n     ILNumerics complex_values from Matlab example:\n");
        ILNumerics.Array<complex> valuesIL = new complex[] { 0.5 * complex.i, 1 + 3 * complex.i, -2.2 + 1 * complex.i };
        for (int i = 0; i < valuesIL.Length; i++)
        {
            Console.WriteLine($"valuesIL[{i}] = {valuesIL[i].ToString().Remove(0, 17)}");
        }
        Console.WriteLine("\n     Compare with Matlab (and C#) results: [0+0.4812i   0.3076+1.8642i  -1.1091+1.5480i]");
        for (int i = 0; i < valuesIL.Length; i++)
        {
            Console.WriteLine($"ILMath.asin({valuesIL[i].ToString().Remove(0, 17)}) = " +
                $"{ILMath.asin(valuesIL[i]).ToString().Remove(0, 17)}");
        }
        Console.WriteLine("\n     Now try to check that ILMath.sin(ILMath.asin(z)) == z:");
        for (int i = 0; i < valuesIL.Length; i++)
        {
            Console.WriteLine($"ILMath.sin(ILMath.asin({valuesIL[i].ToString().Remove(0,17)}) = " +
                $"{ILMath.sin(ILMath.asin(valuesIL[i])).ToString().Remove(0, 17)}");
        }
        Console.WriteLine("\n     So, ILNumerics gives wrong results for ILMath.asin() function (-) \n" +
                            "     [one should use ILMath.asin(ILMath.conj(z)) or ILMath.conj(ILMath.asin(z))  \n" + 
                            "     to obtain correct results for complex z in ILNumerics]:\n");
        for (int i = 0; i < valuesIL.Length; i++)
        {
            Console.WriteLine($"ILMath.sin(ILMath.asin(ILMath.conj({valuesIL[i].ToString().Remove(0, 17)})) = " +
                $"{ILMath.sin(ILMath.asin(ILMath.conj(valuesIL[i]))).ToString().Remove(0, 17)}");
        }
        Console.ReadLine();
    }
}

Is this a real bug or am I missing something?

0 Answers0