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 13 public abstract class RealFunction implements Mapping, Ring.Member { 14 17 public final int dimension() { 18 return 1; 19 } 20 public Object getSet() { 21 throw new RuntimeException ("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 39 public RealFunction differentiate() { 40 return new Product(new Composition(f1.differentiate(), f2), f2.differentiate()); 41 } 42 } 43 44 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 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 ("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 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 ("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 118 public Ring.Member multiply(Ring.Member f) { 119 if(f instanceof RealFunction) 120 return multiply((RealFunction)f); 121 else 122 throw new IllegalArgumentException ("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 139 public RealFunction differentiate() { 140 return new Sum(new Product(f1.differentiate(), f2), new Product(f1, f2.differentiate())); 141 } 142 } 143 144 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 166 public Ring.Member divide(Ring.Member f) { 167 if(f instanceof RealFunction) 168 return divide((RealFunction)f); 169 else 170 throw new IllegalArgumentException ("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 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 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 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 |