171 lines
4.5 KiB
C#
171 lines
4.5 KiB
C#
|
|
namespace BGC
|
|
{
|
|
public struct ComplexFP32
|
|
{
|
|
public float real = 0.0f;
|
|
public float imaginary = 0.0f;
|
|
|
|
public ComplexFP32(float real, float imaginary)
|
|
{
|
|
this.real = real;
|
|
this.imaginary = imaginary;
|
|
}
|
|
|
|
public ComplexFP32(in ComplexFP32 number)
|
|
{
|
|
this.real = number.real;
|
|
this.imaginary = number.imaginary;
|
|
}
|
|
|
|
public readonly float GetSquareModulus()
|
|
{
|
|
return this.real * this.real + this.imaginary * this.imaginary;
|
|
}
|
|
|
|
public readonly float GetModulus()
|
|
{
|
|
return MathF.Sqrt(GetSquareModulus());
|
|
}
|
|
|
|
public readonly bool IsZero()
|
|
{
|
|
return GetSquareModulus() <= UtilityFP32.SQUARE_EPSYLON;
|
|
}
|
|
|
|
public readonly bool IsUnit()
|
|
{
|
|
return UtilityFP32.IsSqareUnit(GetSquareModulus());
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
this.real = 0.0f;
|
|
this.imaginary = 0.0f;
|
|
}
|
|
|
|
public void MakeOpposite()
|
|
{
|
|
this.real = -this.real;
|
|
this.imaginary = -this.imaginary;
|
|
}
|
|
|
|
public bool Normalize()
|
|
{
|
|
float squareModulus = this.GetSquareModulus();
|
|
|
|
if (UtilityFP32.IsSqareUnit(squareModulus))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (squareModulus <= UtilityFP32.SQUARE_EPSYLON || float.IsNaN(squareModulus))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
float multiplier = MathF.Sqrt(1.0f / squareModulus);
|
|
|
|
this.real *= multiplier;
|
|
this.imaginary *= multiplier;
|
|
|
|
return true;
|
|
}
|
|
|
|
public void Conjugate()
|
|
{
|
|
this.imaginary = -this.imaginary;
|
|
}
|
|
|
|
public bool Invert()
|
|
{
|
|
float squareModulus = this.GetSquareModulus();
|
|
|
|
if (squareModulus <= UtilityFP32.SQUARE_EPSYLON || float.IsNaN(squareModulus))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
float multiplicand = 1.0f / squareModulus;
|
|
|
|
this.real *= multiplicand;
|
|
this.imaginary = -this.imaginary * multiplicand;
|
|
|
|
return true;
|
|
}
|
|
|
|
public void SetValues(float real, float imaginary)
|
|
{
|
|
this.real = real;
|
|
this.imaginary = imaginary;
|
|
}
|
|
|
|
public void SetValues(in ComplexFP32 number)
|
|
{
|
|
this.real = number.real;
|
|
this.imaginary = number.imaginary;
|
|
}
|
|
|
|
public static void Reset(out ComplexFP32 number)
|
|
{
|
|
number.real = 0.0f;
|
|
number.imaginary = 0.0f;
|
|
}
|
|
|
|
public static void GetOpposite(in ComplexFP32 number, out ComplexFP32 opposite)
|
|
{
|
|
opposite.real = -number.real;
|
|
opposite.imaginary = -number.imaginary;
|
|
}
|
|
|
|
public static bool GetNormalized(in ComplexFP32 number, out ComplexFP32 normalized)
|
|
{
|
|
float squareModulus = number.GetSquareModulus();
|
|
|
|
if (UtilityFP64.IsSqareUnit(squareModulus))
|
|
{
|
|
normalized.real = number.real;
|
|
normalized.imaginary = number.imaginary;
|
|
return true;
|
|
}
|
|
|
|
if (squareModulus <= UtilityFP64.SQUARE_EPSYLON || float.IsNaN(squareModulus))
|
|
{
|
|
normalized.real = 0.0f;
|
|
normalized.imaginary = 0.0f;
|
|
return false;
|
|
}
|
|
|
|
float multiplier = MathF.Sqrt(1.0f / squareModulus);
|
|
|
|
normalized.real = number.real * multiplier;
|
|
normalized.imaginary = number.imaginary * multiplier;
|
|
return true;
|
|
}
|
|
|
|
public static void GetConjugate(in ComplexFP32 number, out ComplexFP32 conjugate)
|
|
{
|
|
conjugate.real = number.real;
|
|
conjugate.imaginary = -number.imaginary;
|
|
}
|
|
|
|
public static bool GetInverse(in ComplexFP32 number, out ComplexFP32 inverse)
|
|
{
|
|
float squareModulus = number.GetSquareModulus();
|
|
|
|
if (squareModulus <= UtilityFP32.SQUARE_EPSYLON || float.IsNaN(squareModulus))
|
|
{
|
|
inverse.real = 0.0f;
|
|
inverse.imaginary = 0.0f;
|
|
return false;
|
|
}
|
|
|
|
float multiplicand = 1.0f / squareModulus;
|
|
|
|
inverse.real = number.real * multiplicand;
|
|
inverse.imaginary = -number.imaginary * multiplicand;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|