122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
using System.Numerics;
|
|
|
|
namespace Skeleton.Algebra.ScalarFieldStructure;
|
|
|
|
/// <inheritdoc />
|
|
public class ComplexFieldStructure : FieldStructure<Complex>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static readonly ComplexFieldStructure Structure = new();
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Addition(Complex self, Complex other) => self + other;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex AdditionUnit => 0d;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Multiplication(Complex self, Complex other) => self * other;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex MultiplicationUnit => 1d;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex AdditionInverse(Complex self) => -self;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex MultiplicationInverse(Complex self) => 1d / self;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Subtraction(Complex self, Complex other) => self - other;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Division(Complex self, Complex other) => self / other;
|
|
|
|
/// <inheritdoc />
|
|
public override bool IsEqualApprox(Complex self, Complex other, double? absTol = null, double? relTol = null)
|
|
|
|
=> RealFieldStructure.Structure.IsEqualApprox(self.Real, other.Real, absTol, relTol) &&
|
|
RealFieldStructure.Structure.IsEqualApprox(self.Imaginary, other.Imaginary, absTol, relTol);
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Fix(Complex x)
|
|
=> base.Fix(new Complex(
|
|
RealFieldStructure.Structure.Fix(x.Real),
|
|
RealFieldStructure.Structure.Fix(x.Imaginary)
|
|
));
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Conj(Complex self) => Complex.Conjugate(self);
|
|
|
|
/// <inheritdoc />
|
|
public override double AsReal(Complex x) => x.Real;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex FromReal(double x) => x;
|
|
|
|
private const string PlusSign = "+";
|
|
private const string EmptyString = "";
|
|
|
|
/// <inheritdoc />
|
|
public override string RawRepresentation(Complex x)
|
|
=> RealFieldStructure.Structure.IsEqualApprox(x.Imaginary, 0d)
|
|
? RealFieldStructure.Structure.Representation(x.Real)
|
|
: $"{x.Real}{(x.Imaginary > 0 ? PlusSign : EmptyString)}{x.Imaginary}i";
|
|
|
|
/// <inheritdoc />
|
|
public override string RawPythonRepresentation(Complex x)
|
|
=> RealFieldStructure.Structure.IsEqualApprox(x.Imaginary, 0d)
|
|
? RealFieldStructure.Structure.Representation(x.Real)
|
|
: $"{x.Real}{(x.Imaginary > 0 ? PlusSign : EmptyString)}{x.Imaginary}j";
|
|
|
|
/// <inheritdoc />
|
|
public override string RawCSharpRepresentation(Complex x)
|
|
=> RealFieldStructure.Structure.IsEqualApprox(x.Imaginary, 0d)
|
|
? RealFieldStructure.Structure.Representation(x.Real)
|
|
: $"{x.Real}{(x.Imaginary >= 0 ? PlusSign : EmptyString)}{x.Imaginary}*AlgebraConstant.I";
|
|
|
|
/// <inheritdoc />
|
|
public override string DumpString(Complex x)
|
|
=> "<C>" +
|
|
RealFieldStructure.Structure.DumpString(x.Real) +
|
|
"<CSPLIT/>" +
|
|
RealFieldStructure.Structure.DumpString(x.Imaginary) +
|
|
"</C>";
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Resolve(string dumpString)
|
|
{
|
|
double[] restored = dumpString
|
|
.Replace("<C>", "")
|
|
.Replace("</C>", "")
|
|
.Split("<CSPLIT/>")
|
|
.Select(x => RealFieldStructure.Structure.Resolve(x))
|
|
.ToArray();
|
|
return new Complex(restored[0], restored[1]);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override double MaxError(Complex a)
|
|
=> Math.Max(Math.Abs(a.Real), Math.Abs(a.Imaginary));
|
|
|
|
/// <inheritdoc />
|
|
public override Complex ComplexCast(Complex a) => a;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex RealMul(Complex a, double b) => a * b;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex SquareRoot(Complex a) => Complex.Sqrt(a);
|
|
|
|
/// <inheritdoc />
|
|
public override Complex FromComplex(Complex a) => a;
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Log(Complex x) => Complex.Log(x);
|
|
|
|
/// <inheritdoc />
|
|
public override Complex Exp(Complex x) => Complex.Exp(x);
|
|
}
|