Complex 结构

定义

public value class Complex : IEquatable<System::Numerics::Complex>, IFormattable
[System.Serializable]
public struct Complex : IEquatable<System.Numerics.Complex>, IFormattable
type Complex = struct
interface IFormattable
Public Structure Complex
Implements IEquatable(Of Complex), IFormattable

Complex

注解

Complex实例化和操作复数时, 该类型使用笛卡尔坐标系统 (real, 虚部)。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. 复数的实部定位在 x 轴 (水平轴) 上, 而虚部位于 y 轴 (垂直轴) 的位置。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).

• 其量值, 它是原点 (即 0, 0, 或 x 轴和 y 轴相交处的点) 之间的距离。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.

实例化复数Instantiating a Complex Number

• 通过向其Double构造函数传递两个值。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.

• 通过调用静态 (Shared Visual Basic) Complex.FromPolarCoordinates方法从其极坐标创建复数。By calling the static (Shared in Visual Basic) Complex.FromPolarCoordinates method to create a complex number from its polar coordinates.

• 通过Byte将、 、、、UInt32Int32 、、Double 、或值分配Complex给对象。 Int64 UInt64 Int16 SByte UInt16 SingleBy assigning a Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, or Double value to a Complex object. 值变成复数的实部, 其虚部等于0。The value becomes the real part of the complex number, and its imaginary part equals 0.

• 通过强制转换 ( C#在中) 或将Decimal ComplexBigInteger值转换为对象 (在 Visual Basic 中)。By casting (in C#) or converting (in Visual Basic) a Decimal or BigInteger value to a Complex object. 值变成复数的实部, 其虚部等于0。The value becomes the real part of the complex number, and its imaginary part equals 0.

• 通过将方法或运算符返回的复数赋给Complex对象。By assigning the complex number that is returned by a method or operator to a Complex object. 例如, Complex.Add是一个静态方法, 它返回一个复数, 该复数是两个复数的和, Complex.Addition运算符添加两个复数并返回结果。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.

using System;
using System.Numerics;

public class Example
{
public static void Main()
{
// Create a complex number by calling its class constructor.
Complex c1 = new Complex(12, 6);
Console.WriteLine(c1);

// Assign a Double to a complex number.
Complex c2 = 3.14;
Console.WriteLine(c2);

// Cast a Decimal to a complex number.
Complex c3 = (Complex) 12.3m;
Console.WriteLine(c3);

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

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

// Instantiate a complex number from its polar coordinates.
Complex c6 = Complex.FromPolarCoordinates(10, .524);
Console.WriteLine(c6);
}
}
// The example displays the following output:
//       (12, 6)
//       (3.14, 0)
//       (12.3, 0)
//       (1, 0)
//       (2, 0)
//       (8.65824721882145, 5.00347430269914)
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)

带有复数的操作Operations with Complex Numbers

.NET Framework Complex中的结构包括提供以下功能的成员: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使用运算符可以对复数执行加法、减法、乘法、除法和一元求反运算。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.

精度和复数Precision and Complex Numbers

Complex value = new Complex(Double.MinValue/2, Double.MinValue/2);
Complex value2 = Complex.Exp(Complex.Log(value));
Console.WriteLine("{0} \n{1} \nEqual: {2}", value, value2,
value == value2);
// The example displays the following output:
//    (-8.98846567431158E+307, -8.98846567431158E+307)
//    (-8.98846567431161E+307, -8.98846567431161E+307)
//    Equal: False
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

Complex minusOne = 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.
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.

复数、无穷和 NaNComplex Numbers, Infinity, and NaN

using System;
using System.Numerics;

public class Example
{
public static void Main()
{
Complex c1 = new Complex(Double.MaxValue / 2, Double.MaxValue /2);

Complex c2 = c1 / Complex.Zero;
Console.WriteLine(c2.ToString());
c2 = c2 * new Complex(1.5, 1.5);
Console.WriteLine(c2.ToString());
Console.WriteLine();

Complex c3 = c1 * new Complex(2.5, 3.5);
Console.WriteLine(c3.ToString());
c3 = c3 + new Complex(Double.MinValue / 2, Double.MaxValue / 2);
Console.WriteLine(c3);
}
}
// The example displays the following output:
//       (NaN, NaN)
//       (NaN, NaN)
//       (NaN, Infinity)
//       (NaN, Infinity)
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)

设置复数格式Formatting a Complex Number

using System;
using System.Numerics;

public class ComplexFormatter :IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}

public string Format(string format, object arg,
IFormatProvider provider)
{
if (arg is Complex)
{
Complex c1 = (Complex) arg;
// Check if the format string has a precision specifier.
int precision;
string fmtString = String.Empty;
if (format.Length > 1) {
try {
precision = Int32.Parse(format.Substring(1));
}
catch (FormatException) {
precision = 0;
}
fmtString = "N" + precision.ToString();
}
if (format.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase))
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i";
else if (format.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase))
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j";
else
return c1.ToString(format, provider);
}
else
{
if (arg is IFormattable)
return ((IFormattable) arg).ToString(format, provider);
else if (arg != null)
return arg.ToString();
else
return String.Empty;
}
}
}
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

public class Example
{
public static void Main()
{
Complex c1 = 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));
}
}
// 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
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

构造函数

 使用指定的实数值和虚数值初始化 Complex 结构的新实例。Initializes a new instance of the Complex structure using the specified real and imaginary values.

字段

 返回新的 Complex 实例，其实数等于零，虚数等于一。Returns a new Complex instance with a real number equal to zero and an imaginary number equal to one. 用复数表示无穷大。Represents infinity as a complex number. 表示一个不为数值 (NaN) 的复数实例。Represents a complex instance that is not a number (NaN). 返回新的 Complex 实例，其实数等于一，虚数等于零。Returns a new Complex instance with a real number equal to one and an imaginary number equal to zero. 返回新的 Complex 实例，其实数和虚数都等于零。Returns a new Complex instance with a real number equal to zero and an imaginary number equal to zero.

属性

 获取当前 Complex 对象的虚部。Gets the imaginary component of the current Complex object. 获取复数的量值（或绝对值）。Gets the magnitude (or absolute value) of a complex number. 获取复数的相位。Gets the phase of a complex number. 获取当前 Complex 对象的实部。Gets the real component of the current Complex object.

方法

 获取复数的绝对值（或量值）。Gets the absolute value (or magnitude) of a complex number. 返回表示指定复数的反余弦值的角度。Returns the angle that is the arc cosine of the specified complex number. 将两个复数相加，并返回结果。Adds two complex numbers and returns the result. 将一个复数与一个双精度实数相加，并返回结果。Adds a complex number to a double-precision real number and returns the result. 将一个双精度实数与一个复数相加，并返回结果。Adds a double-precision real number to a complex number and returns the result. 返回表示指定复数的反正弦值的角度。Returns the angle that is the arc sine of the specified complex number. 返回表示指定复数的反正切的角度。Returns the angle that is the arc tangent of the specified complex number. 计算复数的共轭，并返回结果。Computes the conjugate of a complex number and returns the result. 返回指定复数的余弦值。Returns the cosine of the specified complex number. 返回指定复数的双曲余弦值。Returns the hyperbolic cosine of the specified complex number. 用一个复数除另一个复数并返回结果。Divides one complex number by another and returns the result. 将一个复数除以一个双精度实数，并返回结果。Divides one complex number by a double-precision real number and returns the result. 将一个双精度实数除以一个复数，并返回结果。Divides one double-precision real number by a complex number and returns the result. 返回一个值，该值指示当前实例与指定的复数是否具有相同的值。Returns a value that indicates whether the current instance and a specified complex number have the same value. 返回一个值，该值指示当前实例与指定的对象是否具有相同的值。Returns a value that indicates whether the current instance and a specified object have the same value. 返回 e 的由一个复数指定的次幂。Returns e raised to the power specified by a complex number. 从点的极坐标创建复数。Creates a complex number from a point's polar coordinates. 返回当前 Complex 对象的哈希代码。Returns the hash code for the current Complex object. 确定指定的复数是否是有限的。Determines whether the specified complex number is finite. 返回一个值，该值指示指定复数的计算结果是否为无穷大。Returns a value indicating whether the specified complex number evaluates to infinity. 返回一个值，该值指示指定的复数实例是否不为数值 (NaN)。Returns a value that indicates whether the specified complex instance is not a number (NaN). 返回指定复数的自然对数（底为 e）。Returns the natural (base e) logarithm of a specified complex number. 返回指定复数在使用指定底时的对数。Returns the logarithm of a specified complex number in a specified base. 返回指定复数以 10 为底的对数。Returns the base-10 logarithm of a specified complex number. 返回两个复数的乘积。Returns the product of two complex numbers. 返回一个复数与一个双精度实数的乘积。Returns the product of a complex number and a double-precision real number. 返回一个双精度实数与一个复数的乘积。Returns the product of a double-precision real number and a complex number. 返回指定复数的加法逆元。Returns the additive inverse of a specified complex number. 返回指定复数的由复数指定的次幂。Returns a specified complex number raised to a power specified by a complex number. 返回指定复数的由双精度浮点数指定的次幂。Returns a specified complex number raised to a power specified by a double-precision floating-point number. 返回复数的乘法倒数。Returns the multiplicative inverse of a complex number. 返回指定复数的正弦值。Returns the sine of the specified complex number. 返回指定复数的双曲正弦值。Returns the hyperbolic sine of the specified complex number. 返回指定复数的平方根。Returns the square root of a specified complex number. 从一个复数中减去另一个复数并返回结果。Subtracts one complex number from another and returns the result. 一个复数减一个双精度实数，并返回结果。Subtracts one double-precision real number from a complex number and returns the result. 一个双精度实数减一个复数，并返回结果。Subtracts one complex number from a double-precision real number and returns the result. 返回指定复数的正切值。Returns the tangent of the specified complex number. 返回指定复数的双曲正切值。Returns the hyperbolic tangent of the specified complex number. 将当前复数的值转换为其采用笛卡尔形式的等效字符串表示形式。Converts the value of the current complex number to its equivalent string representation in Cartesian form. 使用指定的区域性特定格式设置信息，将当前复数的值转换为其采用笛卡尔形式的等效字符串表示形式。Converts the value of the current complex number to its equivalent string representation in Cartesian form by using the specified culture-specific formatting information. 通过对当前复数的实部和虚部使用指定格式，将它的值转换为其采用笛卡尔形式的等效字符串表示形式。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. 通过对当前复数的实部和虚部使用指定格式和区域性特定格式信息，将它的值转换为其采用笛卡尔形式的等效字符串表示形式。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.

操作员

 将两个复数相加。Adds two complex numbers. 将一个复数与一个双精度实数相加。Adds a complex number to a double-precision real number. 将一个双精度实数与一个复数相加。Adds a double-precision real number to a complex number. 用一个指定复数除另一个指定复数。Divides a specified complex number by another specified complex number. 将一个指定复数除以一个指定双精度实数。Divides a specified complex number by a specified double-precision real number. 将一个指定双精度实数除以一个指定复数。Divides a specified double-precision real number by a specified complex number. 返回一个值，该值指示两个复数是否相等。Returns a value that indicates whether two complex numbers are equal. 定义从 BigInteger 值到复数的显式转换。Defines an explicit conversion of a BigInteger value to a complex number. 定义从 Decimal 值到复数的显式转换。Defines an explicit conversion of a Decimal value to a complex number. 定义从无符号字节到复数的隐式转换。Defines an implicit conversion of an unsigned byte to a complex number. 定义从双精度浮点数到复数的隐式转换。Defines an implicit conversion of a double-precision floating-point number to a complex number. 定义从 16 位带符号整数到复数的隐式转换。Defines an implicit conversion of a 16-bit signed integer to a complex number. 定义从 32 位带符号整数到复数的隐式转换。Defines an implicit conversion of a 32-bit signed integer to a complex number. 定义从 64 位带符号整数到复数的隐式转换。Defines an implicit conversion of a 64-bit signed integer to a complex number. 定义从带符号字节到复数的隐式转换。Defines an implicit conversion of a signed byte to a complex number. 此 API 不兼容 CLS。This API is not CLS-compliant. 定义从单精度浮点数到复数的隐式转换。Defines an implicit conversion of a single-precision floating-point number to a complex number. 定义从 16 位无符号整数到复数的隐式转换。Defines an implicit conversion of a 16-bit unsigned integer to a complex number. 此 API 不兼容 CLS。This API is not CLS-compliant. 定义从 32 位无符号整数到复数的隐式转换。Defines an implicit conversion of a 32-bit unsigned integer to a complex number. 此 API 不兼容 CLS。This API is not CLS-compliant. 定义从 64 位无符号整数到复数的隐式转换。Defines an implicit conversion of a 64-bit unsigned integer to a complex number. 此 API 不兼容 CLS。This API is not CLS-compliant. 返回一个值，该值指示两个复数是否不相等。Returns a value that indicates whether two complex numbers are not equal. 将两个指定复数相乘。Multiplies two specified complex numbers. 将一个指定复数乘以一个指定双精度实数。Multiplies the specified complex number by a specified double-precision real number. 将一个指定双精度实数乘以一个指定复数。Multiplies a specified double-precision real number by a specified complex number. 从一个复数中减去另一个复数。Subtracts a complex number from another complex number. 一个复数减一个双精度实数。Subtracts a double-precision real number from a complex number. 一个双精度实数减一个复数。Subtracts a complex number from a double-precision real number. 返回指定复数的加法逆元。Returns the additive inverse of a specified complex number.