Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Last active May 2, 2019 20:34
Show Gist options
  • Save mjs3339/0c4b37e6640e61d6b2170de3db504359 to your computer and use it in GitHub Desktop.
Save mjs3339/0c4b37e6640e61d6b2170de3db504359 to your computer and use it in GitHub Desktop.
C# Big Complex Number (Binomial) Class
using System;
using System.Collections.Generic;
public class BigComplex
{
public BigComplex()
{
Real = 0;
Imaginary = 0;
}
public BigComplex(BigDecimal real_part)
{
Real = real_part;
Imaginary = 0;
}
public BigComplex(BigDecimal real_part, BigDecimal imaginary_part)
{
Real = real_part;
Imaginary = imaginary_part;
}
public BigDecimal Real { get; set; }
public BigDecimal Imaginary { get; set; }
public static BigComplex I => new BigComplex(0, 1);
public static BigComplex Zero => new BigComplex(0, 0);
public static BigComplex One => new BigComplex(1, 0);
public static BigComplex operator +(BigComplex a, BigComplex b)
{
return new BigComplex(a.Real + b.Real, a.Imaginary + b.Imaginary);
}
public static BigComplex operator +(BigComplex a, BigDecimal b)
{
return new BigComplex(a.Real + b, a.Imaginary);
}
public static BigComplex operator +(BigDecimal a, BigComplex b)
{
return new BigComplex(a + b.Real, b.Imaginary);
}
public static BigComplex operator -(BigComplex a, BigComplex b)
{
return new BigComplex(a.Real - b.Real, a.Imaginary - b.Imaginary);
}
public static BigComplex operator -(BigComplex a, BigDecimal b)
{
return new BigComplex(a.Real - b, a.Imaginary);
}
public static BigComplex operator -(BigDecimal a, BigComplex b)
{
return new BigComplex(a - b.Real, -b.Imaginary);
}
public static BigComplex operator -(BigComplex a)
{
return new BigComplex(-a.Real, -a.Imaginary);
}
public static BigComplex operator *(BigComplex a, BigComplex b)
{
return new BigComplex(a.Real * b.Real - a.Imaginary * b.Imaginary,
a.Imaginary * b.Real + a.Real * b.Imaginary);
}
public static BigComplex operator *(BigComplex a, BigDecimal d)
{
return new BigComplex(d * a.Real, d * a.Imaginary);
}
public static BigComplex operator *(BigDecimal d, BigComplex a)
{
return new BigComplex(d * a.Real, d * a.Imaginary);
}
public static BigComplex operator /(BigComplex a, BigComplex b)
{
return a * Conj(b) * (1 / (Abs(b, 100) * Abs(b, 100)));
}
public static BigComplex operator /(BigComplex a, BigDecimal b)
{
return a * (1 / b);
}
public static BigComplex operator /(BigDecimal a, BigComplex b)
{
return a * Conj(b) * (1 / (Abs(b, 100) * Abs(b, 100)));
}
public static bool operator <(BigComplex a, BigComplex b)
{
return a.Real < b.Real && a.Imaginary < b.Imaginary;
}
public static bool operator >(BigComplex a, BigComplex b)
{
return a.Real > b.Real && a.Imaginary > b.Imaginary;
}
public static bool operator ==(BigComplex a, BigComplex b)
{
return a.Real == b.Real && a.Imaginary == b.Imaginary;
}
public static bool operator ==(BigComplex a, BigDecimal b)
{
return a == new BigComplex(b);
}
public static bool operator ==(BigDecimal a, BigComplex b)
{
return new BigComplex(a) == b;
}
public static bool operator !=(BigComplex a, BigComplex b)
{
return !(a == b);
}
public static bool operator !=(BigComplex a, BigDecimal b)
{
return !(a == b);
}
public static bool operator !=(BigDecimal a, BigComplex b)
{
return !(a == b);
}
public static BigComplex Abs(BigComplex a, int n)
{
return new BigComplex(BigDecimal.Sqrt(a.Imaginary * a.Imaginary + a.Real * a.Real));
}
public static BigComplex Inv(BigComplex a)
{
return new BigComplex(a.Real / (a.Real * a.Real + a.Imaginary * a.Imaginary),
-a.Imaginary / (a.Real * a.Real + a.Imaginary * a.Imaginary));
}
public static BigComplex Tan(BigComplex a, int n)
{
return Sin(a, n) / Cos(a, n);
}
public static BigComplex Cosh(BigComplex a, int n)
{
return (Exp(a, n) + Exp(-a, n)) / 2;
}
public static BigComplex Sinh(BigComplex a, int n)
{
return (Exp(a, n) - Exp(-a, n)) / 2;
}
public static BigComplex Tanh(BigComplex a, int n)
{
return (Exp(2 * a, n) - 1) / (Exp(2 * a, n) + 1);
}
public static BigComplex Coth(BigComplex a, int n)
{
return (Exp(2 * a, n) + 1) / (Exp(2 * a, n) - 1);
}
public static BigComplex Sech(BigComplex a, int n)
{
return Inv(Cosh(a, n));
}
public static BigComplex Csch(BigComplex a, int n)
{
return Inv(Sinh(a, n));
}
public static BigComplex Cot(BigComplex a, int n)
{
return Cos(a, n) / Sin(a, n);
}
public static BigComplex Conj(BigComplex a)
{
return new BigComplex(a.Real, -a.Imaginary);
}
public static BigComplex Sqrt(BigDecimal d)
{
if (d >= 0)
return new BigComplex(BigDecimal.Sqrt(d));
return new BigComplex(0, BigDecimal.Sqrt(-d));
}
public static BigComplex Sqrt(BigComplex a, int n)
{
return Pow(a, .5, n);
}
public static BigComplex Exp(BigComplex a, int n)
{
return new BigComplex(BigDecimal.Exp(a.Real) * BigDecimal.Cosine(a.Imaginary, n),
BigDecimal.Exp(a.Real) * BigDecimal.Sine(a.Imaginary, n));
}
public List<BigComplex> GetFactors(BigComplex n, int n1)
{
var Factors = new List<BigComplex>();
var s = Sqrt(n, n1);
var a = new BigComplex(3);
while (a < s)
{
if (n.Real % a.Real == 0)
{
Factors.Add(a);
if (a * a != n)
Factors.Add(n / a);
}
a += 2;
}
return Factors;
}
public static BigComplex Log(BigComplex a, int n)
{
return new BigComplex(BigDecimal.LogN(BigDecimal.Abs(a.Real)), Arg(a, n));
}
public static BigDecimal Arg(BigComplex a, int n)
{
if (!(a.Real < 0))
return BigDecimal.Atan(a.Imaginary / a.Real, n);
if (a.Imaginary < 0)
return BigDecimal.Atan(a.Imaginary / a.Real, n) - BigDecimal.Pi;
return Math.PI - BigDecimal.Atan(-a.Imaginary / a.Real, n);
}
public static BigComplex Cos(BigComplex a, int n)
{
return .5 * (Exp(I * a, n) + Exp(-I * a, n));
}
public static BigComplex Sin(BigComplex a, int n)
{
return (Exp(I * a, n) - Exp(-I * a, n)) / (2 * I);
}
public static BigComplex Pow(BigComplex a, BigComplex b, int n)
{
return Exp(b * Log(a, n), n);
}
public static BigComplex Pow(BigDecimal a, BigComplex b, int n)
{
return Exp(b * Log(new BigComplex(a), n), n);
}
public static BigComplex Pow(BigComplex a, BigDecimal b, int n)
{
return Exp(b * Log(a, n), n);
}
public override string ToString()
{
if (this == Zero)
return "0";
string re, im, sign;
if (Imaginary < 0)
{
if (Real == 0)
sign = " - ";
else
sign = " - ";
}
else if (Imaginary > 0 && Real != 0)
{
sign = " + ";
}
else
{
sign = " (NS) ";
}
if (Real == 0)
re = "(R:0)";
else
re = "R:" + Real;
if (Imaginary == 0)
im = "(I:0)";
else if (Imaginary == -1 || Imaginary == 1)
im = "(I:1)";
else
im = "I:" + BigDecimal.Abs(Imaginary);
return re + sign + im;
}
public string ToString(string format)
{
if (this == Zero)
return "0";
string re, im, sign;
if (Imaginary < 0)
{
if (Real == 0)
sign = "-";
else
sign = " - ";
}
else if (Imaginary > 0 && Real != 0)
{
sign = " + ";
}
else
{
sign = "";
}
if (Real == 0)
re = "";
else
re = ((decimal) Real).ToString(format);
if (Imaginary == 0)
im = "";
else if (Imaginary == -1 || Imaginary == 1)
im = "i";
else
im = Math.Abs((decimal) Imaginary).ToString(format) + "i";
return re + sign + im;
}
public override bool Equals(object obj)
{
return obj.ToString() == ToString();
}
public override int GetHashCode()
{
return -1;
}
public bool IsReal()
{
return Imaginary == 0;
}
public bool IsImaginary()
{
return Real == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment