Struct Complex
- Namespace
- VirtualLabAPI.Core.Numerics
- Assembly
- VirtualLabAPI.dll
This struct holds a complex number.
Operators (\(a+b\), \(a-b\), \(a*b\), \(a/b\) and \(a^b\)) can be used.
Passing \(a\) or \(b\) as double is supported.
[Serializable]
public struct Complex
- Inherited Members
Constructors
Complex(double)
Constructs a complex number with given real part. The imaginary part is zero.
public Complex(double Re)
Parameters
RedoubleReal part.
Complex(double, double)
Constructs a complex number with given real and imaginary parts.
public Complex(double Re, double Im)
Parameters
Complex(Complex)
Copy constructor.
public Complex(Complex c)
Parameters
cComplexComplex number to copy.
Fields
Im
Imaginary part of stored complex number.
public double Im
Field Value
Re
Real part of stored complex number.
public double Re
Field Value
Undefined
A constant representing an undefined number.
public static readonly Complex Undefined
Field Value
Zero
A constant containing the neutral element for addition (= (0, 0))
public static readonly Complex Zero
Field Value
i
The imaginary unit.
public static readonly Complex i
Field Value
Methods
Abs()
Returns absolute value of current Complex.
public double Abs()
Returns
- double
The absolute value, or in physical terms the amplitude
Arg()
Returns the argument (phase) of this complex number. The resulting angle is in the range of [-π, +π).
public double Arg()
Returns
- double
The argument (phase) of this complex number.
ArgDifference(Complex, Complex)
Determine the reduced phase difference of two complex numbers
public static double ArgDifference(Complex c1, Complex c2)
Parameters
Returns
- double
Reduced phase difference.
Conjugate(Complex)
Returns conjugate complex number of current Complex.
public static Complex Conjugate(Complex z)
Parameters
zComplexComplex number to conjugate.
Returns
- Complex
Result of operation.
Cos(Complex)
public static Complex Cos(Complex z)
Parameters
zComplex
Returns
Equals(object)
Compares two complex numbers.
public override bool Equals(object obj)
Parameters
objobjectComplex number to be compared with this complex number.
Returns
EvaluateFielQuantity(FieldQuantity)
Method to evaluate a specific field quantity of the complex number.
public double EvaluateFielQuantity(FieldQuantity quantityToEvaluate)
Parameters
quantityToEvaluateFieldQuantityThe field quantity which should be evaluated.
Returns
- double
The evaluated field quantity.
Exp(Complex)
Returns exponential function of complex number.
public static Complex Exp(Complex c)
Parameters
cComplexComplex number \(a\).
Returns
- Complex
\(e^a\).
FilteredPhase(double, double)
Gets the phase of the complex value. In contrast to the Arg()-method, this method corrects numerical artifacts in the phase.
- The phase is zero if the amplitude of the complex number is less than threshold times maximum amplitude.
- The phase value is -π if its imaginary part value {Im} satisfies t⋅Re < -Abs(Im) < 0.
public double FilteredPhase(double maximumAmplitude = 0, double artifactsThreshold = 0.001)
Parameters
maximumAmplitudedoubleThe maximum amplitude. If this optional parameter is not specified, the filtering step (1) is not done.
artifactsThresholddoubleThe artifacts threshold. 0.1 % is taken as default value.
Returns
- double
The phase of the complex value.
FromFieldQuantity(double, FieldQuantity)
Creates a complex number from the given double value which is written into the given field quantity.
public static Complex FromFieldQuantity(double value, FieldQuantity fieldQuantity)
Parameters
valuedoubleThe double value.
fieldQuantityFieldQuantityThe field quantity into which the value is written.
Returns
- Complex
A new complex number.
GetComplexFormat(string)
Determines the way how a complex number is represented by a string (for example "a + ib" is the RealImaginary format, "a exp ib" is the AmplitudePhase format).
public static ComplexToStringConversionFormat? GetComplexFormat(string numberString)
Parameters
numberStringstringThe number string.
Returns
- ComplexToStringConversionFormat?
The complex number format.
nullif the format cannot be determined, e.g. for a real number.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
InputIsNumericalLarger(Complex)
Method to perform numerical comparison of two complex number. First the amplitudes are compared. If they are equal the phase values are compared.
public bool InputIsNumericalLarger(Complex cfForComparison)
Parameters
cfForComparisonComplexThe complex number which shall be used for comparison.
Returns
- bool
true of input is numerically larger than current instance of the complex number
IsNaN(Complex)
Checks whether either the real or the imaginary is NaN.
public static bool IsNaN(Complex number)
Parameters
numberComplexThe complex number to check.
Returns
Log(Complex)
Returns natural logarithm of complex number.
public static Complex Log(Complex z)
Parameters
zComplexComplex number \(a\).
Returns
- Complex
\(\ln(a)\).
ModifyComplexPart(ComplexPart)
Returns passed ComplexPart.
public Complex ModifyComplexPart(ComplexPart complexPart)
Parameters
complexPartComplexPartComplexPart to be applied on current Complex.
Returns
- Complex
Modified complex number.
Norm()
Returns the squared L2-norm of current Complex.
public double Norm()
Returns
- double
\(Re(this)^2 + Im(this)^2\).
Parse(string, ComplexToStringConversionFormat)
Parses an string which represents a complex number.
public static Complex Parse(string pureNumberStr, ComplexToStringConversionFormat complexFormat)
Parameters
pureNumberStrstringThe string to be converted.
complexFormatComplexToStringConversionFormatThe format of the complex number. Valid formats are RealImaginary and AmplitudePhase.
Returns
- Complex
Complex number.
Parse(string, ComplexToStringConversionFormat, NumberFormatInfo)
Parses an string which represents a complex number.
public static Complex Parse(string pureNumberStr, ComplexToStringConversionFormat complexFormat, NumberFormatInfo doubleFormat)
Parameters
pureNumberStrstringThe string to be converted.
complexFormatComplexToStringConversionFormatThe format of the complex number. The SinusCosinus format is not supported. RealImaginary, AmplitudePhase.
doubleFormatNumberFormatInfoThe format of the double-numbers which form the real and imaginary part.
Returns
- Complex
Complex number.
Exceptions
- ArgumentException
Current complex format is not supported!
- ComplexParseException
string is null-string or string is empty or Invalid complex number Format
Polar(double, double)
Returns complex number created by given absolute value and argument.
public static Complex Polar(double abs, double arg)
Parameters
Returns
- Complex
New complex object.
Pow(Complex, Complex)
Power function.
public static Complex Pow(Complex basis, Complex exponent)
Parameters
basisComplexThe basis for the power operation.
exponentComplexThe exponent for the power operation.
Returns
- Complex
The calculated power value.
Sin(Complex)
public static Complex Sin(Complex z)
Parameters
zComplex
Returns
Sqrt(Complex)
Returns the square root of a complex number.
public static Complex Sqrt(Complex z)
Parameters
zComplexComplex number \(a\).
Returns
- Complex
\(\sqrt{a}\).
ToString()
Returns a string that represents this instance.
public override string ToString()
Returns
- string
A string that represents this instance, formatted with the format of complex numbers as specified in the Global Options.
ToString(int, ComplexToStringConversionFormat?)
Returns formatted string, by default: {Real} + {Imaginary}·i
public string ToString(int numberOfDigits, ComplexToStringConversionFormat? format = null)
Parameters
numberOfDigitsintThe number of digits to use for the conversion of the double parts of the complex number.
formatComplexToStringConversionFormat?Optional enum parameter specifying which format to use. Format can be either
<ul> <li>{Real} + {Imaginary}·i</li> <li>{Amplitude}· exp(i·{Phase})</li> <li>({Real}, {Imaginary})</li></ul> If this parameter is not specified, the format of complex numbers as specified in the Global Options is used.
Returns
- string
Formatted string.
ToString(ComplexToStringConversionFormat)
Returns formatted string, by default: {Real} + {Imaginary}·i
public string ToString(ComplexToStringConversionFormat format)
Parameters
formatComplexToStringConversionFormatOptional enum parameter specifying which format to use. Format can be either
<ul> <li>{Real} + {Imaginary}·i</li> <li>{Amplitude}· exp(i·{Phase})</li> <li>({Real}, {Imaginary})</li> </ul> If this parameter is not specified, the format of complex numbers as specified in the Global Options is used.
Returns
- string
Formatted string.
ToString(PhysicalUnit, ComplexToStringConversionFormat, int)
Returns formatted string, either
- {Real} + {Imaginary}·i
- {Amplitude} · exp({Phase} · i)
- ({Real}, {Imaginary})
public string ToString(PhysicalUnit physicalUnit, ComplexToStringConversionFormat ComplexToStringFormat, int ComplexToStringConversionDigits)
Parameters
physicalUnitPhysicalUnitIf the complex number contains/represents a physical unit the corresponding real and imaginary numbers (or amplitude) are returned in measures of the given physical unit. But the corresponding unit-description-string is not appended to the complex number.
ComplexToStringFormatComplexToStringConversionFormatFormat of the complex number.
ComplexToStringConversionDigitsintDigits for the conversion of the double numbers of the real and imaginary part (amplitude or phase) of the complex number.
Returns
- string
Formatted string.
TryParse(string, out Complex, out ComplexToStringConversionFormat?, NumberFormatInfo)
Tries to parse a string which represents a complex number.
public static bool TryParse(string pureNumberStr, out Complex parsedValue, out ComplexToStringConversionFormat? usedConversionFormat, NumberFormatInfo doubleFormat = null)
Parameters
pureNumberStrstringThe string to be converted.
parsedValueComplexThe parsed value if successful.
usedConversionFormatComplexToStringConversionFormat?Null if parsing failed. Otherwise the used ComplexToStringConversionFormat which was successful.
doubleFormatNumberFormatInfoThe format of the double-numbers which form the real and imaginary part.
Returns
- bool
Trueif parsing was successful.
Exceptions
- ArgumentException
Current complex format is not supported!
- ComplexParseException
string is null-string or string is empty or Invalid complex number Format
Operators
operator +(double, Complex)
Adds a real number and a complex number.
public static Complex operator +(double d, Complex z1)
Parameters
Returns
- Complex
Sum of both numbers.
operator +(Complex, double)
Adds a complex number and a real number.
public static Complex operator +(Complex z1, double d)
Parameters
Returns
- Complex
Sum of both numbers.
operator +(Complex, Complex)
Adds two complex numbers.
public static Complex operator +(Complex z1, Complex z2)
Parameters
Returns
- Complex
Sum of both complex numbers.
operator /(double, Complex)
Divides a real number by complex number.
public static Complex operator /(double d, Complex z1)
Parameters
Returns
- Complex
Quotient of the two numbers.
operator /(Complex, double)
Divides complex number by real number.
public static Complex operator /(Complex z, double d)
Parameters
Returns
- Complex
Quotient of the two numbers
operator /(Complex, Complex)
Divides complex number by complex number.
public static Complex operator /(Complex z1, Complex z2)
Parameters
Returns
- Complex
Quotient of the two numbers.
operator ==(Complex, Complex)
Compares two Complex numbers for equality.
public static bool operator ==(Complex complex1, Complex complex2)
Parameters
Returns
operator !=(Complex, Complex)
Compares two Complex numbers for inequality
public static bool operator !=(Complex complex1, Complex complex2)
Parameters
Returns
operator *(double, Complex)
Multiplies real number with real number.
public static Complex operator *(double d, Complex z)
Parameters
Returns
- Complex
Product of the two numbers.
operator *(Complex, double)
Multiplies complex number with real number.
public static Complex operator *(Complex z, double d)
Parameters
Returns
- Complex
Product of the two numbers.
operator *(Complex, Complex)
Multiplies two complex numbers.
public static Complex operator *(Complex z1, Complex z2)
Parameters
Returns
- Complex
Product of the two numbers.
operator -(double, Complex)
Subtracts complex number from real number.
public static Complex operator -(double d, Complex z1)
Parameters
Returns
- Complex
Difference of the two numbers.
operator -(Complex, double)
Subtracts real number from complex number.
public static Complex operator -(Complex z1, double d)
Parameters
Returns
- Complex
Difference of the two numbers.
operator -(Complex, Complex)
Subtracts two complex numbers.
public static Complex operator -(Complex z1, Complex z2)
Parameters
Returns
- Complex
Difference of the two numbers.
operator -(Complex)
Multiplies a complex number with -1.
public static Complex operator -(Complex z)
Parameters
zComplexThe complex number to negate.
Returns
- Complex
The negated argument.