-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexNumber.java
141 lines (122 loc) · 3.31 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package algebra;
public class ComplexNumber{
private boolean polar = false;
private Vector num;
private Vector getVec() {
return num;
}
//Creates complex number
public ComplexNumber(Double x, Double y) {
num = new Vector(x, y);
}
//Gets real part of complex number
public Double real() {
return num.getValue(0);
}
//Gets imaginary part of complex number
public Double imaginary() {
return num.getValue(1);
}
//Gets magnitude of complex number for polar form
public Double magnitude() {
return this.num.magnitude();
}
//Gets argument of complex number for polar form
public Double arg() {
return Double.valueOf(Math.atan2(this.num.getValue(1), this.num.getValue(0)));
}
//Adds new complex number to current
public void add(ComplexNumber i){
if(polar) {
try {
throw new PolarFormException("Add in cartiesian");
} catch (PolarFormException e) {
e.printStackTrace();
}
}
else {
try {
this.num.add(i.getVec());
} catch (NotEnoughException e) {
e.printStackTrace();
}
}
}
//Subtracts new complex number from current
public void sub(ComplexNumber i){
if(polar) {
try {
throw new PolarFormException("Subtract in cartiesian");
} catch (PolarFormException e) {
e.printStackTrace();
}
}
else {
try {
this.num.sub(i.getVec());
} catch (NotEnoughException e) {
e.printStackTrace();
}
}
}
//Multiplies new complex number by current one
public void mult(ComplexNumber i) {
if(polar) {
this.num.setValue(0, this.magnitude()*i.magnitude());
this.num.setValue(0, this.arg()+i.arg());
}
else {
this.num.setValue(0, this.real()*i.real() - this.imaginary()*i.imaginary());
this.num.setValue(1, this.real()*i.imaginary() + this.imaginary()*i.real());
}
}
//Multiplies complex number by a Double
public void mult(Double i) {
if(polar)
this.num.setValue(0, this.magnitude()*i);
else
this.num.multiply(i);
}
public void div(ComplexNumber i ) {
if(polar) {
this.num.setValue(0, this.magnitude()/i.magnitude());
this.num.setValue(1, this.arg() - i.arg());
}
else {
Double v = Math.pow(i.real(), 2)+Math.pow(i.imaginary(), 2);
this.num.setValue(0, (this.real()*i.real() + this.imaginary()*i.imaginary())/v);
this.num.setValue(1, (i.imaginary()*i.real() + this.real()*i.imaginary())/v);
}
}
//Returns conjugate of complex number
public ComplexNumber conj() {
ComplexNumber z = new ComplexNumber(this.real(), -this.imaginary());
return z;
}
//Returns inverse of complex number
public ComplexNumber invrs() {
Double v = Math.pow(this.real(), 2)+Math.pow(this.imaginary(), 2);
ComplexNumber z = new ComplexNumber(this.real()/v, -this.imaginary()/v);
return z;
}
//Converts to polar form
public void toPolar() {
this.num.setValue(0, this.magnitude());
this.num.setValue(1, this.arg());
}
//Converts to Cartesian
public void toCatesian() {
this.num.setValue(0, this.magnitude()*Math.cos(this.arg()));
this.num.setValue(1, this.magnitude()*Math.sin(this.arg()));
}
//Overide's to string method
@Override
public String toString() {
if(num.getValue(0) == 0.0 && num.getValue(1) == 0.0)
return "0.0";
else if(polar)
return String.format("%.2fe^(%.2f)",this.magnitude(), this.arg());
else
return String.format("%.2f + %.2fi", this.real(), this.imaginary());
}
}