KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.analysis;
2
3 import JSci.maths.Mapping;
4 import JSci.maths.DimensionException;
5 import JSci.maths.groups.AbelianGroup;
6 import JSci.maths.fields.Ring;
7
8 /**
9 * This class describes a function on an n-dimensional space.
10 * @version 1.0
11 * @author Mark Hale
12 */

13 public abstract class RealFunctionND implements Ring.Member {
14     protected final int dim;
15     protected RealFunctionND(int dim) {
16         this.dim = dim;
17     }
18         public abstract double map(double[] x);
19     /**
20     * Returns the dimension of the space this function is on.
21     */

22     public final int dimension() {
23         return dim;
24     }
25
26     public Object JavaDoc getSet() {
27         throw new RuntimeException JavaDoc("Not implemented: file bug");
28     }
29
30         /**
31         * Returns the negative of this matrix.
32         */

33         public AbelianGroup.Member negate() {
34                 return new Negation(this);
35         }
36         private static class Negation extends RealFunctionND {
37                 private final RealFunctionND f;
38                 public Negation(RealFunctionND f) {
39             super(f.dim);
40                         this.f = f;
41                 }
42                 public double map(double[] x) {
43                         return -f.map(x);
44                 }
45         }
46
47         /**
48         * Returns the addition of this function and another.
49         */

50         public AbelianGroup.Member add(final AbelianGroup.Member f) {
51                 if(f instanceof RealFunctionND)
52                         return add((RealFunctionND)f);
53                 else
54                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
55         }
56         public RealFunctionND add(RealFunctionND f) {
57         if(dim != f.dim)
58             throw new DimensionException("Functions have different dimensions.");
59                 return new Sum(this, f);
60         }
61         private static class Sum extends RealFunctionND {
62                 private final RealFunctionND f1, f2;
63                 public Sum(RealFunctionND f1, RealFunctionND f2) {
64             super(f1.dim);
65                         this.f1 = f1;
66                         this.f2 = f2;
67                 }
68                 public double map(double[] x) {
69                         return f1.map(x)+f2.map(x);
70                 }
71         }
72
73         /**
74         * Returns the subtraction of this function and another.
75         */

76         public AbelianGroup.Member subtract(final AbelianGroup.Member f) {
77                 if(f instanceof RealFunctionND)
78                         return subtract((RealFunctionND)f);
79                 else
80                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
81         }
82         public RealFunctionND subtract(RealFunctionND f) {
83         if(dim != f.dim)
84             throw new DimensionException("Functions have different dimensions.");
85                 return new Difference(this, f);
86         }
87         private static class Difference extends RealFunctionND {
88                 private final RealFunctionND f1, f2;
89                 public Difference(RealFunctionND f1, RealFunctionND f2) {
90             super(f1.dim);
91                         this.f1 = f1;
92                         this.f2 = f2;
93                 }
94                 public double map(double[] x) {
95                         return f1.map(x)-f2.map(x);
96                 }
97         }
98
99         /**
100         * Returns the multiplication of this function and another.
101         */

102         public Ring.Member multiply(Ring.Member f) {
103                 if(f instanceof RealFunctionND)
104                         return multiply((RealFunctionND)f);
105                 else
106                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
107         }
108         public RealFunctionND multiply(RealFunctionND f) {
109         if(dim != f.dim)
110             throw new DimensionException("Functions have different dimensions.");
111                 return new Product(this, f);
112         }
113         private static class Product extends RealFunctionND {
114                 private final RealFunctionND f1, f2;
115                 public Product(RealFunctionND f1, RealFunctionND f2) {
116             super(f1.dim);
117                         this.f1 = f1;
118                         this.f2 = f2;
119                 }
120                 public double map(double[] x) {
121                         return f1.map(x)*f2.map(x);
122                 }
123         }
124
125         /**
126         * Returns the multiplicative inverse (reciprocal) of this function.
127         */

128         public Ring.Member inverse() {
129                 return new Reciprocal(this);
130         }
131         private static class Reciprocal extends RealFunctionND {
132                 private final RealFunctionND f;
133                 public Reciprocal(RealFunctionND f) {
134             super(f.dim);
135                         this.f = f;
136                 }
137                 public double map(double[] x) {
138                         return 1.0/f.map(x);
139                 }
140         }
141
142         /**
143         * Returns the division of this function and another.
144         */

145         public Ring.Member divide(Ring.Member f) {
146                 if(f instanceof RealFunctionND)
147                         return divide((RealFunctionND)f);
148                 else
149                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
150         }
151         public RealFunctionND divide(RealFunctionND f) {
152         if(dim != f.dim)
153             throw new DimensionException("Functions have different dimensions.");
154                 return new Quotient(this, f);
155         }
156         private static class Quotient extends RealFunctionND {
157                 private final RealFunctionND f1, f2;
158                 public Quotient(RealFunctionND f1, RealFunctionND f2) {
159             super(f1.dim);
160                         this.f1 = f1;
161                         this.f2 = f2;
162                 }
163                 public double map(double[] x) {
164                         return f1.map(x)/f2.map(x);
165                 }
166         }
167
168         public RealFunctionND tensor(RealFunction f) {
169                 return new TensorProductN1D(this, f);
170         }
171         private static class TensorProductN1D extends RealFunctionND {
172                 private final RealFunctionND f1;
173         private final RealFunction f2;
174                 public TensorProductN1D(RealFunctionND f1, RealFunction f2) {
175             super(f1.dim+1);
176                         this.f1 = f1;
177                         this.f2 = f2;
178                 }
179                 public double map(double[] x) {
180             double[] x1 = new double[f1.dim];
181             System.arraycopy(x, 0, x1, 0, x1.length);
182             double x2 = x[x1.length];
183                         return f1.map(x1)*f2.map(x2);
184                 }
185         }
186         public RealFunctionND tensor(RealFunctionND f) {
187                 return new TensorProductND(this, f);
188         }
189         private static class TensorProductND extends RealFunctionND {
190                 private final RealFunctionND f1, f2;
191                 public TensorProductND(RealFunctionND f1, RealFunctionND f2) {
192             super(f1.dim+f2.dim);
193                         this.f1 = f1;
194                         this.f2 = f2;
195                 }
196                 public double map(double[] x) {
197             double[] x1 = new double[f1.dim];
198             double[] x2 = new double[f2.dim];
199             System.arraycopy(x, 0, x1, 0, x1.length);
200             System.arraycopy(x, x1.length, x2, 0, x2.length);
201                         return f1.map(x1)*f2.map(x2);
202                 }
203         }
204
205     public static RealFunctionND constant(int dim, double k) {
206         return new Constant(dim, k);
207     }
208     private static class Constant extends RealFunctionND {
209         private final double A;
210         public Constant(int dim, double A) {
211             super(dim);
212             this.A = A;
213         }
214         public double map(double[] x) {
215             return A;
216         }
217     }
218 }
219
Popular Tags