KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > math > RealComplexINumber


1 /*
2  * RealComplexINumber.java
3  *
4  * Created on April 18, 2005, 11:09 PM
5  *
6  * To change this template, choose Tools | Options and locate the template under
7  * the Source Creation and Management node. Right-click the template and choose
8  * Open. You can then make changes to the template in the Source Editor.
9  */

10
11 package math;
12
13 /**
14  * Represents complex number.
15  * @author jungi
16  */

17 public class RealComplexINumber implements INumber {
18     
19     private double a;
20     private double b;
21     
22     /** Creates a new instance of RealComplexINumber */
23     public RealComplexINumber() {
24         this(0, 0);
25     }
26
27     /** Creates a new instance of RealComplexINumber */
28     public RealComplexINumber(double real) {
29         this(real, 0);
30     }
31
32     /**
33      * Creates a new instance of RealComplexINumber
34      */

35     public RealComplexINumber(double real, double im) {
36         a = real;
37         b = im;
38     }
39
40     /**
41      * u + v = (a + c) + (b + d)i
42      */

43     public INumber add(INumber no) {
44         RealComplexINumber cn = getComplexINumber(no);
45         return new RealComplexINumber(a + cn.a, b + cn.b);
46     }
47
48     /**
49      * u - v = (a - c) + (b - d)i
50      */

51     public INumber subtract(INumber no) {
52         RealComplexINumber cn = getComplexINumber(no);
53         return new RealComplexINumber(a - cn.a, b - cn.b);
54     }
55
56     /**
57      * u * v = (ac - bd) + (ad + bc)i
58      */

59     public INumber multiply(INumber no) {
60         RealComplexINumber cn = getComplexINumber(no);
61         double rp = a * cn.a - b * cn.b;
62         double ip = a * cn.b - b * cn.a;
63         return new RealComplexINumber(rp, ip);
64     }
65
66     public INumber min(INumber no) {
67         throw new UndefinedOperationException();
68     }
69
70     public INumber max(INumber no) {
71         throw new UndefinedOperationException();
72     }
73
74     /**
75      * u / v = u * v.pow(-1)
76      */

77     public INumber divide(INumber no) {
78         RealComplexINumber cn = getComplexINumber(no);
79         if (cn.a + cn.b == 0) {
80             throw new UndefinedOperationException();
81         }
82         //return multiply(new Zlomek(cn.sdruzene(), cn.absAsRealNumber()));
83
return null;
84     }
85
86     /**
87      * -z = -a - bi
88      */

89     public INumber negate() {
90         return new RealComplexINumber(-1 * a, -1 * b);
91     }
92
93     public boolean isReal() {
94         return b == 0;
95     }
96
97     public boolean isDecimal() {
98         return false;
99     }
100
101     /**
102      * |z| = sqrt(sqr(a) + sqr(b))
103      */

104     public INumber abs() {
105         return new RealComplexINumber(absAsRealNumber(), 0);
106     }
107     
108     public double absAsRealNumber() {
109         return Math.hypot(a, b);
110     }
111     
112     public INumber sdruzene() {
113         return new RealComplexINumber(a, -1 * b);
114     }
115     
116     public INumber prevracene() {
117         double d = absAsRealNumber();
118         //return new Zlomek(sdruzene(), d * d);
119
return null;
120     }
121     
122     private RealComplexINumber getComplexINumber(INumber no) {
123         if (!(no instanceof RealComplexINumber)) {
124             throw new UndefinedOperationException();
125         }
126         return (RealComplexINumber) no;
127     }
128     
129     public String JavaDoc toString() {
130         StringBuilder JavaDoc retVal = new StringBuilder JavaDoc(12);
131         if (a != 0) {
132             if (a > 0) {
133                 retVal.append(a);
134             } else {
135                 retVal.append(" - ");
136                 retVal.append(Math.abs(a));
137             }
138         }
139         if (b != 0) {
140             retVal.append(" ");
141             if (b > 0) {
142                 if (a != 0) {
143                     retVal.append("+ ");
144                 }
145                 if (b != 1) {
146                     retVal.append(b);
147                 }
148             } else {
149                 retVal.append("- ");
150                 if (b != -1) {
151                     retVal.append(Math.abs(b));
152                 }
153             }
154             retVal.append("i");
155         }
156         return retVal.toString().trim().replace(".0", "");
157     }
158     
159     public boolean equals(Object JavaDoc obj) {
160         boolean retVal = obj instanceof RealComplexINumber;
161         if (retVal) {
162             RealComplexINumber cn = (RealComplexINumber) obj;
163             retVal = (a == cn.a) && (b == cn.b);
164         }
165         return retVal;
166     }
167     
168     public int hashCode() {
169         return (int) (a * 5 + b * 7 + 27);
170     }
171     
172
173 }
174
Popular Tags