// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // // Copyright (c) 2009-2010 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // #if PORTABLE namespace System.Numerics { using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using MathNet.Numerics; using MathNet.Numerics.Properties; /// /// 64-bit double precision complex numbers class. /// /// /// /// The class Complex provides all elementary operations /// on complex numbers. All the operators +, -, /// *, /, ==, != are defined in the /// canonical way. Additional complex trigonometric functions /// are also provided. Note that the Complex structures /// has two special constant values and /// . /// /// /// /// Complex x = new Complex(1d, 2d); /// Complex y = Complex.FromPolarCoordinates(1d, Math.Pi); /// Complex z = (x + y) / (x - y); /// /// /// /// For mathematical details about complex numbers, please /// have a look at the /// Wikipedia /// /// [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Complex : IFormattable, IEquatable, IPrecisionSupport { /// /// The real component of the complex number. /// private readonly double _real; /// /// The imaginary component of the complex number. /// private readonly double _imag; /// /// Initializes a new instance of the Complex structure with the given real /// and imaginary parts. /// /// The value for the real component. /// The value for the imaginary component. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public Complex(double real, double imaginary) { _real = real; _imag = imaginary; } /// /// Creates a complex number from a point's polar coordinates. /// /// A complex number. /// The magnitude, which is the distance from the origin (the intersection of the x-axis and the y-axis) to the number. /// The phase, which is the angle from the line to the horizontal axis, measured in radians. public static Complex FromPolarCoordinates(double magnitude, double phase) { return new Complex(magnitude * Math.Cos(phase), magnitude * Math.Sin(phase)); } [Obsolete("Use the public constructor instead. Scheduled for removal in v3.0.")] public static Complex WithRealImaginary(double real, double imaginary) { return new Complex(real, imaginary); } [Obsolete("Use static FromPolarCoordinates instead. Scheduled for removal in v3.0.")] public static Complex WithModulusArgument(double modulus, double argument) { if (modulus < 0.0d) { throw new ArgumentOutOfRangeException("modulus", Resources.ArgumentNotNegative); } return new Complex(modulus * Math.Cos(argument), modulus * Math.Sin(argument)); } /// /// Returns a new instance /// with a real number equal to zero and an imaginary number equal to zero. /// public static readonly Complex Zero = new Complex(0.0f, 0.0f); /// /// Returns a new instance /// with a real number equal to one and an imaginary number equal to zero. /// public static readonly Complex One = new Complex(1.0f, 0.0f); /// /// Returns a new instance /// with a real number equal to zero and an imaginary number equal to one. /// public static readonly Complex ImaginaryOne = new Complex(0, 1); /// /// Returns a new instance /// with real and imaginary numbers positive infinite. /// public static readonly Complex PositiveInfinity = new Complex(float.PositiveInfinity, float.PositiveInfinity); [Obsolete("Use PositiveInfinity instead. Scheduled for removal in v3.0.")] public static readonly Complex Infinity = PositiveInfinity; /// /// Returns a new instance /// with real and imaginary numbers not a number. /// public static readonly Complex NaN = new Complex(float.NaN, float.NaN); /// /// Gets the real component of the complex number. /// /// The real component of the complex number. public double Real { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return _real; } } /// /// Gets the real imaginary component of the complex number. /// /// The real imaginary component of the complex number. public double Imaginary { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return _imag; } } /// /// Gets the phase or argument of this Complex. /// /// /// Phase always returns a value bigger than negative Pi and /// smaller or equal to Pi. If this Complex is zero, the Complex /// is assumed to be positive real with an argument of zero. /// /// The phase or argument of this Complex public double Phase { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return Math.Atan2(_imag, _real); } } /// /// Gets the magnitude (or absolute value) of a complex number. /// /// The magnitude of the current instance. public double Magnitude { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return Math.Sqrt((_real * _real) + (_imag * _imag)); } } /// /// Equality test. /// /// One of complex numbers to compare. /// The other complex numbers to compare. /// true if the real and imaginary components of the two complex numbers are equal; false otherwise. public static bool operator ==(Complex complex1, Complex complex2) { return complex1.Equals(complex2); } /// /// Inequality test. /// /// One of complex numbers to compare. /// The other complex numbers to compare. /// true if the real or imaginary components of the two complex numbers are not equal; false otherwise. public static bool operator !=(Complex complex1, Complex complex2) { return !complex1.Equals(complex2); } /// /// Unary addition. /// /// The complex number to operate on. /// Returns the same complex number. public static Complex operator +(Complex summand) { return summand; } /// /// Unary minus. /// /// The complex number to operate on. /// The negated value of the . public static Complex operator -(Complex subtrahend) { return new Complex(-subtrahend._real, -subtrahend._imag); } /// Addition operator. Adds two complex numbers together. /// The result of the addition. /// One of the complex numbers to add. /// The other complex numbers to add. public static Complex operator +(Complex summand1, Complex summand2) { return new Complex(summand1._real + summand2._real, summand1._imag + summand2._imag); } /// Subtraction operator. Subtracts two complex numbers. /// The result of the subtraction. /// The complex number to subtract from. /// The complex number to subtract. public static Complex operator -(Complex minuend, Complex subtrahend) { return new Complex(minuend._real - subtrahend._real, minuend._imag - subtrahend._imag); } /// Addition operator. Adds a complex number and double together. /// The result of the addition. /// The complex numbers to add. /// The double value to add. public static Complex operator +(Complex summand1, double summand2) { return new Complex(summand1._real + summand2, summand1._imag); } /// Subtraction operator. Subtracts double value from a complex value. /// The result of the subtraction. /// The complex number to subtract from. /// The double value to subtract. public static Complex operator -(Complex minuend, double subtrahend) { return new Complex(minuend._real - subtrahend, minuend._imag); } /// Addition operator. Adds a complex number and double together. /// The result of the addition. /// The double value to add. /// The complex numbers to add. public static Complex operator +(double summand1, Complex summand2) { return new Complex(summand2._real + summand1, summand2._imag); } /// Subtraction operator. Subtracts complex value from a double value. /// The result of the subtraction. /// The double vale to subtract from. /// The complex value to subtract. public static Complex operator -(double minuend, Complex subtrahend) { return new Complex(minuend - subtrahend._real, -subtrahend._imag); } /// Multiplication operator. Multiplies two complex numbers. /// The result of the multiplication. /// One of the complex numbers to multiply. /// The other complex number to multiply. 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)); } /// Multiplication operator. Multiplies a complex number with a double value. /// The result of the multiplication. /// The double value to multiply. /// The complex number to multiply. public static Complex operator *(double multiplicand, Complex multiplier) { return new Complex(multiplier._real * multiplicand, multiplier._imag * multiplicand); } /// Multiplication operator. Multiplies a complex number with a double value. /// The result of the multiplication. /// The complex number to multiply. /// The double value to multiply. public static Complex operator *(Complex multiplicand, double multiplier) { return new Complex(multiplicand._real * multiplier, multiplicand._imag * multiplier); } /// Division operator. Divides a complex number by another. /// The result of the division. /// The dividend. /// The divisor. public static Complex operator /(Complex dividend, Complex divisor) { if (dividend.IsZero() && divisor.IsZero()) { return NaN; } if (divisor.IsZero()) { return PositiveInfinity; } var modSquared = divisor.MagnitudeSquared(); return new Complex( ((dividend._real * divisor._real) + (dividend._imag * divisor._imag)) / modSquared, ((dividend._imag * divisor._real) - (dividend._real * divisor._imag)) / modSquared); } /// Division operator. Divides a double value by a complex number. /// The result of the division. /// The dividend. /// The divisor. public static Complex operator /(double dividend, Complex divisor) { if (dividend == 0.0d && divisor.IsZero()) { return NaN; } if (divisor.IsZero()) { return PositiveInfinity; } var zmod = divisor.MagnitudeSquared(); return new Complex(dividend * divisor._real / zmod, -dividend * divisor._imag / zmod); } /// Division operator. Divides a complex number by a double value. /// The result of the division. /// The dividend. /// The divisor. public static Complex operator /(Complex dividend, double divisor) { if (dividend.IsZero() && divisor == 0.0d) { return NaN; } if (divisor == 0.0d) { return PositiveInfinity; } return new Complex(dividend._real / divisor, dividend._imag / divisor); } #region IFormattable Members /// /// A string representation of this complex number. /// /// /// The string representation of this complex number. /// public override string ToString() { return ToString(null, null); } /// /// A string representation of this complex number. /// /// /// The string representation of this complex number formatted as specified by the /// format string. /// /// /// A format specification. /// public string ToString(string format) { return ToString(format, null); } /// /// A string representation of this complex number. /// /// /// The string representation of this complex number formatted as specified by the /// format provider. /// /// /// An that supplies culture-specific formatting information. /// public string ToString(IFormatProvider formatProvider) { return ToString(null, formatProvider); } /// /// A string representation of this complex number. /// /// /// The string representation of this complex number formatted as specified by the /// format string and format provider. /// /// /// if the n, is not a number. /// /// /// if s, is . /// /// /// A format specification. /// /// /// An that supplies culture-specific formatting information. /// public string ToString(string format, IFormatProvider formatProvider) { var ret = new StringBuilder(); ret.Append("(").Append(_real.ToString(format, formatProvider)).Append(", ").Append(_imag.ToString(format, formatProvider)).Append(")"); return ret.ToString(); } #endregion #region IEquatable Members /// /// Checks if two complex numbers are equal. Two complex numbers are equal if their /// corresponding real and imaginary components are equal. /// /// /// Returns true if the two objects are the same object, or if their corresponding /// real and imaginary components are equal, false otherwise. /// /// /// The complex number to compare to with. /// public bool Equals(Complex other) { if (this.IsNaN() || other.IsNaN()) { return false; } if (this.IsInfinity() && other.IsInfinity()) { return true; } return _real.AlmostEqual(other._real) && _imag.AlmostEqual(other._imag); } /// /// The hash code for the complex number. /// /// /// The hash code of the complex number. /// /// /// The hash code is calculated as /// System.Math.Exp(ComplexMath.Absolute(complexNumber)). /// public override int GetHashCode() { int hash = 27; hash = (13 * hash) + _real.GetHashCode(); hash = (13 * hash) + _imag.GetHashCode(); return hash; } /// /// Checks if two complex numbers are equal. Two complex numbers are equal if their /// corresponding real and imaginary components are equal. /// /// /// Returns true if the two objects are the same object, or if their corresponding /// real and imaginary components are equal, false otherwise. /// /// /// The complex number to compare to with. /// public override bool Equals(object obj) { return (obj is Complex) && Equals((Complex)obj); } #endregion #region IPrecisionSupport /// /// Returns a Norm of a value of this type, which is appropriate for measuring how /// close this value is to zero. /// /// /// A norm of this value. /// double IPrecisionSupport.Norm() { return this.MagnitudeSquared(); } /// /// Returns a Norm of the difference of two values of this type, which is /// appropriate for measuring how close together these two values are. /// /// /// The value to compare with. /// /// /// A norm of the difference between this and the other value. /// double IPrecisionSupport.NormOfDifference(Complex otherValue) { return (this - otherValue).MagnitudeSquared(); } #endregion #region Parse Functions /// /// Creates a complex number based on a string. The string can be in the /// following formats (without the quotes): 'n', 'ni', 'n +/- ni', /// 'ni +/- n', 'n,n', 'n,ni,' '(n,n)', or '(n,ni)', where n is a double. /// /// /// A complex number containing the value specified by the given string. /// /// /// The string to parse. /// public static Complex Parse(string value) { return Parse(value, null); } /// /// Creates a complex number based on a string. The string can be in the /// following formats (without the quotes): 'n', 'ni', 'n +/- ni', /// 'ni +/- n', 'n,n', 'n,ni,' '(n,n)', or '(n,ni)', where n is a double. /// /// /// A complex number containing the value specified by the given string. /// /// /// the string to parse. /// /// /// An that supplies culture-specific /// formatting information. /// public static Complex Parse(string value, IFormatProvider formatProvider) { if (value == null) { throw new ArgumentNullException(value); } value = value.Trim(); if (value.Length == 0) { throw new FormatException(); } // strip out parens if (value.StartsWith("(", StringComparison.Ordinal)) { if (!value.EndsWith(")", StringComparison.Ordinal)) { throw new FormatException(); } value = value.Substring(1, value.Length - 2).Trim(); } // keywords var numberFormatInfo = formatProvider.GetNumberFormatInfo(); var textInfo = formatProvider.GetTextInfo(); var keywords = new[] { textInfo.ListSeparator, numberFormatInfo.NaNSymbol, numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, "+", "-", "i", "j" }; // lexing var tokens = new LinkedList(); GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); var token = tokens.First; // parse the left part bool isLeftPartImaginary; var leftPart = ParsePart(ref token, out isLeftPartImaginary, formatProvider); if (token == null) { return isLeftPartImaginary ? new Complex(0, leftPart) : new Complex(leftPart, 0); } // parse the right part if (token.Value == textInfo.ListSeparator) { // format: real,imag token = token.Next; if (isLeftPartImaginary) { // left must not contain 'i', right doesn't matter. throw new FormatException(); } bool isRightPartImaginary; var rightPart = ParsePart(ref token, out isRightPartImaginary, formatProvider); return new Complex(leftPart, rightPart); } else { // format: real + imag bool isRightPartImaginary; var rightPart = ParsePart(ref token, out isRightPartImaginary, formatProvider); if (!(isLeftPartImaginary ^ isRightPartImaginary)) { // either left or right part must contain 'i', but not both. throw new FormatException(); } return isLeftPartImaginary ? new Complex(rightPart, leftPart) : new Complex(leftPart, rightPart); } } /// /// Parse a part (real or complex) from a complex number. /// /// Start Token. /// Is set to true if the part identified itself as being imaginary. /// /// An that supplies culture-specific /// formatting information. /// /// Resulting part as double. /// private static double ParsePart(ref LinkedListNode token, out bool imaginary, IFormatProvider format) { imaginary = false; if (token == null) { throw new FormatException(); } // handle prefix modifiers if (token.Value == "+") { token = token.Next; if (token == null) { throw new FormatException(); } } var negative = false; if (token.Value == "-") { negative = true; token = token.Next; if (token == null) { throw new FormatException(); } } // handle prefix imaginary symbol if (String.Compare(token.Value, "i", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(token.Value, "j", StringComparison.OrdinalIgnoreCase) == 0) { imaginary = true; token = token.Next; if (token == null) { return negative ? -1 : 1; } } var value = GlobalizationHelper.ParseSingle(ref token); // handle suffix imaginary symbol if (token != null && (String.Compare(token.Value, "i", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(token.Value, "j", StringComparison.OrdinalIgnoreCase) == 0)) { if (imaginary) { // only one time allowed: either prefix or suffix, or neither. throw new FormatException(); } imaginary = true; token = token.Next; } return negative ? -value : value; } /// /// Converts the string representation of a complex number to a single-precision complex number equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// /// A string containing a complex number to convert. /// /// /// The parsed value. /// /// /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized /// public static bool TryParse(string value, out Complex result) { return TryParse(value, null, out result); } /// /// Converts the string representation of a complex number to single-precision complex number equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// /// A string containing a complex number to convert. /// /// /// An that supplies culture-specific formatting information about value. /// /// /// The parsed value. /// /// /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized /// public static bool TryParse(string value, IFormatProvider formatProvider, out Complex result) { bool ret; try { result = Parse(value, formatProvider); ret = true; } catch (ArgumentNullException) { result = Zero; ret = false; } catch (FormatException) { result = Zero; ret = false; } return ret; } #endregion #region Conversion /// /// Explicit conversion of a real decimal to a Complex. /// /// The decimal value to convert. /// The result of the conversion. public static explicit operator Complex(decimal value) { return new Complex((double)value, 0.0d); } /// /// Explicit conversion of a Complex to a Complex. /// /// The decimal value to convert. /// The result of the conversion. public static explicit operator Complex(Complex32 value) { return new Complex((double)value.Real, (double)value.Imaginary); } /// /// Implicit conversion of a real byte to a Complex. /// /// The byte value to convert. /// The result of the conversion. public static implicit operator Complex(byte value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real short to a Complex. /// /// The short value to convert. /// The result of the conversion. public static implicit operator Complex(short value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a signed byte to a Complex. /// /// The signed byte value to convert. /// The result of the conversion. [CLSCompliant(false)] public static implicit operator Complex(sbyte value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a unsgined real short to a Complex. /// /// The unsgined short value to convert. /// The result of the conversion. [CLSCompliant(false)] public static implicit operator Complex(ushort value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real int to a Complex. /// /// The int value to convert. /// The result of the conversion. public static implicit operator Complex(int value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real long to a Complex. /// /// The long value to convert. /// The result of the conversion. public static implicit operator Complex(long value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real uint to a Complex. /// /// The uint value to convert. /// The result of the conversion. [CLSCompliant(false)] public static implicit operator Complex(uint value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real ulong to a Complex. /// /// The ulong value to convert. /// The result of the conversion. [CLSCompliant(false)] public static implicit operator Complex(ulong value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real double to a Complex. /// /// The double value to convert. /// The result of the conversion. public static implicit operator Complex(double value) { return new Complex(value, 0.0d); } /// /// Implicit conversion of a real float to a Complex. /// /// The double value to convert. /// The result of the conversion. public static explicit operator Complex(float value) { return new Complex((double)value, 0.0d); } /// /// Converts this Complex to a . /// /// A with the same values as this Complex. public Complex ToComplex() { return new Complex(_real, _imag); } #endregion /// /// Returns the additive inverse of a specified complex number. /// /// The result of the and components of the parameter multiplied by -1. /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Negate(Complex value) { return -value; } /// /// Computes the conjugate of a complex number and returns the result. /// /// The conjugate of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Conjugate(Complex value) { return new Complex(value._real, -value._imag); } /// /// Adds two complex numbers and returns the result. /// /// The sum of and . /// The first complex number to add. /// The second complex number to add. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Add(Complex left, Complex right) { return left + right; } /// /// Subtracts one complex number from another and returns the result. /// /// The result of subtracting from . /// The value to subtract from (the minuend). /// The value to subtract (the subtrahend). [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Subtract(Complex left, Complex right) { return left - right; } /// /// Returns the product of two complex numbers. /// /// The product of the and parameters. /// The first complex number to multiply. /// The second complex number to multiply. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Multiply(Complex left, Complex right) { return left * right; } /// /// Divides one complex number by another and returns the result. /// /// The quotient of the division. /// The complex number to be divided. /// The complex number to divide by. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Divide(Complex dividend, Complex divisor) { return dividend / divisor; } /// /// Returns the multiplicative inverse of a complex number. /// /// The reciprocal of . /// A complex number. public static Complex Reciprocal(Complex value) { if (value.IsZero()) { return Zero; } return 1.0d / value; } /// /// Returns the square root of a specified complex number. /// /// The square root of . /// A complex number. public static Complex Sqrt(Complex value) { if (value.IsRealNonNegative()) { return new Complex(Math.Sqrt(value.Real), 0.0); } Complex result; var absReal = Math.Abs(value.Real); var absImag = Math.Abs(value.Imaginary); double w; if (absReal >= absImag) { var ratio = value.Imaginary / value.Real; w = Math.Sqrt(absReal) * Math.Sqrt(0.5 * (1.0 + Math.Sqrt(1.0 + (ratio * ratio)))); } else { var ratio = value.Real / value.Imaginary; w = Math.Sqrt(absImag) * Math.Sqrt(0.5 * (Math.Abs(ratio) + Math.Sqrt(1.0 + (ratio * ratio)))); } if (value.Real >= 0.0) { result = new Complex(w, value.Imaginary / (2.0 * w)); } else if (value.Imaginary >= 0.0) { result = new Complex(absImag / (2.0 * w), w); } else { result = new Complex(absImag / (2.0 * w), -w); } return result; } /// /// Gets the absolute value (or magnitude) of a complex number. /// /// A complex number. /// The absolute value (or magnitude) of a complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static double Abs(Complex value) { return value.Magnitude; } /// /// Returns e raised to the power specified by a complex number. /// /// The number e raised to the power . /// A complex number that specifies a power. public static Complex Exp(Complex value) { var exp = Math.Exp(value.Real); if (value.IsReal()) { return new Complex(exp, 0.0); } return new Complex(exp * Trig.Cosine(value.Imaginary), exp * Trig.Sine(value.Imaginary)); } /// /// Returns a specified complex number raised to a power specified by a complex number. /// /// The complex number raised to the power . /// A complex number to be raised to a power. /// A complex number that specifies a power. public static Complex Pow(Complex value, Complex power) { if (value.IsZero()) { if (power.IsZero()) { return One; } if (power.Real > 0.0) { return Zero; } if (power.Real < 0) { if (power.Imaginary == 0.0) { return new Complex(double.PositiveInfinity, 0.0); } return new Complex(double.PositiveInfinity, double.PositiveInfinity); } return double.NaN; } return Exp(power * Log(value)); } /// /// Returns a specified complex number raised to a power specified by a double-precision floating-point number. /// /// The complex number raised to the power . /// A complex number to be raised to a power. /// A double-precision floating-point number that specifies a power. public static Complex Pow(Complex value, double power) { if (value.IsZero()) { if (power == 0d) { return One; } return power > 0d ? Zero : new Complex(double.PositiveInfinity, 0.0); } return Exp(power * Log(value)); } /// /// Returns the natural (base e) logarithm of a specified complex number. /// /// The natural (base e) logarithm of . /// A complex number. public static Complex Log(Complex value) { if (value.IsRealNonNegative()) { return new Complex(Math.Log(value.Real), 0.0); } return new Complex(0.5 * Math.Log(value.MagnitudeSquared()), value.Phase); } /// /// Returns the logarithm of a specified complex number in a specified base. /// /// The logarithm of in base . /// A complex number. /// The base of the logarithm. public static Complex Log(Complex value, double baseValue) { return Log(value) / Math.Log(baseValue); } /// /// Returns the base-10 logarithm of a specified complex number. /// /// The base-10 logarithm of . /// A complex number. public static Complex Log10(Complex value) { return Log(value) / Constants.Ln10; } /// /// Returns the sine of the specified complex number. /// /// The sine of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Sin(Complex value) { return Trig.Sine(value); } /// /// Returns the cosine of the specified complex number. /// /// The cosine of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Cos(Complex value) { return Trig.Cosine(value); } /// /// Returns the tangent of the specified complex number. /// /// The tangent of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Tan(Complex value) { return Trig.Tangent(value); } /// /// Returns the angle that is the arc sine of the specified complex number. /// /// The angle which is the arc sine of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Asin(Complex value) { return Trig.InverseSine(value); } /// /// Returns the angle that is the arc cosine of the specified complex number. /// /// The angle, measured in radians, which is the arc cosine of . /// A complex number that represents a cosine. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Acos(Complex value) { return Trig.InverseCosine(value); } /// /// Returns the angle that is the arc tangent of the specified complex number. /// /// The angle that is the arc tangent of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Atan(Complex value) { return Trig.InverseTangent(value); } /// /// Returns the hyperbolic sine of the specified complex number. /// /// The hyperbolic sine of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Sinh(Complex value) { return Trig.HyperbolicSine(value); } /// /// Returns the hyperbolic cosine of the specified complex number. /// /// The hyperbolic cosine of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Cosh(Complex value) { return Trig.HyperbolicCosine(value); } /// /// Returns the hyperbolic tangent of the specified complex number. /// /// The hyperbolic tangent of . /// A complex number. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Complex Tanh(Complex value) { return Trig.HyperbolicTangent(value); } } } #endif