KickJava   Java API By Example, From Geeks To Geeks.

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


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

12 public abstract class RealFunction2D implements Ring.Member {
13         public abstract double map(double x, double y);
14     /**
15     * Returns the dimension of the space this function is on.
16     */

17     public final int dimension() {
18         return 2;
19     }
20
21     public Object JavaDoc getSet() {
22         throw new RuntimeException JavaDoc("Not implemented: file bug");
23     }
24
25         /**
26         * Returns the negative of this matrix.
27         */

28         public AbelianGroup.Member negate() {
29                 return new Negation(this);
30         }
31         private static class Negation extends RealFunction2D {
32                 private final RealFunction2D f;
33                 public Negation(RealFunction2D f) {
34                         this.f = f;
35                 }
36                 public double map(double x, double y) {
37                         return -f.map(x, y);
38                 }
39         }
40
41         /**
42         * Returns the addition of this function and another.
43         */

44         public AbelianGroup.Member add(final AbelianGroup.Member f) {
45                 if(f instanceof RealFunction2D)
46                         return add((RealFunction2D)f);
47                 else
48                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
49         }
50         public RealFunction2D add(RealFunction2D f) {
51                 return new Sum(this, f);
52         }
53         private static class Sum extends RealFunction2D {
54                 private final RealFunction2D f1, f2;
55                 public Sum(RealFunction2D f1, RealFunction2D f2) {
56                         this.f1 = f1;
57                         this.f2 = f2;
58                 }
59                 public double map(double x, double y) {
60                         return f1.map(x, y)+f2.map(x, y);
61                 }
62         }
63
64         /**
65         * Returns the subtraction of this function and another.
66         */

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

90         public Ring.Member multiply(Ring.Member f) {
91                 if(f instanceof RealFunction2D)
92                         return multiply((RealFunction2D)f);
93                 else
94                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
95         }
96         public RealFunction2D multiply(RealFunction2D f) {
97                 return new Product(this, f);
98         }
99         private static class Product extends RealFunction2D {
100                 private final RealFunction2D f1, f2;
101                 public Product(RealFunction2D f1, RealFunction2D f2) {
102                         this.f1 = f1;
103                         this.f2 = f2;
104                 }
105                 public double map(double x, double y) {
106                         return f1.map(x, y)*f2.map(x, y);
107                 }
108         }
109
110         /**
111         * Returns the multiplicative inverse (reciprocal) of this function.
112         */

113         public Ring.Member inverse() {
114                 return new Reciprocal(this);
115         }
116         private static class Reciprocal extends RealFunction2D {
117                 private final RealFunction2D f;
118                 public Reciprocal(RealFunction2D f) {
119                         this.f = f;
120                 }
121                 public double map(double x, double y) {
122                         return 1.0/f.map(x, y);
123                 }
124         }
125
126         /**
127         * Returns the division of this function and another.
128         */

129         public Ring.Member divide(Ring.Member f) {
130                 if(f instanceof RealFunction2D)
131                         return divide((RealFunction2D)f);
132                 else
133                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
134         }
135         public RealFunction2D divide(RealFunction2D f) {
136                 return new Quotient(this, f);
137         }
138         private static class Quotient extends RealFunction2D {
139                 private final RealFunction2D f1, f2;
140                 public Quotient(RealFunction2D f1, RealFunction2D f2) {
141                         this.f1 = f1;
142                         this.f2 = f2;
143                 }
144                 public double map(double x, double y) {
145                         return f1.map(x, y)/f2.map(x, y);
146                 }
147         }
148
149         public RealFunctionND tensor(RealFunction f) {
150                 return new TensorProduct3D(this, f);
151         }
152         private static class TensorProduct3D extends RealFunctionND {
153                 private final RealFunction2D f1;
154         private final RealFunction f2;
155                 public TensorProduct3D(RealFunction2D f1, RealFunction f2) {
156             super(3);
157                         this.f1 = f1;
158                         this.f2 = f2;
159                 }
160                 public double map(double[] x) {
161                         return f1.map(x[0], x[1])*f2.map(x[2]);
162                 }
163         }
164         public RealFunctionND tensor(RealFunction2D f) {
165                 return new TensorProduct4D(this, f);
166         }
167         private static class TensorProduct4D extends RealFunctionND {
168                 private final RealFunction2D f1;
169         private final RealFunction2D f2;
170                 public TensorProduct4D(RealFunction2D f1, RealFunction2D f2) {
171             super(4);
172                         this.f1 = f1;
173                         this.f2 = f2;
174                 }
175                 public double map(double[] x) {
176                         return f1.map(x[0], x[1])*f2.map(x[2], x[3]);
177                 }
178         }
179
180     public static RealFunction2D constant(double k) {
181         return new Constant(k);
182     }
183     private static class Constant extends RealFunction2D {
184         private final double A;
185         public Constant(double A) {
186             this.A = A;
187         }
188         public double map(double x, double y) {
189             return A;
190         }
191     }
192 }
193
Popular Tags