-
Notifications
You must be signed in to change notification settings - Fork 8
/
BigComplex.java
59 lines (47 loc) · 1.95 KB
/
BigComplex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.math.BigDecimal;
import java.math.MathContext;
// Complex number arithmetic using BigDecimal components for real and imaginary
// parts of the number.
public class BigComplex {
private static final int INITPRECISION = 10;
public static MathContext mc = new MathContext(INITPRECISION);
public static final BigComplex ZERO = new BigComplex(0, 0);
public static final BigComplex ONE = new BigComplex(1, 0);
public static final BigComplex I = new BigComplex(0, 1);
// A complex number consists of real and imaginary parts.
private final BigDecimal re;
private final BigDecimal im;
public BigComplex(double re, double im) {
this.re = new BigDecimal(re, mc);
this.im = new BigDecimal(im, mc);
}
public BigComplex(BigDecimal re, BigDecimal im) {
this.re = re; this.im = im;
}
public BigDecimal getRe() { return re; }
public BigDecimal getIm() { return im; }
public BigComplex add(BigComplex other) {
return new BigComplex(this.re.add(other.re, mc), this.im.add(other.im, mc));
}
public BigComplex subtract(BigComplex other) {
return new BigComplex(this.re.subtract(other.re, mc), this.im.subtract(other.im, mc));
}
public BigComplex multiply(BigComplex other) {
BigDecimal r1 = this.re.multiply(other.re, mc);
BigDecimal r2 = this.im.multiply(other.im, mc);
BigDecimal i1 = this.im.multiply(other.re, mc);
BigDecimal i2 = other.im.multiply(this.re, mc);
return new BigComplex(r1.subtract(r2, mc), i1.add(i2, mc));
}
public BigDecimal getAbsSq() {
return this.re.multiply(this.re, mc).add(this.im.multiply(this.im, mc), mc);
}
public String toString() {
if(this.im.compareTo(BigDecimal.ZERO) >= 0) {
return "(" + this.re + "+" + this.im + "i)";
}
else {
return "(" + this.re + "" + this.im + "i)";
}
}
}