Beispiel #1
0
        public static ComplexD Log(ComplexD a)
        {
            ComplexD result = ComplexD.Zero;

            if ((a.Real > 0.0) && (a.Imaginary == 0.0))
            {
                result.Real      = System.Math.Log(a.Real);
                result.Imaginary = 0.0;
            }
            else if (a.Real == 0.0)
            {
                if (a.Imaginary > 0.0)
                {
                    result.Real      = System.Math.Log(a.Imaginary);
                    result.Imaginary = MathFunctions.HalfPI;
                }
                else
                {
                    result.Real      = System.Math.Log(-(a.Imaginary));
                    result.Imaginary = -MathFunctions.HalfPI;
                }
            }
            else
            {
                result.Real      = System.Math.Log(a.GetModulus());
                result.Imaginary = System.Math.Atan2(a.Imaginary, a.Real);
            }

            return(result);
        }
Beispiel #2
0
        public static ComplexD Sqrt(ComplexD a)
        {
            ComplexD result = ComplexD.Zero;

            if ((a.Real == 0.0) && (a.Imaginary == 0.0))
            {
                return(result);
            }
            else if (a.Imaginary == 0.0)
            {
                result.Real      = (a.Real > 0) ? System.Math.Sqrt(a.Real) : System.Math.Sqrt(-a.Real);
                result.Imaginary = 0.0;
            }
            else
            {
                double modulus = a.GetModulus();

                result.Real      = System.Math.Sqrt(0.5 * (modulus + a.Real));
                result.Imaginary = System.Math.Sqrt(0.5 * (modulus - a.Real));
                if (a.Imaginary < 0.0)
                {
                    result.Imaginary = -result.Imaginary;
                }
            }

            return(result);
        }
Beispiel #3
0
 /// <summary>
 /// Tests whether two complex numbers are approximately equal given a tolerance value.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="b">A <see cref="ComplexD"/> instance.</param>
 /// <param name="tolerance">The tolerance value used to test approximate equality.</param>
 /// <returns><see langword="true"/> if the two vectors are approximately equal; otherwise, <see langword="false"/>.</returns>
 public static bool ApproxEqual(ComplexD a, ComplexD b, double tolerance)
 {
     return
         (
         (System.Math.Abs(a.Real - b.Real) <= tolerance) &&
         (System.Math.Abs(a.Imaginary - b.Imaginary) <= tolerance)
         );
 }
Beispiel #4
0
        /// <summary>
        /// Multiplies two complex numbers.
        /// </summary>
        /// <param name="a">A <see cref="ComplexD"/> instance.</param>
        /// <param name="b">A <see cref="ComplexD"/> instance.</param>
        /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
        public static ComplexD Multiply(ComplexD a, ComplexD b)
        {
            // (x + yi)(u + vi) = (xu � yv) + (xv + yu)i.
            double x = a.Real, y = a.Imaginary;
            double u = b.Real, v = b.Imaginary;

            return(new ComplexD(x * u - y * v, x * v + y * u));
        }
Beispiel #5
0
 /// <summary>
 /// Returns a value indicating whether this instance is equal to
 /// the specified object.
 /// </summary>
 /// <param name="obj">An object to compare to this instance.</param>
 /// <returns><see langword="true"/> if <paramref name="obj"/> is a <see cref="ComplexD"/> and has the same values as this instance; otherwise, <see langword="false"/>.</returns>
 public override bool Equals(object obj)
 {
     if (obj is ComplexD)
     {
         ComplexD c = (ComplexD)obj;
         return((this.Real == c.Real) && (this.Imaginary == c.Imaginary));
     }
     return(false);
 }
Beispiel #6
0
        /// <summary>
        /// Multiplies two complex numbers and put the result in a third complex number.
        /// </summary>
        /// <param name="a">A <see cref="ComplexD"/> instance.</param>
        /// <param name="b">A <see cref="ComplexD"/> instance.</param>
        /// <param name="result">A <see cref="ComplexD"/> instance to hold the result.</param>
        public static void Multiply(ComplexD a, ComplexD b, ref ComplexD result)
        {
            // (x + yi)(u + vi) = (xu � yv) + (xv + yu)i.
            double x = a.Real, y = a.Imaginary;
            double u = b.Real, v = b.Imaginary;

            result.Real      = x * u - y * v;
            result.Imaginary = x * v + y * u;
        }
Beispiel #7
0
        /// <summary>
        /// Converts the given object to the type of this converter, using the specified context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
        /// <param name="culture">The <see cref="System.Globalization.CultureInfo"/> to use as the current culture. </param>
        /// <param name="value">The <see cref="Object"/> to convert.</param>
        /// <returns>An <see cref="Object"/> that represents the converted value.</returns>
        /// <exception cref="ParseException">Failed parsing from string.</exception>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(string))
            {
                return(ComplexD.Parse((string)value));
            }

            return(base.ConvertFrom(context, culture, value));
        }
Beispiel #8
0
        /// <summary>
        /// Divides a scalar by a complex and put the result into another complex number.
        /// </summary>
        /// <param name="a">A <see cref="ComplexD"/> instance.</param>
        /// <param name="s">A scalar.</param>
        /// <param name="result">A <see cref="ComplexD"/> instance to hold the result.</param>
        public static void Divide(double s, ComplexD a, ref ComplexD result)
        {
            if ((a.Real == 0) || (a.Imaginary == 0))
            {
                throw new DivideByZeroException();
            }

            result.Real      = s / a.Real;
            result.Imaginary = s / a.Imaginary;
        }
Beispiel #9
0
        /// <summary>
        /// Divides a complex by a scalar and put the result into another complex number.
        /// </summary>
        /// <param name="a">A <see cref="ComplexD"/> instance.</param>
        /// <param name="s">A scalar.</param>
        /// <param name="result">A <see cref="ComplexD"/> instance to hold the result.</param>
        public static void Divide(ComplexD a, double s, ref ComplexD result)
        {
            if (s == 0)
            {
                throw new DivideByZeroException();
            }

            result.Real      = a.Real / s;
            result.Imaginary = a.Imaginary / s;
        }
Beispiel #10
0
 /// <summary>
 /// Divides a scalar by a complex.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD Divide(double s, ComplexD a)
 {
     if ((a.Real == 0) || (a.Imaginary == 0))
     {
         throw new DivideByZeroException();
     }
     return(new ComplexD(
                s / a.Real,
                s / a.Imaginary));
 }
Beispiel #11
0
 /// <summary>
 /// Divides a complex by a scalar.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD Divide(ComplexD a, double s)
 {
     if (s == 0)
     {
         throw new DivideByZeroException();
     }
     return(new ComplexD(
                a.Real / s,
                a.Imaginary / s));
 }
Beispiel #12
0
        /// <summary>
        /// Converts the given value object to the specified type, using the specified context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
        /// <param name="culture">A <see cref="System.Globalization.CultureInfo"/> object. If a null reference (Nothing in Visual Basic) is passed, the current culture is assumed.</param>
        /// <param name="value">The <see cref="Object"/> to convert.</param>
        /// <param name="destinationType">The Type to convert the <paramref name="value"/> parameter to.</param>
        /// <returns>An <see cref="Object"/> that represents the converted value.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if ((destinationType == typeof(string)) && (value is ComplexD))
            {
                ComplexD c = (ComplexD)value;
                return(c.ToString());
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
Beispiel #13
0
        public static ComplexD Exp(ComplexD a)
        {
            ComplexD result = ComplexD.Zero;
            double   r      = System.Math.Exp(a.Real);

            result.Real      = r * System.Math.Cos(a.Imaginary);
            result.Imaginary = r * System.Math.Sin(a.Imaginary);

            return(result);
        }
Beispiel #14
0
        /// <summary>
        /// Divides a complex by a complex and put the result in a third complex number.
        /// </summary>
        /// <param name="a">A <see cref="ComplexD"/> instance.</param>
        /// <param name="b">A <see cref="ComplexD"/> instance.</param>
        /// <param name="result">A <see cref="ComplexD"/> instance to hold the result.</param>
        public static void Divide(ComplexD a, ComplexD b, ref ComplexD result)
        {
            double x = a.Real, y = a.Imaginary;
            double u = b.Real, v = b.Imaginary;
            double modulusSquared = u * u + v * v;

            if (modulusSquared == 0)
            {
                throw new DivideByZeroException();
            }

            double invModulusSquared = 1 / modulusSquared;

            result.Real      = (x * u + y * v) * invModulusSquared;
            result.Imaginary = (y * u - x * v) * invModulusSquared;
        }
Beispiel #15
0
        /// <summary>
        /// Divides a complex by a complex.
        /// </summary>
        /// <param name="a">A <see cref="ComplexD"/> instance.</param>
        /// <param name="b">A <see cref="ComplexD"/> instance.</param>
        /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
        public static ComplexD Divide(ComplexD a, ComplexD b)
        {
            double x = a.Real, y = a.Imaginary;
            double u = b.Real, v = b.Imaginary;
            double modulusSquared = u * u + v * v;

            if (modulusSquared == 0)
            {
                throw new DivideByZeroException();
            }

            double invModulusSquared = 1 / modulusSquared;

            return(new ComplexD(
                       (x * u + y * v) * invModulusSquared,
                       (y * u - x * v) * invModulusSquared));
        }
Beispiel #16
0
        public static ComplexD Cos(ComplexD a)
        {
            ComplexD result = ComplexD.Zero;

            if (a.Imaginary == 0.0)
            {
                result.Real      = System.Math.Cos(a.Real);
                result.Imaginary = 0.0;
            }
            else
            {
                result.Real      = System.Math.Cos(a.Real) * System.Math.Cosh(a.Imaginary);
                result.Imaginary = -System.Math.Sin(a.Real) * System.Math.Sinh(a.Imaginary);
            }

            return(result);
        }
Beispiel #17
0
        public static ComplexD Tan(ComplexD a)
        {
            ComplexD result = ComplexD.Zero;

            if (a.Imaginary == 0.0)
            {
                result.Real      = System.Math.Tan(a.Real);
                result.Imaginary = 0.0;
            }
            else
            {
                double real2 = 2 * a.Real;
                double imag2 = 2 * a.Imaginary;
                double denom = System.Math.Cos(real2) + System.Math.Cosh(real2);

                result.Real      = System.Math.Sin(real2) / denom;
                result.Imaginary = System.Math.Sinh(imag2) / denom;
            }

            return(result);
        }
Beispiel #18
0
 /// <summary>
 /// Divides a complex by a scalar.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD operator/(ComplexD a, double s)
 {
     return(ComplexD.Divide(a, s));
 }
Beispiel #19
0
 /// <summary>
 /// Divides a complex by a complex.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="b">A <see cref="ComplexD"/> instance.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD operator/(ComplexD a, ComplexD b)
 {
     return(ComplexD.Divide(a, b));
 }
Beispiel #20
0
 /// <summary>
 /// Multiplies a complex by a scalar.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD operator*(ComplexD a, double s)
 {
     return(ComplexD.Multiply(a, s));
 }
Beispiel #21
0
 /// <summary>
 /// Multiplies two complex numbers.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="b">A <see cref="ComplexD"/> instance.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD operator*(ComplexD a, ComplexD b)
 {
     return(ComplexD.Multiply(a, b));
 }
Beispiel #22
0
 /// <summary>
 /// Subtracts a complex from a scalar.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the difference.</returns>
 public static ComplexD operator-(double s, ComplexD a)
 {
     return(ComplexD.Subtract(s, a));
 }
Beispiel #23
0
 /// <summary>
 /// Subtracts a scalar from a complex.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the difference.</returns>
 public static ComplexD operator-(ComplexD a, double s)
 {
     return(ComplexD.Subtract(a, s));
 }
Beispiel #24
0
 /// <summary>
 /// Adds a complex number and a scalar.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the sum.</returns>
 public static ComplexD operator+(double s, ComplexD a)
 {
     return(ComplexD.Add(a, s));
 }
Beispiel #25
0
 /// <summary>
 /// Adds two complex numbers.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="b">A <see cref="ComplexD"/> instance.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the sum.</returns>
 public static ComplexD operator+(ComplexD a, ComplexD b)
 {
     return(ComplexD.Add(a, b));
 }
Beispiel #26
0
 /// <summary>
 /// Negates the complex number.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the negated values.</returns>
 public static ComplexD operator-(ComplexD a)
 {
     return(ComplexD.Negate(a));
 }
Beispiel #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComplexD"/> class using values from a given complex instance.
 /// </summary>
 /// <param name="c">A complex number to get values from.</param>
 public ComplexD(ComplexD c)
 {
     _real  = c.Real;
     _image = c.Imaginary;
 }
Beispiel #28
0
 /// <summary>
 /// Divides a scalar by a complex.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="s">A scalar.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the result.</returns>
 public static ComplexD operator/(double s, ComplexD a)
 {
     return(ComplexD.Divide(s, a));
 }
Beispiel #29
0
 /// <summary>
 /// Subtracts a complex from a complex.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="b">A <see cref="ComplexD"/> instance.</param>
 /// <returns>A new <see cref="ComplexD"/> instance containing the difference.</returns>
 public static ComplexD operator-(ComplexD a, ComplexD b)
 {
     return(ComplexD.Subtract(a, b));
 }
Beispiel #30
0
 /// <summary>
 /// Tests whether two complex numbers are approximately equal using default tolerance value.
 /// </summary>
 /// <param name="a">A <see cref="ComplexD"/> instance.</param>
 /// <param name="b">A <see cref="ComplexD"/> instance.</param>
 /// <returns><see langword="true"/> if the two vectors are approximately equal; otherwise, <see langword="false"/>.</returns>
 public static bool ApproxEqual(ComplexD a, ComplexD b)
 {
     return(ApproxEqual(a, b, MathFunctions.EpsilonD));
 }