static public BigComplex invertRound(final BigComplex z) {
if (z.im.compareTo(BigDecimal.ZERO) == 0) {
/*
* In this case with vanishing Im(x), the result is simply 1/Re z.
*/
final MathContext mc = new MathContext(z.re.precision());
return new BigComplex(BigDecimal.ONE.divide(z.re, mc));
}
else if (z.re.compareTo(BigDecimal.ZERO) == 0) {
/*
* In this case with vanishing Re(z), the result is simply -i/Im z
*/
final MathContext mc = new MathContext(z.im.precision());
return new BigComplex(BigDecimal.ZERO, BigDecimal.ONE.divide(z.im, 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.re, divideRound(multiplyRound(z.im, z.im), z.re));
BigDecimal I = addRound(z.im, divideRound(multiplyRound(z.re, z.re), z.im));
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());
}
}