-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMyComplex.java
More file actions
96 lines (75 loc) · 2.3 KB
/
MyComplex.java
File metadata and controls
96 lines (75 loc) · 2.3 KB
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
package src;
public class MyComplex {
private double real;
private double imag;
public MyComplex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public double getReal() {
return this.real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return this.imag;
}
public void setImag(double imag) {
this.imag = imag;
}
public void setValue(double real, double imag) {
this.real = real;
this.imag = imag;
}
public boolean isReal() {
return (imag == 0);
}
public boolean isImaginary() {
return (real == 0);
}
public boolean equals(double real, double imag) {
return (this.real == real && this.imag == imag);
}
public boolean equals(MyComplex another) {
double epsilon = 10E-8;
return (real - another.getReal() <= epsilon && imag - another.getImag() <= epsilon);
}
public double magnitude() {
return Math.sqrt(real*real + imag*imag);
}
public double argumentInRadians() {
return Math.atan2(imag, real);
}
public int argumentInDegrees() {
return 0;
}
public MyComplex conjugate() {
return new MyComplex(real, -imag);
}
public MyComplex add(MyComplex another) {
return new MyComplex(real+another.getReal(), imag+another.getImag());
}
public MyComplex substract(MyComplex another) {
return new MyComplex(real-another.getReal(), imag-another.getImag());
}
public MyComplex multiplyWith(MyComplex another) {
// (ac - bd) + (ad + bc)i
real = real*another.getReal() - imag*another.getImag();
imag = real*another.getImag() + imag*another.getReal();
return this;
}
public MyComplex divideBy(MyComplex another) {
// [(a + bi) * (c – di)] / (c2 + d2)
MyComplex tmp = multiplyWith(another.conjugate());
double delimiter = another.getReal()*another.getReal() + another.getImag()*another.getImag();
if (delimiter != 0) {
real = tmp.getReal() / delimiter;
imag = tmp.getImag() / delimiter;
}
return this;
}
public String toString() {
return "("+real +" + "+ imag+"i)" ;
}
}