KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > analysis > ComplexFunction


1 package JSci.maths.analysis;
2
3 import JSci.maths.Complex;
4 import JSci.maths.ComplexMapping;
5 import JSci.maths.groups.AbelianGroup;
6 import JSci.maths.fields.Ring;
7 import JSci.maths.polynomials.ComplexPolynomial;
8
9 /**
10 * This class describes a function on the complex numbers.
11 * @version 1.0
12 * @author Mark Hale
13 */

14 public abstract class ComplexFunction implements ComplexMapping, Ring.Member {
15     /**
16     * Returns the (complex) dimension of the space this function is on.
17     */

18     public final int dimension() {
19         return 1;
20     }
21     public Object JavaDoc getSet() {
22         throw new RuntimeException JavaDoc("Not implemented: please file bug report");
23     }
24
25         public ComplexFunction compose(ComplexFunction f) {
26                 return new Composition(this, f);
27         }
28         private static class Composition extends ComplexFunction {
29                 private final ComplexFunction f1, f2;
30                 public Composition(ComplexFunction f1, ComplexFunction f2) {
31                         this.f1 = f1;
32                         this.f2 = f2;
33                 }
34                 public Complex map(double x, double y) {
35                         return f1.map(f2.map(x, y));
36                 }
37                 public Complex map(Complex z) {
38                         return f1.map(f2.map(z));
39                 }
40                 /**
41                 * Chain rule.
42                 */

43                 public ComplexFunction differentiate() {
44                         return new Product(new Composition(f1.differentiate(), f2), f2.differentiate());
45                 }
46         }
47
48         /**
49         * Returns the negative of this function.
50         */

51         public AbelianGroup.Member negate() {
52                 return new Negation(this);
53         }
54         private static class Negation extends ComplexFunction {
55                 private final ComplexFunction f;
56                 public Negation(ComplexFunction f) {
57                         this.f = f;
58                 }
59                 public Complex map(double x, double y) {
60                         return (Complex) f.map(x, y).negate();
61                 }
62                 public Complex map(Complex z) {
63                         return (Complex) f.map(z).negate();
64                 }
65                 public ComplexFunction differentiate() {
66                         return new Negation(f.differentiate());
67                 }
68         }
69
70         /**
71         * Returns the addition of this function and another.
72         */

73         public AbelianGroup.Member add(final AbelianGroup.Member f) {
74                 if(f instanceof ComplexFunction)
75                         return add((ComplexFunction)f);
76                 else
77                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
78         }
79         public ComplexFunction add(ComplexFunction f) {
80                 return new Sum(this, f);
81         }
82         private static class Sum extends ComplexFunction {
83                 private final ComplexFunction f1, f2;
84                 public Sum(ComplexFunction f1, ComplexFunction f2) {
85                         this.f1 = f1;
86                         this.f2 = f2;
87                 }
88                 public Complex map(double x, double y) {
89                         return f1.map(x, y).add(f2.map(x, y));
90                 }
91                 public Complex map(Complex z) {
92                         return f1.map(z).add(f2.map(z));
93                 }
94                 public ComplexFunction differentiate() {
95                         return new Sum(f1.differentiate(), f2.differentiate());
96                 }
97         }
98
99         /**
100         * Returns the subtraction of this function and another.
101         */

102         public AbelianGroup.Member subtract(final AbelianGroup.Member f) {
103                 if(f instanceof ComplexFunction)
104                         return subtract((ComplexFunction)f);
105                 else
106                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
107         }
108         public ComplexFunction subtract(ComplexFunction f) {
109                 return new Difference(this, f);
110         }
111         private static class Difference extends ComplexFunction {
112                 private final ComplexFunction f1, f2;
113                 public Difference(ComplexFunction f1, ComplexFunction f2) {
114                         this.f1 = f1;
115                         this.f2 = f2;
116                 }
117                 public Complex map(double x, double y) {
118                         return f1.map(x, y).subtract(f2.map(x, y));
119                 }
120                 public Complex map(Complex z) {
121                         return f1.map(z).subtract(f2.map(z));
122                 }
123                 public ComplexFunction differentiate() {
124                         return new Difference(f1.differentiate(), f2.differentiate());
125                 }
126         }
127
128         /**
129         * Returns the multiplication of this function and another.
130         */

131         public Ring.Member multiply(Ring.Member f) {
132                 if(f instanceof ComplexFunction)
133                         return multiply((ComplexFunction)f);
134                 else
135                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
136         }
137         public ComplexFunction multiply(ComplexFunction f) {
138                 return new Product(this, f);
139         }
140         private static class Product extends ComplexFunction {
141                 private final ComplexFunction f1, f2;
142                 public Product(ComplexFunction f1, ComplexFunction f2) {
143                         this.f1 = f1;
144                         this.f2 = f2;
145                 }
146                 public Complex map(double x, double y) {
147                         return f1.map(x, y).multiply(f2.map(x, y));
148                 }
149                 public Complex map(Complex z) {
150                         return f1.map(z).multiply(f2.map(z));
151                 }
152                 /**
153                 * Product rule.
154                 */

155                 public ComplexFunction differentiate() {
156                         return new Sum(new Product(f1.differentiate(), f2), new Product(f1, f2.differentiate()));
157                 }
158         }
159
160         /**
161         * Returns the multiplicative inverse (reciprocal) of this function.
162         */

163         public Ring.Member inverse() {
164                 return new Reciprocal(this);
165         }
166         private static class Reciprocal extends ComplexFunction {
167                 private final ComplexFunction f;
168                 public Reciprocal(ComplexFunction f) {
169                         this.f = f;
170                 }
171                 public Complex map(double x, double y) {
172                         return Complex.ONE.divide(f.map(x, y));
173                 }
174                 public Complex map(Complex z) {
175                         return Complex.ONE.divide(f.map(z));
176                 }
177                 public ComplexFunction differentiate() {
178                         return new Quotient(new Negation(f.differentiate()), new Product(f, f));
179                 }
180         }
181
182         /**
183         * Returns the division of this function and another.
184         */

185         public Ring.Member divide(Ring.Member f) {
186                 if(f instanceof ComplexFunction)
187                         return divide((ComplexFunction)f);
188                 else
189                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
190         }
191         public ComplexFunction divide(ComplexFunction f) {
192                 return new Quotient(this, f);
193         }
194         private static class Quotient extends ComplexFunction {
195                 private final ComplexFunction f1, f2;
196                 public Quotient(ComplexFunction f1, ComplexFunction f2) {
197                         this.f1 = f1;
198                         this.f2 = f2;
199                 }
200                 public Complex map(double x, double y) {
201                         return f1.map(x, y).divide(f2.map(x, y));
202                 }
203                 public Complex map(Complex z) {
204                         return f1.map(z).divide(f2.map(z));
205                 }
206                 /**
207                 * Quotient rule.
208                 */

209                 public ComplexFunction differentiate() {
210                         return new Quotient(new Difference(new Product(f1.differentiate(), f2), new Product(f1, f2.differentiate())), new Product(f2, f2));
211                 }
212         }
213
214         /**
215         * Returns the differential of this function.
216         */

217         public abstract ComplexFunction differentiate();
218
219     public static ComplexFunction constant(Complex k) {
220         return new Constant(k);
221     }
222     private static final ComplexFunction ZERO = constant(Complex.ZERO);
223     private static class Constant extends ComplexFunction {
224         private final Complex A;
225         public Constant(Complex A) {
226             this.A = A;
227         }
228         public Complex map(double x, double y) {
229             return A;
230         }
231         public Complex map(Complex z) {
232             return A;
233         }
234         public ComplexFunction differentiate() {
235             return ZERO;
236         }
237     }
238 }
239
Popular Tags