2010-07-08 Jb Evain <jbevain@novell.com>
authorJb Evain <jbevain@gmail.com>
Thu, 8 Jul 2010 10:22:16 +0000 (10:22 -0000)
committerJb Evain <jbevain@gmail.com>
Thu, 8 Jul 2010 10:22:16 +0000 (10:22 -0000)
* Complex.cs: Fix Phase. Implement Acos, Asin, Atan, Exp,
Log, Log10, Pow and Sqrt.

svn path=/trunk/mcs/; revision=160039

mcs/class/System.Numerics/System.Numerics/ChangeLog
mcs/class/System.Numerics/System.Numerics/Complex.cs

index 01204baf424425609b3004a0ca3ef82752eeb246..42efed4573530101a7dbfe2534de3818a26223d9 100644 (file)
@@ -1,3 +1,8 @@
+2010-07-08  Jb Evain  <jbevain@novell.com>
+
+       * Complex.cs: Fix Phase. Implement Acos, Asin, Atan, Exp,
+       Log, Log10, Pow and Sqrt.
+
 2010-04-23  Marek Safar <marek.safar@gmail.com>
 
        * Complex.cs: Add explicit BigInteger operator.
index c5d94e0970e09144a8404f63200aaf2ec61bc632..0162ccbbdf91e7306f96b104eafa9c46add77aca 100644 (file)
@@ -4,17 +4,14 @@
 // Author:
 //   Miguel de Icaza (miguel@gnome.org)
 //   Marek Safar (marek.safar@gmail.com)
+//   Jb Evain (jbevain@novell.com)
 //
 // Copyright 2009, 2010 Novell, Inc.
 //
-//
-//
 // TODO:
 // string ToString (string format, IFormatProvider)
 // string ToString (string format)
 // string ToString (IFormatProvider)
-//
-// Acos, Asin, Atan, Exp, Log, Log10, Pow, Sqrt
 
 using System;
 
@@ -27,6 +24,22 @@ namespace System.Numerics {
                public static readonly Complex ImaginaryOne = new Complex (0, 1);
                public static readonly Complex One = new Complex (1, 0);
                public static readonly Complex Zero = new Complex (0, 0);
+
+               public double Imaginary {
+                       get { return imaginary; }
+               }
+
+               public double Real {
+                       get { return real; }
+               }
+
+               public double Magnitude {
+                       get { return Math.Sqrt (imaginary * imaginary + real * real); }
+               }
+
+               public double Phase {
+                       get { return Math.Atan2 (imaginary, real); }
+               }
                
                public Complex (double real, double imaginary)
                {
@@ -245,25 +258,62 @@ namespace System.Numerics {
                {
                        return Sinh (value) / Cosh (value);
                }
-               
-               public override int GetHashCode ()
+
+               public static Complex Acos (Complex value)
                {
-                       return real.GetHashCode () ^ imaginary.GetHashCode ();
+                       return -ImaginaryOne * Log (value + (ImaginaryOne * Sqrt (One - (value * value))));
                }
-               
-               public double Imaginary { get { return imaginary; } }
-               public double Real { get { return real; } }
 
-               public double Magnitude {
-                       get {
-                               return Math.Sqrt (imaginary * imaginary + real * real);
-                       }
+               public static Complex Asin (Complex value)
+               {
+                       return -ImaginaryOne * Log ((ImaginaryOne * value) + Sqrt (One - (value * value)));
                }
 
-               public double Phase {
-                       get {
-                               return Math.Atan (imaginary / real);
-                       }
+               public static Complex Atan (Complex value)
+               {
+                       return (ImaginaryOne / new Complex (2, 0)) * (Log (One - (ImaginaryOne * value)) - Log (One + (ImaginaryOne * value)));
+               }
+
+               public static Complex Exp (Complex value)
+               {
+                       var e = Math.Exp (value.real);
+
+                       return new Complex (e * Math.Cos (value.imaginary), e * Math.Sin (value.imaginary));
+               }
+
+               public static Complex Log (Complex value)
+               {
+                       return new Complex (Math.Log (Abs (value)), value.Phase);
+               }
+
+               public static Complex Log (Complex value, double baseValue)
+               {
+                       return Log (value)/ Log (new Complex (baseValue, 0));
+               }
+
+               public static Complex Log10 (Complex value)
+               {
+                       return Log (value, 10);
+               }
+
+               public static Complex Sqrt (Complex value)
+               {
+                       return FromPolarCoordinates (Math.Sqrt (value.Magnitude), value.Phase / 2);
+               }
+
+               public static Complex Pow (Complex value, double power)
+               {
+                       return Pow (value, new Complex (power, 0));
+               }
+
+               public static Complex Pow (Complex value, Complex power)
+               {
+                       return Exp (Log (value) * power);
+               }
+
+               public override int GetHashCode ()
+               {
+                       return real.GetHashCode () ^ imaginary.GetHashCode ();
                }
        }
 }