init
This commit is contained in:
106
DataPRO/FftSharp/Complex.cs
Normal file
106
DataPRO/FftSharp/Complex.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FftSharp
|
||||
{
|
||||
public struct Complex
|
||||
{
|
||||
private double _real;
|
||||
public double Real { get => _real; set { _real = value; _magnitude = null; _magnitudeSquared = null; } }
|
||||
|
||||
private double _imaginary;
|
||||
public double Imaginary { get => _imaginary; set { _imaginary = value; _magnitude = null; _magnitudeSquared = null; } }
|
||||
|
||||
private double? _magnitudeSquared;
|
||||
public double MagnitudeSquared
|
||||
{
|
||||
get
|
||||
{
|
||||
if (null == _magnitudeSquared)
|
||||
_magnitudeSquared = CalculateMagnitudeSquared(Real, Imaginary);
|
||||
return (double)_magnitudeSquared;
|
||||
}
|
||||
}
|
||||
|
||||
private double? _magnitude;
|
||||
public double Magnitude
|
||||
{
|
||||
get
|
||||
{
|
||||
if (null == _magnitude)
|
||||
_magnitude = CalculateMagnitude(Real, Imaginary);
|
||||
return (double)_magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
public static Complex Conjugate(Complex a)
|
||||
{
|
||||
return new Complex(a.Real, -a.Imaginary);
|
||||
}
|
||||
|
||||
public Complex(double real, double imaginary)
|
||||
{
|
||||
_real = real;
|
||||
_imaginary = imaginary;
|
||||
_magnitude = CalculateMagnitude(real, imaginary);
|
||||
_magnitudeSquared = CalculateMagnitudeSquared(real, imaginary);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Imaginary < 0)
|
||||
return $"{Real}-{-Imaginary}j";
|
||||
else
|
||||
return $"{Real}+{Imaginary}j";
|
||||
}
|
||||
|
||||
public static Complex operator +(Complex a, Complex b)
|
||||
{
|
||||
return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
|
||||
}
|
||||
|
||||
public static Complex operator -(Complex a, Complex b)
|
||||
{
|
||||
return new Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
|
||||
}
|
||||
|
||||
public static Complex operator *(Complex a, Complex b)
|
||||
{
|
||||
return new Complex(
|
||||
real: (a.Real * b.Real) - (a.Imaginary * b.Imaginary),
|
||||
imaginary: (a.Real * b.Imaginary) + (a.Imaginary * b.Real));
|
||||
}
|
||||
|
||||
public static Complex operator *(Complex a, double b)
|
||||
{
|
||||
return new Complex(a.Real * b, a.Imaginary * b);
|
||||
}
|
||||
|
||||
public static Complex[] FromReal(double[] real)
|
||||
{
|
||||
Complex[] complex = new Complex[real.Length];
|
||||
for (int i = 0; i < real.Length; i++)
|
||||
complex[i].Real = real[i];
|
||||
return complex;
|
||||
}
|
||||
|
||||
private static double CalculateMagnitudeSquared(double real, double imaginary)
|
||||
{
|
||||
return real * real + imaginary * imaginary;
|
||||
}
|
||||
|
||||
private static double CalculateMagnitude(double real, double imaginary)
|
||||
{
|
||||
return Math.Sqrt(CalculateMagnitudeSquared(real, imaginary));
|
||||
}
|
||||
|
||||
public static double[] GetMagnitudes(Complex[] input)
|
||||
{
|
||||
double[] output = new double[input.Length];
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
output[i] = input[i].Magnitude;
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user