1 // GeometricAlgebra Java package file
2 // Current version
3 // Go to ... http://sinai.mech.fukui-u.ac.jp/gcj/software/KamiWaAi/index.html
4 // Copyright (C) 2003, Eckhard M.S. Hitzer
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // Go to ... http://www.gnu.org/copyleft/lesser.html
20 //
21 // hitzer@mech.fukui-u.ac.jp
22 //
23 // Department of Mechanical Engineering, Fukui University
24 // 3-9-1 Bunkyo, 910-8507 Fukui, Japan
25 //
26 //
27 //
28 // It the personal wish of E.M.S. Hitzer,
29 // that you never use this software for infringing human dignity:
30 //
31 // "God created man in his own image,
32 // in the image of God he created him;
33 // male and female he created them.
34 // [The Bible, Genesis chapter 1 verse 27]
35 //
36 //
37 //
38 //
39 // File ComplexNumber.java
40 // Dec. 2002
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 // void setRePart(double rp) {
71 // real = rp;
72 // }
73 //
74 // void setImPart(double im) {
75 // imaginary = im;
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 // TODO Auto-generated method stub
116 return super.hashCode();
117 }
118
119 }