-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexNumber.java
122 lines (96 loc) · 3.1 KB
/
ComplexNumber.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class ComplexNumber implements Comparable<ComplexNumber>{
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber num) {
real += num.getReal();
imaginary += num.getImaginary();
return this;
}
public ComplexNumber subtract(ComplexNumber num) {
real -= num.getReal();
imaginary -= num.getImaginary();
return this;
}
public ComplexNumber multiply(ComplexNumber num) {
double temp = real * num.getReal() - imaginary * num.getImaginary();
imaginary = real * num.getImaginary() + imaginary * num.getReal();
real = temp;
return this;
}
public ComplexNumber pow(int power) {
ComplexNumber z = new ComplexNumber(1, 0);
for(int i = 0; i < power; i++)
z.multiply(this);
for(int i = 0; i > power; i--)
z.divide(this);
return z;
}
public ComplexNumber divide(ComplexNumber num) {
return multiply(num.reciprocal());
}
public ComplexNumber scale(double num) {
real *= num;
imaginary *= num;
return this;
}
/*
1/(a + bi) = (a - bi)/(a - bi) * 1/(a + bi) = (a - bi) / (a * a + b * b)
*/
public ComplexNumber reciprocal() {
return conjugate().scale(1 / squaredMagnitude());
}
public ComplexNumber conjugate() {
return new ComplexNumber(real, -imaginary);
}
public double squaredMagnitude() {
return real * real + imaginary * imaginary;
}
public double magnitude() {
return Math.sqrt(squaredMagnitude());
}
public double phase() {
return Math.atan2(imaginary, real);
}
public ComplexNumber exp() {
return new ComplexNumber(Math.cos(imaginary), Math.sin(imaginary)).scale(Math.exp(real));
}
public ComplexNumber sin() {
return new ComplexNumber(Math.sin(real) * Math.cosh(imaginary), Math.cos(real) * Math.sinh(imaginary));
}
public ComplexNumber cos() {
return new ComplexNumber(Math.cos(real) * Math.cosh(imaginary), -Math.sin(real) * Math.sinh(imaginary));
}
public ComplexNumber tan() {
return sin().divide(cos());
}
public ComplexNumber clone() {
return new ComplexNumber(real, imaginary);
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public boolean equals(ComplexNumber x) {
if(x == null) return false;
return real == x.getReal() && imaginary == x.getImaginary();
}
@Override
public int compareTo(ComplexNumber o) {
return squaredMagnitude() < o.squaredMagnitude()? -1 : squaredMagnitude() == o.squaredMagnitude()? 0: 1;
}
public String toString() {
return real + " + i * " + imaginary;
}
}