KickJava   Java API By Example, From Geeks To Geeks.

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


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

13 public abstract class RealFunction implements Mapping, Ring.Member {
14     /**
15     * Returns the dimension of the space this function is on.
16     */

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

39                 public RealFunction differentiate() {
40                         return new Product(new Composition(f1.differentiate(), f2), f2.differentiate());
41                 }
42         }
43
44         /**
45         * Returns the negative of this function.
46         */

47         public AbelianGroup.Member negate() {
48                 return new Negation(this);
49         }
50         private static class Negation extends RealFunction {
51                 private final RealFunction f;
52                 public Negation(RealFunction f) {
53                         this.f = f;
54                 }
55                 public double map(double x) {
56                         return -f.map(x);
57                 }
58                 public RealFunction differentiate() {
59                         return new Negation(f.differentiate());
60                 }
61         }
62
63         /**
64         * Returns the addition of this function and another.
65         */

66         public AbelianGroup.Member add(final AbelianGroup.Member f) {
67                 if(f instanceof RealFunction)
68                         return add((RealFunction)f);
69                 else
70                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
71         }
72         public RealFunction add(RealFunction f) {
73                 return new Sum(this, f);
74         }
75         private static class Sum extends RealFunction {
76                 private final RealFunction f1, f2;
77                 public Sum(RealFunction f1, RealFunction f2) {
78                         this.f1 = f1;
79                         this.f2 = f2;
80                 }
81                 public double map(double x) {
82                         return f1.map(x)+f2.map(x);
83                 }
84                 public RealFunction differentiate() {
85                         return new Sum(f1.differentiate(), f2.differentiate());
86                 }
87         }
88
89         /**
90         * Returns the subtraction of this function and another.
91         */

92         public AbelianGroup.Member subtract(final AbelianGroup.Member f) {
93                 if(f instanceof RealFunction)
94                         return subtract((RealFunction)f);
95                 else
96                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
97         }
98         public RealFunction subtract(RealFunction f) {
99                 return new Difference(this, f);
100         }
101         private static class Difference extends RealFunction {
102                 private final RealFunction f1, f2;
103                 public Difference(RealFunction f1, RealFunction f2) {
104                         this.f1 = f1;
105                         this.f2 = f2;
106                 }
107                 public double map(double x) {
108                         return f1.map(x)-f2.map(x);
109                 }
110                 public RealFunction differentiate() {
111                         return new Difference(f1.differentiate(), f2.differentiate());
112                 }
113         }
114
115         /**
116         * Returns the multiplication of this function and another.
117         */

118         public Ring.Member multiply(Ring.Member f) {
119                 if(f instanceof RealFunction)
120                         return multiply((RealFunction)f);
121                 else
122                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
123         }
124         public RealFunction multiply(RealFunction f) {
125                 return new Product(this, f);
126         }
127         private static class Product extends RealFunction {
128                 private final RealFunction f1, f2;
129                 public Product(RealFunction f1, RealFunction f2) {
130                         this.f1 = f1;
131                         this.f2 = f2;
132                 }
133                 public double map(double x) {
134                         return f1.map(x)*f2.map(x);
135                 }
136                 /**
137                 * Product rule.
138                 */

139                 public RealFunction differentiate() {
140                         return new Sum(new Product(f1.differentiate(), f2), new Product(f1, f2.differentiate()));
141                 }
142         }
143
144         /**
145         * Returns the multiplicative inverse (reciprocal) of this function.
146         */

147         public Ring.Member inverse() {
148                 return new Reciprocal(this);
149         }
150         private static class Reciprocal extends RealFunction {
151                 private final RealFunction f;
152                 public Reciprocal(RealFunction f) {
153                         this.f = f;
154                 }
155                 public double map(double x) {
156                         return 1.0/f.map(x);
157                 }
158                 public RealFunction differentiate() {
159                         return new Quotient(new Negation(f.differentiate()), new Product(f, f));
160                 }
161         }
162
163         /**
164         * Returns the division of this function and another.
165         */

166         public Ring.Member divide(Ring.Member f) {
167                 if(f instanceof RealFunction)
168                         return divide((RealFunction)f);
169                 else
170                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
171         }
172         public RealFunction divide(RealFunction f) {
173                 return new Quotient(this, f);
174         }
175         private static class Quotient extends RealFunction {
176                 private final RealFunction f1, f2;
177                 public Quotient(RealFunction f1, RealFunction f2) {
178                         this.f1 = f1;
179                         this.f2 = f2;
180                 }
181                 public double map(double x) {
182                         return f1.map(x)/f2.map(x);
183                 }
184                 /**
185                 * Quotient rule.
186                 */

187                 public RealFunction differentiate() {
188                         return new Quotient(new Difference(new Product(f1.differentiate(), f2), new Product(f1, f2.differentiate())), new Product(f2, f2));
189                 }
190         }
191
192         public RealFunction2D tensor(RealFunction f) {
193                 return new TensorProduct2D(this, f);
194         }
195         private static class TensorProduct2D extends RealFunction2D {
196                 private final RealFunction f1, f2;
197                 public TensorProduct2D(RealFunction f1, RealFunction f2) {
198                         this.f1 = f1;
199                         this.f2 = f2;
200                 }
201                 public double map(double x, double y) {
202                         return f1.map(x)*f2.map(y);
203                 }
204         }
205
206         /**
207         * Returns the Taylor expansion of this function about a point.
208         * @param a the point at which to expand about.
209         * @param n the number of terms to expand to.
210         * @return the Taylor series of f(x+a).
211         */

212         public RealPolynomial taylorExpand(double a, int n) {
213                 double coeff[] = new double[n];
214                 coeff[0] = map(a);
215                 RealFunction diff = this;
216                 int factorial = 1;
217                 for(int i=1; i<n; i++) {
218                         diff = diff.differentiate();
219                         factorial *= i;
220                         coeff[i] = diff.map(a)/factorial;
221                 }
222                 return new RealPolynomial(coeff);
223         }
224         /**
225         * Returns the differential of this function.
226         */

227         public abstract RealFunction differentiate();
228
229     public static RealFunction constant(double k) {
230         return new Constant(k);
231     }
232     private static final RealFunction ZERO = constant(0.0);
233     private static class Constant extends RealFunction {
234         private final double A;
235         public Constant(double A) {
236             this.A = A;
237         }
238         public double map(double x) {
239             return A;
240         }
241         public RealFunction differentiate() {
242             return ZERO;
243         }
244     }
245 }
246
Popular Tags