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 package net.sourceforge.kamiwaai.geometricalgebra;
43
44 class ComplexNumber {
45 public static final ComplexNumber ZERO = new ComplexNumber(0.0, 0.0);
46 private double real, imaginary;
47
48 public ComplexNumber(double realPart, double imaginaryPart) {
49 real = realPart;
50 imaginary = imaginaryPart;
51 }
52
53 public ComplexNumber(ComplexNumber complex) {
54 real = complex.RealPart();
55 imaginary = complex.ImaginaryPart();
56 }
57
58 public double RealPart() {
59 return real;
60 }
61
62 public double ImaginaryPart() {
63 return imaginary;
64 }
65
66 public double Magnitude() {
67 return Math.sqrt(real * real + imaginary * imaginary);
68 }
69
70
71
72
73
74
75
76
77
78 public ComplexNumber add(ComplexNumber cn) {
79 return new ComplexNumber(
80 real + cn.RealPart(),
81 imaginary + cn.ImaginaryPart());
82 }
83
84 public ComplexNumber sub(ComplexNumber cn) {
85 return new ComplexNumber(
86 real - cn.RealPart(),
87 imaginary - cn.ImaginaryPart());
88 }
89
90 public ComplexNumber mult(ComplexNumber cn) {
91 double Re, Im;
92
93 Re = real * cn.RealPart() - imaginary * cn.ImaginaryPart();
94 Im = imaginary * cn.RealPart() + real * cn.ImaginaryPart();
95
96 return new ComplexNumber(Re, Im);
97 }
98
99 public String toString() {
100 return "[" + RealPart() + ", " + ImaginaryPart() + "]";
101 }
102
103 public boolean equals(Object obj) {
104 if (obj == null)
105 throw new NullPointerException("obj is null");
106 if (!(obj instanceof ComplexNumber))
107 return false;
108 ComplexNumber complex = (ComplexNumber) obj;
109 return (
110 (complex.RealPart() == RealPart())
111 && (complex.ImaginaryPart() == ImaginaryPart()));
112 }
113
114 public int hashCode() {
115
116 return super.hashCode();
117 }
118
119 }