本文整理汇总了C#中System.Complex类的典型用法代码示例。如果您正苦于以下问题:C# Complex类的具体用法?C# Complex怎么用?C# Complex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Complex类属于System命名空间,在下文中一共展示了Complex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Complex
// оператор + ____________________________________________________________________________________________
// оператор - ____________________________________________________________________________________________
public static Complex operator -(Complex FirstComplex, Complex SecondComplex)
{
if (FirstComplex.DNum != SecondComplex.DNum)
{
int Temp;
Temp = FirstComplex.DNum;
FirstComplex.UNum *= SecondComplex.DNum;
FirstComplex.DNum *= SecondComplex.DNum;
SecondComplex.DNum *= Temp;
SecondComplex.UNum *= Temp;
Complex TEMP = new Complex(FirstComplex.UNum - SecondComplex.UNum, FirstComplex.DNum);
Console.WriteLine(FirstComplex.UNum);
Console.WriteLine(SecondComplex.UNum);
while (TEMP.DNum != 0 && TEMP.UNum != 0)
{
if (TEMP.DNum > TEMP.UNum)
TEMP.DNum %= TEMP.UNum;
else
TEMP.UNum %= TEMP.DNum;
}
Complex Result = new Complex((FirstComplex.UNum - SecondComplex.UNum) / Math.Abs(TEMP.DNum + TEMP.UNum), FirstComplex.DNum / Math.Abs(TEMP.DNum + TEMP.UNum));
return Result;
}
Complex ResultElse = new Complex(FirstComplex.UNum - SecondComplex.UNum, FirstComplex.DNum);
return ResultElse;
}
示例2: SubtrachtTest
public void SubtrachtTest()
{
calculator = new ComplexNumbers.Calculator();
Complex<double> actual = calculator.Subtracht(new Complex<double>(4, 5), new Complex<double>(6, 7));
Complex<double> expected = new Complex<double>(4 - 6, 5 - 7);
Assert.True(actual.Equals(expected));
}
示例3: ComplexRational
/// <summary>
/// Initializes a new instance of the ComplexRational class,
/// by directly referencing the two provided polynomial coefficients.
/// </summary>
public ComplexRational(
Complex[] numeratorCoefficients,
Complex[] denominatorCoefficients)
{
_numerator = new ComplexPolynomial(numeratorCoefficients);
_denominator = new ComplexPolynomial(denominatorCoefficients);
}
示例4: TestDivision
public void TestDivision()
{
// 0/1
Complex zeroDivOne = Complex.Zero / Complex.One;
Assert.That(zeroDivOne.Real, Is.EqualTo(0), "Re{0/1} = 0");
Assert.That(zeroDivOne.Imag, Is.EqualTo(0), "Im{0/1} = 0");
// 1/0
// TODO: verify this is really what should happen
Complex oneDivZero = Complex.One / Complex.Zero;
Assert.That(oneDivZero.Real, Is.EqualTo(double.PositiveInfinity), "Re{1/0} = infty");
Assert.That(oneDivZero.Imag, Is.EqualTo(double.PositiveInfinity), "Im{1/0} = infty");
// (1+2I)/(3+4I)
Complex onePlus2I = new Complex(1, 2);
Complex threePlus4I = new Complex(3, 4);
Complex onPlus2IDivthreePlus4I = onePlus2I / threePlus4I;
Assert.That(onPlus2IDivthreePlus4I.Real, Is.EqualTo(11d / 25d), "Re{(1+2I)/(3+4I)} = 11/25");
Assert.That(onPlus2IDivthreePlus4I.Imag, Is.EqualTo(2d / 25d), "Im{(1+2I)/(3+4I)} = 2/25");
// (big+big*I)/(2*big+2*big*I)
const double big1 = double.MaxValue / 4;
const double big2 = double.MaxValue / 2;
Complex big1PlusBig1I = new Complex(big1, big1);
Complex big2PlusBig2I = new Complex(big2, big2);
Complex big1PlusBig1IDivBig2PlusBig2I = big1PlusBig1I / big2PlusBig2I;
Assert.That(big1PlusBig1IDivBig2PlusBig2I.Real, Is.EqualTo(0.5), "Re{(big+big*I)/(2*big+2*big*I)} = 0.5");
Assert.That(big1PlusBig1IDivBig2PlusBig2I.Imag, Is.EqualTo(0), "Im{((big+big*I)/(2*big+2*big*I)} = 0");
}
示例5: ComplexThreshold
/// <summary>
/// Returns are much larger the real or imaginary part of a complex number.
/// If the ratio of real and imaginary parts of complex number are not so large
/// returns the initial value.
/// </summary>
/// <param name="value">A complex number.</param>
/// <param name="complexThreshold">An integer representing the complex threshold.</param>
/// <returns>
/// Are much larger the real or imaginary part of the value.
/// If the ratio of real and imaginary parts of the value are not so large
/// returns the value.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// complexThreshold must be between 0 and 307.
/// </exception>
public static Complex ComplexThreshold(Complex value, int complexThreshold)
{
if (complexThreshold < 0 || complexThreshold > MaxComplexZeroThreshold)
throw new ArgumentOutOfRangeException("complexThreshold", String.Format("Complex threshold must be between 0 and {0}.", MaxComplexZeroThreshold));
if (value.IsReal || value.IsImaginary)
{
return value;
}
double d = Math.Pow(10, complexThreshold);
double reAbs = Math.Abs(value.Re);
double imAbs = Math.Abs(value.Im);
if ((reAbs > imAbs) && (reAbs / imAbs > d))
{
return new Complex(value.Re, 0.0);
}
else if ((imAbs > reAbs) && (imAbs / reAbs > d))
{
return new Complex(0.0, value.Im);
}
return value;
}
示例6: Compute
public static double Compute(Complex a, Complex b)
{
Complex temp = a * ComplexMath.Conjugate(a);
temp += (b * ComplexMath.Conjugate(b));
double ret = ComplexMath.Absolute(temp);
return System.Math.Sqrt(ret);
}
示例7: MultiplyTest
public void MultiplyTest()
{
calculator = new ComplexNumbers.Calculator();
Complex<double> actual = calculator.Multiply(new Complex<double>(4, 5), new Complex<double>(6, 7));
Complex<double> expected = new Complex<double>(4 * 6 - 5 * 7, 5 * 6 + 4 * 7);
Assert.True(actual.Equals(expected));
}
示例8: IsConstant
private static bool IsConstant(string equation, int index, out string n, out Complex val)
{
var spi = Term.ExtractName(equation, index, false).ToLower();
if (cs.Contains(spi, StringComparer.OrdinalIgnoreCase))
{
n = spi;
var consts =
typeof (Constants).GetProperties(BindingFlags.Public |
BindingFlags.Static);
if (
consts.Any(
x =>
Attribute.IsDefined(x, typeof (MathConst)) &&
(Attribute.GetCustomAttribute(x, typeof (MathConst)) as MathConst).Name == spi.ToLower()))
{
var prop =
consts.First(
x =>
Attribute.IsDefined(x, typeof (MathConst)) &&
(Attribute.GetCustomAttribute(x, typeof (MathConst)) as MathConst).Name == spi.ToLower());
val = (Complex) (prop.GetValue(null, null));
return true;
}
else
{
val = double.NaN;
return false;
}
}
n = null;
val = double.NaN;
return false;
}
示例9: AddArrays
/// <summary>
/// Does a point wise add of two arrays <c>z = x + y</c>. This can be used
/// to add vectors or matrices.
/// </summary>
/// <param name="x">The array x.</param>
/// <param name="y">The array y.</param>
/// <param name="result">The result of the addition.</param>
/// <remarks>There is no equivalent BLAS routine, but many libraries
/// provide optimized (parallel and/or vectorized) versions of this
/// routine.</remarks>
public virtual void AddArrays(Complex[] x, Complex[] y, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException("y");
}
if (x == null)
{
throw new ArgumentNullException("x");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (y.Length != x.Length || y.Length != result.Length)
{
throw new ArgumentException(NumericsResources.ArgumentVectorsSameLength);
}
CommonParallel.For(0, y.Length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
result[i] = x[i] + y[i];
}
});
}
示例10: FftKernel
public override void FftKernel(Complex[] x, Complex[] y, int y0, int dy, int sign)
{
// all we have to do here is convolve (b x) with b-star
// to do this convolution, we need to multiply the DFT of (b x) with the DFT of b-star, the IDFT the result back
// we have already stored the DFT of b-star
// create c = b x and transform it into Fourier space
Complex[] c = new Complex[Nb];
if (sign > 0) {
for (int i = 0; i < R; i++) c[i] = b[i] * x[i];
} else {
for (int i = 0; i < R; i++) c[i] = b[i] * x[i].Conjugate;
}
Complex[] ct = ft.Transform(c);
// multiply b-star and c = b x in Fourier space, and inverse transform the product back into configuration space
for (int i = 0; i < Nb; i++) {
ct[i] = ct[i] * bt[i];
}
c = ft.InverseTransform(ct);
// read off the result
if (sign > 0) {
for (int i = 0; i < R; i++) y[y0 + i * dy] = b[i] * c[i];
} else {
for (int i = 0; i < R; i++) y[y0 + i * dy] = b[i].Conjugate * c[i].Conjugate;
}
// for the sign < 0 case, we have used the fact that the convolution of (b-star x) with b is
// just the convolution of (b x-star) with b-star, starred
}
示例11: HComplex
public HComplex()
{
Value = new Complex(0, 0);
Attributes.Add("real", new HassiumProperty("real", x => Real, (self, x) => Real = x[0].HDouble().Value));
Attributes.Add("imaginary", new HassiumProperty("imaginary", x => Imaginary, (self, x) => Imaginary = x[0].HDouble().Value));
Attributes.Add("imag", new HassiumProperty("imag", x => Imaginary, (self, x) => Imaginary = x[0].HDouble().Value));
Attributes.Add("module", new HassiumProperty("module", x => (double)Value.Module, null, true));
Attributes.Add("argument", new HassiumProperty("argument", x => (double)Value.Argument, null, true));
Attributes.Add("conjugate", new HassiumProperty("conjugate", x => (double)Value.Conjugate, null, true));
Attributes.Add("__add", new InternalFunction(Add, -1));
Attributes.Add("__substract", new InternalFunction(Substract, -1));
Attributes.Add("__multiply", new InternalFunction(Multiply, -1));
Attributes.Add("__divide", new InternalFunction(Divide, -1));
Attributes.Add("__compare", new InternalFunction(x =>
{
var v = x[0].HComplex().Value;
if (v > Value) return 1;
if (v < Value) return -1;
return 0;
}, 1));
Attributes.Add("__pow", new InternalFunction(x => new HComplex(Functions.Pow(Value, x[0].HComplex().Value)), 1));
Attributes.Add("__root", new InternalFunction(x => new HComplex(Functions.NthRoot(Value, x[0].HComplex().Value)), 1));
Attributes.Add("negate", new InternalFunction(Negate, 0));
Attributes.Add("square", new InternalFunction(x => new HComplex(Value.Square()), 0));
}
示例12: AddTest
public void AddTest()
{
calculator = new ComplexNumbers.Calculator();
Complex<double> actual = calculator.Add(new Complex<double>(4, 5), new Complex<double>(6, 7));
Complex<double> expected = new Complex<double>(4 + 6, 5 + 7);
Assert.True(actual.Equals(expected));
}
示例13: Main
static void Main()
{
//try
//{
// string input;
// while ((input = Console.ReadLine()) != "exit")
// new Calculator(input);
//}
//catch (IndexOutOfRangeException)
//{
// Console.WriteLine("Incorrect input");
//}
string str;
var matrix = new Complex[2][];
matrix[0] = new Complex[2];
matrix[1] = new Complex[2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
{
str = Console.ReadLine();
double a, b;
double.TryParse(str, out a);
str = Console.ReadLine();
double.TryParse(str, out b);
matrix[i][j] = new Complex(a, b);
}
var m = new Matrix<Complex>(matrix);
var result = m + m;
result.Print();
}
示例14: CalculateMandelbrotAsync
Task CalculateMandelbrotAsync(BmpMaker bmpMaker)
{
return Task.Run(() =>
{
for (int row = 0; row < pixelHeight; row++)
{
double y = center.Imaginary - size.Height / 2 + row * size.Height / pixelHeight;
for (int col = 0; col < pixelWidth; col++)
{
double x = center.Real - size.Width / 2 + col * size.Width / pixelWidth;
Complex c = new Complex(x, y);
Complex z = 0;
int iteration = 0;
do
{
z = z * z + c;
iteration++;
}
while (iteration < iterations && z.MagnitudeSquared < 4);
bool isMandelbrotSet = iteration == iterations;
bmpMaker.SetPixel(row, col, isMandelbrotSet ? Color.Black : Color.White);
}
}
});
}
示例15: padded_IFFT
public static double[] padded_IFFT(ref Complex[] @in, bool doProperScaling=false)
{
Debug.Assert(@in.Length > 1);
int originalLength = @in.Length;
int n = (@in.Length - 1) * 2;
int padded = n > 256 ? Util.NextLowPrimes(n) : n;
Array.Resize<Complex>(ref @in, padded / 2 + 1);
// prepare the input arrays
var complexDouble = FFTUtils.ComplexToComplexDouble(@in);
var fftwBackwardInput = new FFTW.ComplexArray(complexDouble);
var fftwBackwardOutput = new FFTW.DoubleArray(padded);
// this method needs that the backwards transform uses the output.length as it's N
// i.e. FFTW.dft_c2r_1d(output.Length, input.Handle, output.Handle, Flags.Estimate);
FFTW.BackwardTransform(fftwBackwardInput, fftwBackwardOutput);
double[] @out = null;
if (doProperScaling) {
@out = fftwBackwardOutput.ValuesDivedByN;
} else {
// in the original method it wasn't scaled correctly (meaning ValuesDivedByN)
@out = fftwBackwardOutput.Values;
}
Array.Resize<Complex>(ref @in, n / 2 + 1);
// free up memory
GC.Collect();
return @out;
}