in

.NET Opensource

Community for opensource projects by Christoph Rüegg

Possible bug in Complex class?

Last post 03-31-2008 12:07 by Christoph Rüegg. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 02-03-2008 10:41

    • LudovicoVan
    • Top 10 Contributor
    • Joined on 01-31-2008
    • Hic abundant leones!
    • Posts 6

    Possible bug in Complex class?

    Hello Mr Ruegg,

    I've been  trying to use your Math.NET library for a simple project of mine where I need some basic complex math, and I must say the whole library looks very interesting and promising.

    Anyway, I have noticed a strange behaviour in that I get an exception if I call the Exponent function on a complex whose value is zero.

    By inspecting the code, I have tracked it down to the NaturalLogarithm function which reads like this:

            /// <summary>Natural Logarithm of this <c>Complex</c> (Base E).</summary>
            public Complex NaturalLogarithm()
            {
                if(IsReal)
                    return new Complex(Math.Log(real), 0d);
                return new Complex(0.5d * Math.Log(ModulusSquared), Argument);
            }

    This of course fails if the number is zero, and so most of the functions in the class would fail because they rely on this NaturalLogarithm function.

    I guess in transforming from rectangular to exponential and back, there should always be checks for zero denominators and stuff like that, anyway my mathematical background is not that strong so I might be just missing something.

    What's the culprit?

    Thanks. -LV

     

  • 02-03-2008 10:53 In reply to

    • LudovicoVan
    • Top 10 Contributor
    • Joined on 01-31-2008
    • Hic abundant leones!
    • Posts 6

    Re: Possible bug in Complex class?

    P.S. I have seen your comment about IsReal that has become IsPositiveReal (http://community.opensourcedotnet.info/forums/t/418.aspx), but I guess that won't solve this one.

    -LV

  • 02-03-2008 11:25 In reply to

    Re: Possible bug in Complex class?

    This should actually work in the new release from yesterday: The natural logarithm of zero returns a new complex value with zero imaginary part and minus infinity real part.
  • 02-03-2008 21:01 In reply to

    • LudovicoVan
    • Top 10 Contributor
    • Joined on 01-31-2008
    • Hic abundant leones!
    • Posts 6

    Re: Possible bug in Complex class?

    Yep, that is true and correct but does not seem to solve the issue. Indeed I now realize my post above was quite a bit off mark.

    If we look at the Power function for instance:

        public Complex Power(Complex exponent)
        {
            return (exponent * NaturalLogarithm()).Exponential();
        }

    This should return Zero when applied to number Zero, unless the exponent is Zero as well, in which case it should return NaN. At the moment it is returning NaN whichever the exponent.

    To reproduce this is fairly simple:

        static void Main(string[ args)
        {
            Console.WriteLine(Complex.Zero.Power(2).ToString());
            Thread.Sleep(Timeout.Infinite);
        }

    By debug-stepping into the Power function, here is what happens:

    1) NaturalLogarithm() correctly returns (re=-Infinity, im=0);

    Then it goes to the * operator:

        public static Complex operator *(Complex multiplicand, Complex multiplier)
        {
            return new Complex(multiplicand.real * multiplier.real - multiplicand.imag * multiplier.imag, multiplicand.real * multiplier.imag + multiplicand.imag * multiplier.real);
        }

    2) Here the * operator fails in calculating the imaginary party, bacause re=2*-Inf-0*0=-Inf is correct, but im=2*0+0*-Inf=NaN is not, and here we get the NaN.

    In general, I guess the basic problem is the built-in double operators and the Math class cannot check the order of an infinite versus the order of an infinitesimal (what above I called checking for zero denominators and the like), so maybe here we have another instance of a special case (like when you check for IsReal or IsRealPositive on top of many functions).

    Well, hope it is not too confused, anyway a very simple test like the above Complex.Zero.Power(2) is enough to show the problem.

    To work around this, my program's code at the moment looks like:

        if(value1.IsZero)
            return Complex.Zero;
        return value1.Power(value2);

    -LV

  • 02-03-2008 21:23 In reply to

    • LudovicoVan
    • Top 10 Contributor
    • Joined on 01-31-2008
    • Hic abundant leones!
    • Posts 6

    Re: Possible bug in Complex class?

    Sorry, should read:

        if(value1.IsZero && value2.IsZero)
            return Complex.NaN;
        if(value1.IsZero)
            return Complex.Zero;
        return value1.Power(value2); 

    -LV

  • 02-08-2008 0:42 In reply to

    Re: Possible bug in Complex class?

    Yes, I think you're right. I'll have a look at all this methods and check for these cases. I'll let you know here when I create a bug in the issue tracker.

    Filed under: , ,
  • 02-08-2008 17:30 In reply to

    • LudovicoVan
    • Top 10 Contributor
    • Joined on 01-31-2008
    • Hic abundant leones!
    • Posts 6

    Re: Possible bug in Complex class?

    Ok, thanks for looking into it.

    Btw, in the meantime I have been trying to write my own Complex struct which is the only thing I need in my current project.

    The goal is to have something that *never* gives back a NaN whichever the arguments, and this is proving to be much more difficult than I could imagine.

    For instance, I am stuck with a basic pair of functions, FixedModulus and FixedAngle, the latter leveraging the first, whose purpose is simply to reduce the angle to the canonical (-pi, pi] range. Trivial, were it not that I am getting mad in defining what should happen at the boundaries, that is when the divisor is 0 or when one or both of the operands are infinite.

    By digging the web I have found something very interesting about "zero-module" and "infinity-module": http://www.gwiep.net/books/parad09.htm. Still it doesn't cover all needed cases and I even wander if what I'm trying to do does make any sense at all.

    Anyway, despite I am nothing more than passionate with Math, I am a professional programmer, so should you be interested in what I am trying, should you be interested in introducing some of this into your own library, and/or should you have any kind of suggestions (even a get a life :)... well, you'd be infinitely welcome!

    In case, just let me know.

    Cheers. -LV

  • 03-12-2008 13:55 In reply to

    Re: Possible bug in Complex class?

    Your issue around Complex Power should be fixed in revision 382, see IRID-107. However, I didn't touch other methods.

    Note that Complex Power still retuns NaN, in the case (0) ^ (z) where Re(z) = 0 but Im(z) != 0.

  • 03-13-2008 7:50 In reply to

    • LudovicoVan
    • Top 10 Contributor
    • Joined on 01-31-2008
    • Hic abundant leones!
    • Posts 6

    Re: Possible bug in Complex class?

    Hi Christoph,

    Thank you for looking into it.

    Keep up the good work. :)

    Cheers.

    -LV

  • 03-31-2008 12:07 In reply to

    Re: Possible bug in Complex class?

    FYI: The Iridium 2008 March Release (v2008.3.12.405) is now available, which includes the mentioned fix.

Page 1 of 1 (10 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems