-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathComplex.java
More file actions
123 lines (97 loc) · 3.6 KB
/
Copy pathComplex.java
File metadata and controls
123 lines (97 loc) · 3.6 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
package solver;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Complex {
private final double re;
private final double im;
private String decimalPattern = "#.####";
public static final Complex ZERO = new Complex(0, 0);
public Complex(double real, double imag) {
this.re = real;
this.im = imag;
}
public Complex(String str) {
Complex cmp = parseComplex(str);
this.re = cmp.re;
this.im = cmp.im;
}
public Complex(Complex b) {
this.re = b.re;
this.im = b.im;
}
@Override
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);
}
public Complex add(Complex b) {
Complex a = this;
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
public Complex subtract(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
public Complex multiply(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.im * b.re + a.re * b.im;
return new Complex(real, imag);
}
public Complex reciprocal() {
Complex a = this;
double denominator = a.re * a.re + a.im * a.im;
double real = a.re / denominator;
double imag = - a.im / denominator;
return new Complex(real, imag);
}
public Complex divide(Complex b) {
Complex a = this;
return a.multiply(b.reciprocal());
}
public static Complex parseComplex(String inp) {
Pattern imagPattern = Pattern.compile("([+-]?\\d+(\\.?\\d+)?([eE][+-]?\\d)?[i|I])|([+-]?[i|I](\\d+(\\.?\\d+)?)?([eE][+-]?\\d)?)");
// My numerous attempts, i leave it here)
// Pattern realPattern = Pattern.compile("([+-]?(?!i|I)?\\d++(\\.?\\d++)?([eE][+-]?\\d)?(?!i|I))");
// Pattern realPattern = Pattern.compile("(?!(([+-]?\\d+(\\.?\\d+)?([eE][+-]?\\d)?[i|I])))");
// Matcher re = realPattern.matcher(inp);
// re.find();
// System.out.printf("Re: " + re.group() + " ");
Matcher im = imagPattern.matcher(inp);
String impart, repart;
try {
im.find();
impart = im.group();
} catch (IllegalStateException e) {
impart = ""; // If a repart or impart is blank it will be zero
}
repart = inp.replace(impart, ""); // It is easier than finding/creating regexp pattern to find real part
if(impart.equals("+i") || impart.equals("+I") || impart.equals("i") || impart.equals("I")) {
impart = "1i";
} else if(impart.equals("-i") || impart.equals("-I")) {
impart = "-1i";
}
double rep = repart.isBlank() ? 0 : Double.parseDouble(repart);
double imp = impart.isBlank() ? 0 : Double.parseDouble(impart.replaceAll("i|I", ""));
return new Complex(rep, imp);
}
public void setDecimalPattern(String pattern) {
this.decimalPattern = pattern;
}
@Override
public String toString() {
String ans;
DecimalFormat f = new DecimalFormat(decimalPattern);
if (im == 0) return f.format(re) + "";
if (re == 0) return f.format(im) + "i";
if (im < 0) return f.format(re) + "-" + f.format(-im) + "i";
return f.format(re) + "+" + f.format(im) + "i";
}
}