-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathComplex.java
More file actions
226 lines (200 loc) · 6.52 KB
/
Copy pathComplex.java
File metadata and controls
226 lines (200 loc) · 6.52 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package examples.Mandelbrot;
public class Complex{
private double x,y;
/**
Constructs the complex number z = u + i*v
@param u Real part
@param v Imaginary part
*/
public Complex(double u,double v) {
x=u;
y=v;
}
/**
Real part of this Complex number
(the x-coordinate in rectangular coordinates).
@return Re[z] where z is this Complex number.
*/
public double real() {
return x;
}
/**
Imaginary part of this Complex number
(the y-coordinate in rectangular coordinates).
@return Im[z] where z is this Complex number.
*/
public double imag() {
return y;
}
/**
Modulus of this Complex number
(the distance from the origin in polar coordinates).
@return |z| where z is this Complex number.
*/
public double mod() {
if (x!=0 || y!=0) {
return Math.sqrt(x*x+y*y);
} else {
return 0d;
}
}
/**
Argument of this Complex number
(the angle in radians with the x-axis in polar coordinates).
@return arg(z) where z is this Complex number.
*/
public double arg() {
return Math.atan2(y,x);
}
/**
Complex conjugate of this Complex number
(the conjugate of x+i*y is x-i*y).
@return z-bar where z is this Complex number.
*/
public Complex conj() {
return new Complex(x,-y);
}
/**
Addition of Complex numbers (doesn't change this Complex number).
<br>(x+i*y) + (s+i*t) = (x+s)+i*(y+t).
@param w is the number to add.
@return z+w where z is this Complex number.
*/
public Complex plus(Complex w) {
return new Complex(x+w.real(),y+w.imag());
}
/**
Subtraction of Complex numbers (doesn't change this Complex number).
<br>(x+i*y) - (s+i*t) = (x-s)+i*(y-t).
@param w is the number to subtract.
@return z-w where z is this Complex number.
*/
public Complex minus(Complex w) {
return new Complex(x-w.real(),y-w.imag());
}
/**
Complex multiplication (doesn't change this Complex number).
@param w is the number to multiply by.
@return z*w where z is this Complex number.
*/
public Complex times(Complex w) {
return new Complex(x*w.real()-y*w.imag(),x*w.imag()+y*w.real());
}
/**
Division of Complex numbers (doesn't change this Complex number).
<br>(x+i*y)/(s+i*t) = ((x*s+y*t) + i*(y*s-y*t)) / (s^2+t^2)
@param w is the number to divide by
@return new Complex number z/w where z is this Complex number
*/
public Complex div(Complex w) {
double den=Math.pow(w.mod(),2);
return new Complex((x*w.real()+y*w.imag())/den,(y*w.real()-x*w.imag())/den);
}
/**
Complex exponential (doesn't change this Complex number).
@return exp(z) where z is this Complex number.
*/
public Complex exp() {
return new Complex(Math.exp(x)*Math.cos(y),Math.exp(x)*Math.sin(y));
}
/**
Principal branch of the Complex logarithm of this Complex number.
(doesn't change this Complex number).
The principal branch is the branch with -pi < arg <= pi.
@return log(z) where z is this Complex number.
*/
public Complex log() {
return new Complex(Math.log(this.mod()),this.arg());
}
/**
Complex square root (doesn't change this complex number).
Computes the principal branch of the square root, which
is the value with 0 <= arg < pi.
@return sqrt(z) where z is this Complex number.
*/
public Complex sqrt() {
double r=Math.sqrt(this.mod());
double theta=this.arg()/2;
return new Complex(r*Math.cos(theta),r*Math.sin(theta));
}
// Real cosh function (used to compute complex trig functions)
private double cosh(double theta) {
return (Math.exp(theta)+Math.exp(-theta))/2;
}
// Real sinh function (used to compute complex trig functions)
private double sinh(double theta) {
return (Math.exp(theta)-Math.exp(-theta))/2;
}
/**
Sine of this Complex number (doesn't change this Complex number).
<br>sin(z) = (exp(i*z)-exp(-i*z))/(2*i).
@return sin(z) where z is this Complex number.
*/
public Complex sin() {
return new Complex(cosh(y)*Math.sin(x),sinh(y)*Math.cos(x));
}
/**
Cosine of this Complex number (doesn't change this Complex number).
<br>cos(z) = (exp(i*z)+exp(-i*z))/ 2.
@return cos(z) where z is this Complex number.
*/
public Complex cos() {
return new Complex(cosh(y)*Math.cos(x),-sinh(y)*Math.sin(x));
}
/**
Hyperbolic sine of this Complex number
(doesn't change this Complex number).
<br>sinh(z) = (exp(z)-exp(-z))/2.
@return sinh(z) where z is this Complex number.
*/
public Complex sinh() {
return new Complex(sinh(x)*Math.cos(y),cosh(x)*Math.sin(y));
}
/**
Hyperbolic cosine of this Complex number
(doesn't change this Complex number).
<br>cosh(z) = (exp(z) + exp(-z)) / 2.
@return cosh(z) where z is this Complex number.
*/
public Complex cosh() {
return new Complex(cosh(x)*Math.cos(y),sinh(x)*Math.sin(y));
}
/**
Tangent of this Complex number (doesn't change this Complex number).
<br>tan(z) = sin(z)/cos(z).
@return tan(z) where z is this Complex number.
*/
public Complex tan() {
return (this.sin()).div(this.cos());
}
/**
Negative of this complex number (chs stands for change sign).
This produces a new Complex number and doesn't change
this Complex number.
<br>-(x+i*y) = -x-i*y.
@return -z where z is this Complex number.
*/
public Complex chs() {
return new Complex(-x,-y);
}
/**
String representation of this Complex number.
@return x+i*y, x-i*y, x, or i*y as appropriate.
*/
public String toString() {
if (x!=0 && y>0) {
return x+" + "+y+"i";
}
if (x!=0 && y<0) {
return x+" - "+(-y)+"i";
}
if (y==0) {
return String.valueOf(x);
}
if (x==0) {
return y+"i";
}
// shouldn't get here (unless Inf or NaN)
return x+" + i*"+y;
}
}