Complex Structure

 

Represents a complex number.

Namespace:   System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

<SerializableAttribute>
Public Structure Complex
	Implements IEquatable(Of Complex), IFormattable

NameDescription
System_CAPS_pubmethodComplex(Double, Double)

Initializes a new instance of the Complex structure using the specified real and imaginary values.

NameDescription
System_CAPS_pubpropertyImaginary

Gets the imaginary component of the current Complex object.

System_CAPS_pubpropertyMagnitude

Gets the magnitude (or absolute value) of a complex number.

System_CAPS_pubpropertyPhase

Gets the phase of a complex number.

System_CAPS_pubpropertyReal

Gets the real component of the current Complex object.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticAbs(Complex)

Gets the absolute value (or magnitude) of a complex number.

System_CAPS_pubmethodSystem_CAPS_staticAcos(Complex)

Returns the angle that is the arc cosine of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticAdd(Complex, Complex)

Adds two complex numbers and returns the result.

System_CAPS_pubmethodSystem_CAPS_staticAsin(Complex)

Returns the angle that is the arc sine of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticAtan(Complex)

Returns the angle that is the arc tangent of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticConjugate(Complex)

Computes the conjugate of a complex number and returns the result.

System_CAPS_pubmethodSystem_CAPS_staticCos(Complex)

Returns the cosine of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticCosh(Complex)

Returns the hyperbolic cosine of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticDivide(Complex, Complex)

Divides one complex number by another and returns the result.

System_CAPS_pubmethodEquals(Complex)

Returns a value that indicates whether the current instance and a specified complex number have the same value.

System_CAPS_pubmethodEquals(Object)

Returns a value that indicates whether the current instance and a specified object have the same value. (Overrides ValueType.Equals(Object).)

System_CAPS_pubmethodSystem_CAPS_staticExp(Complex)

Returns e raised to the power specified by a complex number.

System_CAPS_pubmethodSystem_CAPS_staticFromPolarCoordinates(Double, Double)

Creates a complex number from a point's polar coordinates.

System_CAPS_pubmethodGetHashCode()

Returns the hash code for the current Complex object.(Overrides ValueType.GetHashCode().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticLog(Complex)

Returns the natural (base e) logarithm of a specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticLog(Complex, Double)

Returns the logarithm of a specified complex number in a specified base.

System_CAPS_pubmethodSystem_CAPS_staticLog10(Complex)

Returns the base-10 logarithm of a specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticMultiply(Complex, Complex)

Returns the product of two complex numbers.

System_CAPS_pubmethodSystem_CAPS_staticNegate(Complex)

Returns the additive inverse of a specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticPow(Complex, Complex)

Returns a specified complex number raised to a power specified by a complex number.

System_CAPS_pubmethodSystem_CAPS_staticPow(Complex, Double)

Returns a specified complex number raised to a power specified by a double-precision floating-point number.

System_CAPS_pubmethodSystem_CAPS_staticReciprocal(Complex)

Returns the multiplicative inverse of a complex number.

System_CAPS_pubmethodSystem_CAPS_staticSin(Complex)

Returns the sine of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticSinh(Complex)

Returns the hyperbolic sine of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticSqrt(Complex)

Returns the square root of a specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticSubtract(Complex, Complex)

Subtracts one complex number from another and returns the result.

System_CAPS_pubmethodSystem_CAPS_staticTan(Complex)

Returns the tangent of the specified complex number.

System_CAPS_pubmethodSystem_CAPS_staticTanh(Complex)

Returns the hyperbolic tangent of the specified complex number.

System_CAPS_pubmethodToString()

Converts the value of the current complex number to its equivalent string representation in Cartesian form.(Overrides ValueType.ToString().)

System_CAPS_pubmethodToString(IFormatProvider)

Converts the value of the current complex number to its equivalent string representation in Cartesian form by using the specified culture-specific formatting information.

System_CAPS_pubmethodToString(String)

Converts the value of the current complex number to its equivalent string representation in Cartesian form by using the specified format for its real and imaginary parts.

System_CAPS_pubmethodToString(String, IFormatProvider)

Converts the value of the current complex number to its equivalent string representation in Cartesian form by using the specified format and culture-specific format information for its real and imaginary parts.

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticImaginaryOne

Returns a new Complex instance with a real number equal to zero and an imaginary number equal to one.

System_CAPS_pubfieldSystem_CAPS_staticOne

Returns a new Complex instance with a real number equal to one and an imaginary number equal to zero.

System_CAPS_pubfieldSystem_CAPS_staticZero

Returns a new Complex instance with a real number equal to zero and an imaginary number equal to zero.

NameDescription
System_CAPS_puboperatorSystem_CAPS_staticAddition(Complex, Complex)

Adds two complex numbers.

System_CAPS_puboperatorSystem_CAPS_staticDivision(Complex, Complex)

Divides a specified complex number by another specified complex number.

System_CAPS_puboperatorSystem_CAPS_staticEquality(Complex, Complex)

Returns a value that indicates whether two complex numbers are equal.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(BigInteger to Complex)

Defines an explicit conversion of a BigInteger value to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(Decimal to Complex)

Defines an explicit conversion of a Decimal value to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(Byte to Complex)

Defines an implicit conversion of an unsigned byte to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(Double to Complex)

Defines an implicit conversion of a double-precision floating-point number to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(Int16 to Complex)

Defines an implicit conversion of a 16-bit signed integer to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(Int32 to Complex)

Defines an implicit conversion of a 32-bit signed integer to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(Int64 to Complex)

Defines an implicit conversion of a 64-bit signed integer to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(SByte to Complex)

Defines an implicit conversion of a signed byte to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(Single to Complex)

Defines an implicit conversion of a single-precision floating-point number to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(UInt16 to Complex)

Defines an implicit conversion of a 16-bit unsigned integer to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(UInt32 to Complex)

Defines an implicit conversion of a 32-bit unsigned integer to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticWidening(UInt64 to Complex)

Defines an implicit conversion of a 64-bit unsigned integer to a complex number.

System_CAPS_puboperatorSystem_CAPS_staticInequality(Complex, Complex)

Returns a value that indicates whether two complex numbers are not equal.

System_CAPS_puboperatorSystem_CAPS_staticMultiply(Complex, Complex)

Multiplies two specified complex numbers.

System_CAPS_puboperatorSystem_CAPS_staticSubtraction(Complex, Complex)

Subtracts a complex number from another complex number.

System_CAPS_puboperatorSystem_CAPS_staticUnaryNegation(Complex)

Returns the additive inverse of a specified complex number.

A complex number is a number that comprises a real number part and an imaginary number part. A complex number z is usually written in the form z = x + yi, where x and y are real numbers, and i is the imaginary unit that has the property i2 = -1. The real part of the complex number is represented by x, and the imaginary part of the complex number is represented by y.

The Complex type uses the Cartesian coordinate system (real, imaginary) when instantiating and manipulating complex numbers. A complex number can be represented as a point in a two-dimensional coordinate system, which is known as the complex plane. The real part of the complex number is positioned on the x-axis (the horizontal axis), and the imaginary part is positioned on the y-axis (the vertical axis).

Any point in the complex plane can also be expressed based on its absolute value, by using the polar coordinate system., In polar coordinates, a point is characterized by two numbers:

  • Its magnitude, which is the distance of the point from the origin (that is, 0,0, or the point at which the x-axis and the y-axis intersect).

  • Its phase, which is the angle between the real axis and the line drawn from the origin to the point.

You can assign a value to a complex number in one of the following ways:

  • By passing two Double values to its constructor. The first value represents the real part of the complex number, and the second value represents its imaginary part. These values represent the position of the complex number in the two-dimensional Cartesian coordinate system.

  • By calling the static (Shared in Visual Basic) Complex.FromPolarCoordinates method to create a complex number from its polar coordinates.

  • By assigning a Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, or Double value to a Complex object. The value becomes the real part of the complex number, and its imaginary part equals 0.

  • By casting (in C#) or converting (in Visual Basic) a Decimal or BigInteger value to a Complex object. The value becomes the real part of the complex number, and its imaginary part equals 0.

  • By assigning the complex number that is returned by a method or operator to a Complex object. For example, Complex.Add is a static method that returns a complex number that is the sum of two complex numbers, and the Complex.Addition operator adds two complex numbers and returns the result.

The following example demonstrates each of these five ways of assigning a value to a complex number.

Imports System.Numerics

Module Example
   Public Sub Main()
      ' Create a complex number by calling its class constructor.
      Dim c1 As New Complex(12, 6)
      Console.WriteLine(c1)

      ' Assign a Double to a complex number.
      Dim c2 As Complex = 3.14
      Console.WriteLine(c2)

      ' Cast a Decimal to a complex number.
      Dim c3 As Complex = CType(12.3d, Complex)
      Console.WriteLine(c3)

      ' Assign the return value of a method to a Complex variable.
      Dim c4 As Complex = Complex.Pow(Complex.One, -1)
      Console.WriteLine(c4)

      ' Assign the value returned by an operator to a Complex variable.
      Dim c5 As Complex = Complex.One + Complex.One
      Console.WriteLine(c5)

      ' Instantiate a complex number from its polar coordinates.
      Dim c6 As Complex = Complex.FromPolarCoordinates(10, .524)
      Console.WriteLine(c6)
   End Sub
End Module
' The example displays the following output:
'       (12, 6)
'       (3.14, 0)
'       (12.3000001907349, 0)
'       (1, 0)
'       (2, 0)
'       (8.65824721882145, 5.00347430269914)

The Complex structure in the .NET Framework includes members that provide the following functionality:

  • Methods to compare two complex numbers to determine whether they are equal.

  • Operators to perform arithmetic operations on complex numbers. Complex operators enable you to perform addition, subtraction, multiplication, division, and unary negation with complex numbers.

  • Methods to perform other numerical operations on complex numbers. In addition to the four basic arithmetic operations, you can raise a complex number to a specified power, find the square root of a complex number, and get the absolute value of a complex number.

  • Methods to perform trigonometric operations on complex numbers. For example, you can calculate the tangent of an angle represented by a complex number.

Note that, because the Real and Imaginary properties are read-only, you cannot modify the value of an existing Complex object. All methods that perform an operation on a Complex number, if their return value is of type Complex, return a new Complex number.

The real and imaginary parts of a complex number are represented by two double-precision floating-point values. This means that Complex values, like double-precision floating-point values, can lose precision as a result of numerical operations. This means that strict comparisons for equality of two Complex values may fail, even if the difference between the two values is due to a loss of precision. For more information, see Double.

For example, performing exponentiation on the logarithm of a number should return the original number. However, in some cases, the loss of precision of floating-point values can cause slight differences between the two values, as the following example illustrates.

Dim value As New Complex(Double.MinValue/2, Double.MinValue/2)
Dim value2 As Complex = Complex.Exp(Complex.Log(value))
Console.WriteLine("{0} {3}{1} {3}Equal: {2}", value, value2, 
                                              value = value2,
                                              vbCrLf)
' The example displays the following output:
'    (-8.98846567431158E+307, -8.98846567431158E+307)
'    (-8.98846567431161E+307, -8.98846567431161E+307)
'    Equal: False

Similarly, the following example, which calculates the square root of a Complex number, produces slightly different results on the 32-bit and IA64 versions of the .NET Framework.

Dim minusOne As New Complex(-1, 0)
Console.WriteLine(Complex.Sqrt(minusOne))
' The example displays the following output:
'    (6.12303176911189E-17, 1) on 32-bit systems.
'    (6.12323399573677E-17,1) on IA64 systems.

The real and imaginary parts of a complex number are represented by Double values. In addition to ranging from Double.MinValue to Double.MaxValue, the real or imaginary part of a complex number can have a value of Double.PositiveInfinity, Double.NegativeInfinity, or Double.NaN. Double.PositiveInfinity, Double.NegativeInfinity, and Double.NaN all propagate in any arithmetic or trigonometric operation.

In the following example, division by Zero produces a complex number whose real and imaginary parts are both Double.NaN. As a result, performing multiplication with this value also produces a complex number whose real and imaginary parts are Double.NaN. Similarly, performing a multiplication that overflows the range of the Double type produces a complex number whose real part is Double.NaN and whose imaginary part is Double.PositiveInfinity. Subsequently performing division with this complex number returns a complex number whose real part is Double.NaN and whose imaginary part is Double.PositiveInfinity.

Imports System.Numerics

Module Example
   Public Sub Main()
      Dim c1 As Complex = New Complex(Double.MaxValue / 2, Double.MaxValue /2)

      Dim c2 As Complex = c1 / Complex.Zero
      Console.WriteLine(c2.ToString())
      c2 = c2 * New Complex(1.5, 1.5)
      Console.WriteLine(c2.ToString())
      Console.WriteLine()

      Dim c3 As Complex = c1 * New Complex(2.5, 3.5)
      Console.WriteLine(c3.ToString())
      c3 = c3 + New Complex(Double.MinValue / 2, Double.MaxValue / 2) 
      Console.WriteLine(c3)     
   End Sub
End Module
' The example displays the following output:
'       (NaN, NaN)
'       (NaN, NaN)
'
'       (NaN, Infinity)
'       (NaN, Infinity)

Mathematical operations with complex numbers that are invalid or that overflow the range of the Double data type do not throw an exception. Instead, they return a Double.PositiveInfinity, Double.NegativeInfinity, or Double.NaN under the following conditions:

Note that this applies to any intermediate calculations performed by a method. For example, the multiplication of new Complex(9e308, 9e308) and new Complex(2.5, 3.5) uses the formula (ac - bd) + (ad + bc)i. The calculation of the real component that results from the multiplication evaluates the expression 9e308 * 2.5 - 9e308 * 3.5. Each intermediate multiplication in this expression returns Double.PositiveInfinity, and the attempt to subtract Double.PositiveInfinity from Double.PositiveInfinity returns Double.NaN.

By default, the string representation of a complex number takes the form (real, imaginary), where real and imaginary are the string representations of the Double values that form the complex number's real and imaginary components. Some overloads of the ToString method allow customization of the string representations of these Double values to reflect the formatting conventions of a particular culture or to appear in a particular format defined by a standard or custom numeric format string. (For more information, see Standard Numeric Format Strings and Custom Numeric Format Strings.)

One of the more common ways of expressing the string representation of a complex number takes the form a + bi, where a is the complex number's real component, and b is the complex number's imaginary component. In electrical engineering, a complex number is most commonly expressed as a + bj. You can return the string representation of a complex number in either of these two forms. To do this, define a custom format provider by implementing the ICustomFormatter and IFormatProvider interfaces, and then call the String.Format(IFormatProvider, String, Object()) method.

The following example defines a ComplexFormatter class that represents a complex number as a string in the form of either a + bi or a + bj.

Imports System.Numerics

Public Class ComplexFormatter 
             Implements IFormatProvider, ICustomFormatter

   Public Function GetFormat(formatType As Type) As Object _
                   Implements IFormatProvider.GetFormat
      If formatType Is GetType(ICustomFormatter) Then
         Return Me
      Else
         Return Nothing
      End If
   End Function

   Public Function Format(fmt As String, arg As Object, 
                          provider As IFormatProvider) As String _
                   Implements ICustomFormatter.Format
      If TypeOf arg Is Complex Then
         Dim c1 As Complex = DirectCast(arg, Complex)
         ' Check if the format string has a precision specifier.
         Dim precision As Integer
         Dim fmtString As String = String.Empty
         If fmt.Length > 1 Then
            Try
               precision = Int32.Parse(fmt.Substring(1))
            Catch e As FormatException
               precision = 0
            End Try
            fmtString = "N" + precision.ToString()
         End If
         If fmt.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase) Then
            Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i"
         ElseIf fmt.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase) Then
            Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j"
         Else
            Return c1.ToString(fmt, provider)
         End If
      Else
         If Typeof arg Is IFormattable Then
            Return DirectCast(arg, IFormattable).ToString(fmt, provider)
         ElseIf arg IsNot Nothing Then
            Return arg.ToString()
         Else
            Return String.Empty
         End If   
      End If                        
   End Function
End Class

The following example then uses this custom formatter to display the string representation of a complex number.

Module Example
   Public Sub Main()
      Dim c1 As Complex = New Complex(12.1, 15.4)
      Console.WriteLine("Formatting with ToString():       " + 
                        c1.ToString())
      Console.WriteLine("Formatting with ToString(format): " + 
                        c1.ToString("N2"))
      Console.WriteLine("Custom formatting with I0:        " + 
                        String.Format(New ComplexFormatter(), "{0:I0}", c1))
      Console.WriteLine("Custom formatting with J3:        " + 
                        String.Format(New ComplexFormatter(), "{0:J3}", c1))
   End Sub
End Module
' The example displays the following output:
'    Formatting with ToString():       (12.1, 15.4)
'    Formatting with ToString(format): (12.10, 15.40)
'    Custom formatting with I0:        12 + 15i
'    Custom formatting with J3:        12.100 + 15.400j

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: