public BigComplex Subtract(BigComplex oth) { BigDecimal x = Real.Subtract(oth.Real); BigDecimal y = Imaginary.Subtract(oth.Imaginary); return(new BigComplex(x, y)); }
public BigComplex Multiply(BigComplex oth, MathContext mc) { BigDecimal a = Real.Add(Imaginary).Multiply(oth.Real); BigDecimal b = oth.Real.Add(oth.Imaginary).Multiply(Imaginary); BigDecimal c = oth.Imaginary.Subtract(oth.Real).Multiply(Real); BigDecimal x = a.Subtract(b, mc); BigDecimal y = a.Add(c, mc); return(new BigComplex(x, y)); }
public BigComplex Multiply(BigComplex oth, MathContext mc) { BigDecimal a = Real.Add(Imaginary).Multiply(oth.Real); BigDecimal b = oth.Real.Add(oth.Imaginary).Multiply(Imaginary); BigDecimal c = oth.Imaginary.Subtract(oth.Real).Multiply(Real); BigDecimal x = a.Subtract(b, mc); BigDecimal y = a.Add(c, mc); return new BigComplex(x, y); }
public BigComplex Divide(BigComplex oth, MathContext mc) { /* lazy implementation: (x+iy)/(a+ib)= (x+iy)* 1/(a+ib) */ return Multiply(oth.Inverse(mc), mc); }
public BigComplex Subtract(BigComplex oth) { BigDecimal x = Real.Subtract(oth.Real); BigDecimal y = Imaginary.Subtract(oth.Imaginary); return new BigComplex(x, y); }
public static BigComplex MultiplyRound(BigComplex x, BigComplex y) { BigDecimal R = SubtractRound(MultiplyRound(x.Real, y.Real), MultiplyRound(x.Imaginary, y.Imaginary)); BigDecimal I = AddRound(MultiplyRound(x.Real, y.Imaginary), MultiplyRound(x.Imaginary, y.Real)); return new BigComplex(R, I); }
public static BigComplex MultiplyRound(BigComplex x, BigDecimal y) { BigDecimal R = MultiplyRound(x.Real, y); BigDecimal I = MultiplyRound(x.Imaginary, y); return new BigComplex(R, I); }
public static BigComplex InvertRound(BigComplex z) { if (z.Imaginary.CompareTo(BigDecimal.Zero) == 0) { /* In this case with vanishing Im(x), the result is simply 1/Re z. */ MathContext mc = new MathContext(z.Real.Precision); return new BigComplex(BigDecimal.One.Divide(z.Real, mc)); } else if (z.Real.CompareTo(BigDecimal.Zero) == 0) { /* In this case with vanishing Re(z), the result is simply -i/Im z */ MathContext mc = new MathContext(z.Imaginary.Precision); return new BigComplex(BigDecimal.Zero, BigDecimal.One.Divide(z.Imaginary, mc).Negate()); } else { /* 1/(x.re+I*x.im) = 1/(x.re+x.im^2/x.re) - I /(x.im +x.re^2/x.im) */ BigDecimal R = AddRound(z.Real, DivideRound(MultiplyRound(z.Imaginary, z.Imaginary), z.Real)); BigDecimal I = AddRound(z.Imaginary, DivideRound(MultiplyRound(z.Real, z.Real), z.Imaginary)); MathContext mc = new MathContext(1 + R.Precision); R = BigDecimal.One.Divide(R, mc); mc = new MathContext(1 + I.Precision); I = BigDecimal.One.Divide(I, mc); return new BigComplex(R, I.Negate()); } }
public static BigComplex DivideRound(BigInteger n, BigComplex x) { /* catch case of real-valued denominator first */ if (x.Imaginary.CompareTo(BigDecimal.Zero) == 0) return new BigComplex(DivideRound(n, x.Real), BigDecimal.Zero); else if (x.Real.CompareTo(BigDecimal.Zero) == 0) return new BigComplex(BigDecimal.Zero, DivideRound(n, x.Imaginary).Negate()); BigComplex z = InvertRound(x); /* n/(x+iy) = nx/(x^2+y^2) -nyi/(x^2+y^2) */ BigDecimal repart = MultiplyRound(z.Real, n); BigDecimal impart = MultiplyRound(z.Imaginary, n); return new BigComplex(repart, impart); }
public static BigComplex DivideRound(BigComplex x, BigComplex y) { return MultiplyRound(x, InvertRound(y)); }
public static BigComplex AddRound(BigComplex x, BigComplex y) { BigDecimal R = AddRound(x.Real, y.Real); BigDecimal I = AddRound(x.Imaginary, y.Imaginary); return new BigComplex(R, I); }
public static BigComplex AddRound(BigComplex x, BigDecimal y) { BigDecimal R = AddRound(x.Real, y); return new BigComplex(R, x.Imaginary); }
public BigComplex Divide(BigComplex oth, MathContext mc) { /* lazy implementation: (x+iy)/(a+ib)= (x+iy)* 1/(a+ib) */ return(Multiply(oth.Inverse(mc), mc)); }