-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStrategy.java
More file actions
215 lines (175 loc) · 6.22 KB
/
Strategy.java
File metadata and controls
215 lines (175 loc) · 6.22 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
import java.util.Objects;
interface DiscriminantStrategy {
double calculateDiscriminant(double a, double b, double c);
}
class OrdinaryDiscriminantStrategy implements DiscriminantStrategy {
@Override
public double calculateDiscriminant(double a, double b, double c) {
return b * b - 4 * a * c;
}
}
class RealDiscriminantStrategy implements DiscriminantStrategy {
@Override
public double calculateDiscriminant(double a, double b, double c) {
double disc = b * b - 4 * a * c;
if (disc < 0.0) {
return Double.NaN;
}
return disc;
}
}
class QuadraticEquationSolver {
private DiscriminantStrategy strategy;
public QuadraticEquationSolver(DiscriminantStrategy strategy) {
this.strategy = strategy;
}
public Pair<Complex, Complex> solve(double a, double b, double c) {
double discriminate = strategy.calculateDiscriminant(a, b, c);
Complex rootDiscriminate = Complex.sqrt(discriminate);
Complex term1 = new Complex(-b, 0);
Complex denom = new Complex(2.0 * a, 0);
Complex plus = Complex.plus(term1, rootDiscriminate).divides(denom);
Complex minus = rootDiscriminate.times(new Complex(-1.0, 0.0)).plus(term1).divides(denom);
return new Pair<>(plus, minus);
}
}
// complex number implementation taken from here:
// https://introcs.cs.princeton.edu/java/32class/Complex.java.html
class Complex {
private final double re; // the real part
private final double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
re = real;
im = imag;
}
// return a string representation of the invoking Complex object
public String toString() {
if (im == 0)
return re + "";
if (re == 0)
return im + "i";
if (im < 0)
return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
// return abs/modulus/magnitude
public double abs() {
return Math.hypot(re, im);
}
// return angle/phase/argument, normalized to be between -pi and pi
public double phase() {
return Math.atan2(im, re);
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
// return a new object whose value is (this * alpha)
public Complex scale(double alpha) {
return new Complex(alpha * re, alpha * im);
}
// return a new Complex object whose value is the conjugate of this
public Complex conjugate() {
return new Complex(re, -im);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re * re + im * im;
return new Complex(re / scale, -im / scale);
}
// return the real or imaginary part
public double re() {
return re;
}
public double im() {
return im;
}
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.times(b.reciprocal());
}
// return a new Complex object whose value is the complex exponential of
// this
public Complex exp() {
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
// return a new Complex object whose value is the complex sine of this
public Complex sin() {
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex cosine of this
public Complex cos() {
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
public static Complex sqrt(double value) {
if (value < 0)
return new Complex(0, Math.sqrt(-value));
else
return new Complex(Math.sqrt(value), 0);
}
// return a new Complex object whose value is the complex tangent of this
public Complex tan() {
return sin().divides(cos());
}
// a static version of plus
public static Complex plus(Complex a, Complex b) {
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
// See Section 3.3.
public boolean equals(Object x) {
if (x == null)
return false;
if (this.getClass() != x.getClass())
return false;
Complex that = (Complex) x;
return (this.re == that.re) && (this.im == that.im);
}
// See Section 3.3.
public int hashCode() {
return Objects.hash(re, im);
}
public boolean isNaN() {
return Double.isNaN(re) || Double.isNaN(im);
}
}
class Pair<X, Y> {
public final X first;
public final Y second;
public Pair(X first, Y second) {
this.first = first;
this.second = second;
}
public String toString() {
return String.format("(%s,%s)", first, second);
}
}
class DemoStrategy {
public static void main(String[] args) {
QuadraticEquationSolver s1 = new QuadraticEquationSolver(new OrdinaryDiscriminantStrategy());
System.out.println(s1.solve(5, 10, 20));
QuadraticEquationSolver s2 = new QuadraticEquationSolver(new RealDiscriminantStrategy());
System.out.println(s2.solve(5, 10, 20));
}
}