This repository was archived by the owner on Oct 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumber.java
More file actions
92 lines (75 loc) · 2.57 KB
/
ComplexNumber.java
File metadata and controls
92 lines (75 loc) · 2.57 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
package complexNumber;
import static java.lang.Double.compare;
import static java.lang.Math.abs;
import static java.lang.Math.atan;
import static java.lang.Math.sqrt;
/**
* Created by lixir on 09.02.17.
*/
public final class ComplexNumber {
private final double x; //действительная часть valid
private final double y; //мнимая часть imaginary
public ComplexNumber ( double x, double y) {
this.x = x;
this.y = y;
}
public ComplexNumber ( int x, int y){
this.x = x;
this.y = y;
}
public ComplexNumber (double x) {
this.x = x;
this.y = 0;
}
@Override
public String toString() {
if (y == 0) return x + "";
if (y > 0) return x + " + i" + y;
else return x + " - i" + abs(y);
}
@Override
public boolean equals(Object obj){
if (this == obj) return true;
if (obj instanceof ComplexNumber){
ComplexNumber other = (ComplexNumber) obj;
if (abs(this.x - other.x) <= 1e-10 && abs(this.y - other.y) <= 1e-10) return true;
}
return false;
}
@Override
public int hashCode(){
return Double.valueOf(this.x).hashCode() * 31 + Double.valueOf(this.y).hashCode();
}
public ComplexNumber plus(ComplexNumber other) {
return new ComplexNumber(this.x + other.x, this.y + other.y);
}
public ComplexNumber minus(ComplexNumber other) {
return new ComplexNumber(this.x - other.x, this.y - other.y);
}
public ComplexNumber multipli(ComplexNumber other) {
return new ComplexNumber(this.x * other.x - this.y * other.y, this.x * other.y + other.x * this.y);
}
public ComplexNumber divided(ComplexNumber other) {
return new ComplexNumber((
this.x * other.x + this.y * other.y)/(other.x * other.x + other.y * other.y),
(other.x * this.y - other.y * this.x)/(other.x * other.x + other.y * other.y));
}
public ComplexNumber multipli(int k) {
return new ComplexNumber(this.x * k, this.y * k);
}
public double modulus() {
return sqrt(this.x * this.x + this.y * this.y);
}
public double argument() {
return atan( this.y / this.x);
}
public ComplexNumber linking(){
return new ComplexNumber(this.x, - this.y);
}
public ComplexNumber complexSqrt(){
double x = sqrt((this.modulus() + this.x)/2);
double y = sqrt((this.modulus() - this.x)/2);
if (this.y != 0) y *= compare(0, this.y);
return new ComplexNumber(x, y);
}
}