forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.java
164 lines (158 loc) · 4.56 KB
/
Complex.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Objects;
/**
*
* This is an implementation of Complex numbers in Java.
*
* @author Ricardo Prins
* @since 12-01-2019
* @version 1.0.0
*
*/
public class Complex {
private double real; // the real part
private double imaginary; // the imaginary part
/**
* Creates a complex number.
* @param real
* @param imaginary
*/
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public double getImaginary() {
return imaginary;
}
public void setReal(double real) {
this.real = real;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
/**
* @return modulus/absolute value of a given complex number
*/
public double abs() {
return Math.hypot(real, imaginary);
}
/**
* @return angle/phase/argument, normalized to be between -pi and pi
*/
public double phase() {
return Math.atan2(imaginary, real);
}
/**
* @param b complex to be added
* @return new Complex object whose value is (this + b)
*/
public Complex add(Complex b) {
Complex a = this;
double real = a.real + b.real;
double imag = a.imaginary + b.imaginary;
return new Complex(real, imag);
}
/**
* Static version of add method
* @param a
* @param b
* @return
*/
public static Complex add(Complex a, Complex b) {
double real = a.real + b.real;
double imag = a.imaginary + b.imaginary;
return new Complex(real, imag);
}
/**
* @param b
* @return a new Complex object whose value is (this - b)
*/
public Complex subtract(Complex b) {
Complex a = this;
double real = a.real - b.real;
double imag = a.imaginary - b.imaginary;
return new Complex(real, imag);
}
/**
* @param b
* @return a new Complex object whose value is (this * b)
*/
public Complex multiply(Complex b) {
Complex a = this;
double resultReal = a.real * b.real - a.imaginary * b.imaginary;
double resultImaginary = a.real * b.imaginary + a.imaginary * b.real;
return new Complex(resultReal, resultImaginary);
}
/**
* @param b
* @return Complex a/b
*/
public Complex divide(Complex b) {
Complex a = this;
return a.multiply(b.reciprocal());
}
/**
* @return a new Complex object whose value is the complex exponential of this
*/
public Complex exp() {
return new Complex(Math.exp(real) * Math.cos(imaginary), Math.exp(real) * Math.sin(imaginary));
}
/**
* @return a new Complex object whose value is the complex sine of this
*/
public Complex sin() {
return new Complex(Math.sin(real) * Math.cosh(imaginary), Math.cos(real) * Math.sinh(imaginary));
}
/**
* @return a new Complex object whose value is the complex cosine of this
*/
public Complex cos() {
return new Complex(Math.cos(real) * Math.cosh(imaginary), -Math.sin(real) * Math.sinh(imaginary));
}
/**
* @return a new Complex object whose value is the complex tangent of this
*/
public Complex tan() {
return sin().divide(cos());
}
/**
* @param alpha
* @return a new object whose value is (this * alpha)
*/
public Complex scale(double alpha) {
return new Complex(alpha * real, alpha * imaginary);
}
/**
* @return a new Complex object whose value is the conjugate of this
*/
public Complex conjugate() {
return new Complex(real, -imaginary);
}
/**
* @return a new Complex object whose value is the reciprocal of this
*/
public Complex reciprocal() {
double scale = real * real + imaginary * imaginary;
return new Complex(real / scale, -imaginary / scale);
}
@Override
public boolean equals(Object x) {
if (x == null) return false;
if (this.getClass() != x.getClass()) return false;
Complex that = (Complex) x;
return (this.real == that.real) && (this.imaginary == that.imaginary);
}
@Override
public int hashCode() {
return Objects.hash(real, imaginary);
}
@Override
public String toString() {
if (imaginary == 0) return real + "";
if (real == 0) return imaginary + "i";
if (imaginary < 0) return real + " - " + (-imaginary) + "i";
return real + " + " + imaginary + "i";
}
}