Struct Complex
Represents a complex number of the form a + bi, where a is the real part
and b is the imaginary part. Provides arithmetic operations, polar form
conversions, and common complex number properties.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Math
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct Complex : IEquatable<Complex>
Remarks
A complex number extends the real number line to a two-dimensional plane. The real part corresponds to the horizontal axis and the imaginary part corresponds to the vertical axis of the complex plane.
This struct is serializable and can be
exposed in the Unity Inspector. It is also implicitly convertible to and from
float (treating the value as a real number with zero imaginary part)
and to and from UnityEngine.Vector2 (treating x as real and y as
imaginary).
Equality comparisons use Approximately(float, float, float) to tolerate floating-point rounding errors rather than requiring exact bit-level equality.
Constructors
Complex(float, float)
Initializes a new Complex with the specified real and imaginary parts.
Declaration
public Complex(float real, float imaginary)
Parameters
| Type | Name | Description |
|---|---|---|
| float | real | The real part of the complex number. |
| float | imaginary | The imaginary part of the complex number. |
Fields
I
The imaginary unit i, defined by the property i^2 = -1. This is the
complex number at 90 degrees on the unit circle, i.e., (0, 1) in the complex
plane.
Declaration
public static readonly Complex I
Field Value
| Type | Description |
|---|---|
| Complex |
Imaginary
The imaginary part of this complex number. Corresponds to the vertical axis of the complex plane.
Declaration
[SerializeField]
public float Imaginary
Field Value
| Type | Description |
|---|---|
| float |
One
The complex number 1 + 0i (multiplicative identity). Multiplying any complex
number by One leaves it unchanged.
Declaration
public static readonly Complex One
Field Value
| Type | Description |
|---|---|
| Complex |
Real
The real part of this complex number. Corresponds to the horizontal axis of the complex plane.
Declaration
[SerializeField]
public float Real
Field Value
| Type | Description |
|---|---|
| float |
Zero
The complex number 0 + 0i (additive identity). Adding Zero to any
complex number leaves it unchanged.
Declaration
public static readonly Complex Zero
Field Value
| Type | Description |
|---|---|
| Complex |
Properties
Argument
Gets the argument (phase angle) of this complex number in radians. This is the
angle that the line from the origin to (Real, Imaginary) makes with the
positive real axis, computed via atan2(Imaginary, Real).
Declaration
public float Argument { get; }
Property Value
| Type | Description |
|---|---|
| float | A float in the range |
Conjugate
Gets the complex conjugate of this number. For a complex number a + bi,
the conjugate is a - bi. The conjugate reflects the number across the real
axis and is used to compute division and to extract real parts from products.
Declaration
public Complex Conjugate { get; }
Property Value
| Type | Description |
|---|---|
| Complex | A new Complex with the same real part and negated imaginary part. |
IsPurelyImaginary
Gets a value indicating whether this complex number is purely imaginary (i.e., its real part is negligibly close to zero). A purely imaginary number lies on the vertical axis of the complex plane.
Declaration
public bool IsPurelyImaginary { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsReal
Gets a value indicating whether this complex number is purely real (i.e., its imaginary part is negligibly close to zero). A real complex number lies on the horizontal axis of the complex plane.
Declaration
public bool IsReal { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
Modulus
Gets the modulus (magnitude) of this complex number. This is the Euclidean distance
from the origin to the point (Real, Imaginary) on the complex plane, computed
as sqrt(Real^2 + Imaginary^2).
Declaration
public float Modulus { get; }
Property Value
| Type | Description |
|---|---|
| float | A non-negative float representing the distance from the origin.
Returns |
See Also
SqrModulus
Gets the squared modulus (magnitude squared) of this complex number, computed as
Real^2 + Imaginary^2. Avoids the square root of Modulus and
is therefore more efficient when only comparing relative magnitudes.
Declaration
public float SqrModulus { get; }
Property Value
| Type | Description |
|---|---|
| float | A non-negative float. Equal to the product of this number and its Conjugate's real part. |
See Also
Methods
Abs(Complex)
Returns the absolute value (modulus) of a complex number. This is a static functional equivalent of the Modulus property.
Declaration
public static float Abs(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex number whose modulus is to be computed. |
Returns
| Type | Description |
|---|---|
| float | The Euclidean distance from the origin to |
See Also
Cbrt(Complex)
Returns the principal cube root of a complex number. The result w satisfies
w * w * w == z on the principal branch (argument in (-PI/3, PI/3]).
Declaration
public static Complex Cbrt(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex number whose cube root is to be computed. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex whose modulus is |
See Also
Cis(float)
Returns the complex number cis(theta), defined as
cos(theta) + isin(theta). This is equivalent to e^(itheta) by
Euler's formula and always lies on the unit circle.
Declaration
public static Complex Cis(float theta)
Parameters
| Type | Name | Description |
|---|---|---|
| float | theta | The angle in radians, measured from the positive real axis. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex on the unit circle at angle |
See Also
Equals(Complex)
Determines whether this Complex is equal to another Complex. Equality is checked using Approximately(float, float, float) to tolerate floating-point rounding errors.
Declaration
public bool Equals(Complex other)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | other | The Complex to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether this Complex is equal to another object.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
FromPolar(float, float)
Creates a complex number from polar coordinates using Euler's formula:
r * e^(itheta) = r * (cos(theta) + isin(theta)).
Declaration
public static Complex FromPolar(float r, float theta)
Parameters
| Type | Name | Description |
|---|---|---|
| float | r | The modulus (distance from the origin). A value of |
| float | theta | The argument (angle) in radians, measured from the positive real axis. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex whose Real part is |
See Also
GetHashCode()
Returns a hash code for this Complex. The hash is computed from both the Real and Imaginary parts using a combined hash strategy.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | An int hash code suitable for use in hash-based collections. |
Overrides
Im(Complex)
Returns the imaginary part of a complex number. This is a static functional equivalent of accessing the Imaginary field directly.
Declaration
public static float Im(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex number whose imaginary part is to be extracted. |
Returns
| Type | Description |
|---|---|
| float | The value of |
Pow(Complex, float)
Returns z raised to the real power p using
polar form: |z|^p * e^(i * arg(z) * p).
Declaration
public static Complex Pow(Complex z, float p)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex base. |
| float | p | The real exponent. Fractional and negative values are supported. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex with modulus |
Re(Complex)
Returns the real part of a complex number. This is a static functional equivalent of accessing the Real field directly.
Declaration
public static float Re(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex number whose real part is to be extracted. |
Returns
| Type | Description |
|---|---|
| float | The value of |
RootOfUnity(int, int)
Returns the k-th n-th root of unity, i.e.,
the k-th solution to the equation z^n = 1. All roots of
unity lie on the unit circle and are evenly spaced by an angle of 2*PI/n.
Declaration
public static Complex RootOfUnity(int n, int k = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | The degree of the root. Must be a positive integer greater than zero. |
| int | k | The index of the root to return, in the range |
Returns
| Type | Description |
|---|---|
| Complex | The complex number |
Remarks
This method is used internally by GetRoots() when computing the three real roots of a depressed cubic via Cardano's formula.
Sqrt(Complex)
Returns the principal square root of a complex number. The result is chosen such
that its argument lies in the range (-PI/2, PI/2] (i.e., the principal
branch). The result w satisfies w * w == z.
Declaration
public static Complex Sqrt(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex number whose square root is to be computed. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex whose modulus is |
See Also
Sqrt(float)
Returns the square root of a real number as a complex result, allowing negative
inputs to produce a purely imaginary result rather than NaN.
Declaration
public static Complex Sqrt(float f)
Parameters
| Type | Name | Description |
|---|---|---|
| float | f | The real number to take the square root of. May be negative. |
Returns
| Type | Description |
|---|---|
| Complex | If |
See Also
ToString()
Returns a string representation of this complex number in standard mathematical notation. Special cases are handled for purely real numbers, purely imaginary numbers, and unit imaginary parts.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string |
|
Overrides
Operators
operator +(Complex, Complex)
Adds two complex numbers by summing their real and imaginary parts independently.
Declaration
public static Complex operator +(Complex z, Complex w)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The left-hand addend. |
| Complex | w | The right-hand addend. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex equal to |
operator /(Complex, Complex)
Divides one complex number by another using conjugate multiplication:
z / w = (z * conjugate(w)) / |w|^2. This avoids complex division
by converting the denominator to a real number.
Declaration
public static Complex operator /(Complex z, Complex w)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The dividend. |
| Complex | w | The divisor. Must not be the zero complex number. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex equal to |
Exceptions
| Type | Condition |
|---|---|
| DivideByZeroException | Thrown when |
operator ==(Complex, Complex)
Returns true if two Complex values are approximately equal,
comparing both their real and imaginary parts using Approximately(float, float, float).
Declaration
public static bool operator ==(Complex z, Complex w)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The left-hand operand. |
| Complex | w | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
implicit operator Vector2(Complex)
Implicitly converts a Complex to a UnityEngine.Vector2, treating
the real part as x and the imaginary part as y. This allows
complex numbers to be used directly as 2D vectors.
Declaration
public static implicit operator Vector2(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The complex number to convert. |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 with |
implicit operator Complex(float)
Implicitly converts a float real number to a Complex with a zero imaginary part, placing it on the real axis of the complex plane.
Declaration
public static implicit operator Complex(float real)
Parameters
| Type | Name | Description |
|---|---|---|
| float | real | The real value to convert. |
Returns
| Type | Description |
|---|---|
| Complex |
implicit operator Complex(Vector2)
Implicitly converts a UnityEngine.Vector2 to a Complex, treating
x as the real part and y as the imaginary part. This allows
2D vectors to be used directly in complex arithmetic.
Declaration
public static implicit operator Complex(Vector2 v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | v | The vector to convert. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex with Real equal to |
operator !=(Complex, Complex)
Returns true if two Complex values are not approximately equal.
Declaration
public static bool operator !=(Complex z, Complex w)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The left-hand operand. |
| Complex | w | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator *(Complex, Complex)
Multiplies two complex numbers using the standard algebraic formula:
(a + bi)(c + di) = (ac - bd) + (ad + bc)i.
Declaration
public static Complex operator *(Complex z, Complex w)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The left-hand factor. |
| Complex | w | The right-hand factor. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex equal to
|
operator -(Complex, Complex)
Subtracts one complex number from another by subtracting their real and imaginary parts independently.
Declaration
public static Complex operator -(Complex z, Complex w)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The minuend. |
| Complex | w | The subtrahend. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex equal to |
operator -(Complex)
Unary negation operator. Returns the additive inverse of the complex number, negating both the real and imaginary parts.
Declaration
public static Complex operator -(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The operand to negate. |
Returns
| Type | Description |
|---|---|
| Complex | A Complex equal to |
operator +(Complex)
Unary plus operator. Returns the complex number unchanged.
Declaration
public static Complex operator +(Complex z)
Parameters
| Type | Name | Description |
|---|---|---|
| Complex | z | The operand. |
Returns
| Type | Description |
|---|---|
| Complex | The same Complex value as |