View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.legacy.analysis;
19  
20  import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
21  import org.apache.commons.math4.legacy.analysis.differentiation.MultivariateDifferentiableFunction;
22  import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
23  import org.apache.commons.math4.legacy.analysis.function.Add;
24  import org.apache.commons.math4.legacy.analysis.function.Constant;
25  import org.apache.commons.math4.legacy.analysis.function.Cos;
26  import org.apache.commons.math4.legacy.analysis.function.Cosh;
27  import org.apache.commons.math4.legacy.analysis.function.Divide;
28  import org.apache.commons.math4.legacy.analysis.function.Identity;
29  import org.apache.commons.math4.legacy.analysis.function.Inverse;
30  import org.apache.commons.math4.legacy.analysis.function.Log;
31  import org.apache.commons.math4.legacy.analysis.function.Max;
32  import org.apache.commons.math4.legacy.analysis.function.Min;
33  import org.apache.commons.math4.legacy.analysis.function.Minus;
34  import org.apache.commons.math4.legacy.analysis.function.Multiply;
35  import org.apache.commons.math4.legacy.analysis.function.Pow;
36  import org.apache.commons.math4.legacy.analysis.function.Power;
37  import org.apache.commons.math4.legacy.analysis.function.Sin;
38  import org.apache.commons.math4.legacy.analysis.function.Sinc;
39  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
40  import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
41  import org.apache.commons.math4.core.jdkmath.JdkMath;
42  import org.junit.Assert;
43  import org.junit.Test;
44  
45  /**
46   * Test for {@link FunctionUtils}.
47   */
48  public class FunctionUtilsTest {
49      private final double EPS = JdkMath.ulp(1d);
50  
51      @Test
52      public void testCompose() {
53          UnivariateFunction id = new Identity();
54          Assert.assertEquals(3, FunctionUtils.compose(id, id, id).value(3), EPS);
55  
56          UnivariateFunction c = new Constant(4);
57          Assert.assertEquals(4, FunctionUtils.compose(id, c).value(3), EPS);
58          Assert.assertEquals(4, FunctionUtils.compose(c, id).value(3), EPS);
59  
60          UnivariateFunction m = new Minus();
61          Assert.assertEquals(-3, FunctionUtils.compose(m).value(3), EPS);
62          Assert.assertEquals(3, FunctionUtils.compose(m, m).value(3), EPS);
63  
64          UnivariateFunction inv = new Inverse();
65          Assert.assertEquals(-0.25, FunctionUtils.compose(inv, m, c, id).value(3), EPS);
66  
67          UnivariateFunction pow = new Power(2);
68          Assert.assertEquals(81, FunctionUtils.compose(pow, pow).value(3), EPS);
69      }
70  
71      @Test
72      public void testComposeDifferentiable() {
73          UnivariateDifferentiableFunction id = new Identity();
74          Assert.assertEquals(1, FunctionUtils.compose(id, id, id).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS);
75  
76          UnivariateDifferentiableFunction c = new Constant(4);
77          Assert.assertEquals(0, FunctionUtils.compose(id, c).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS);
78          Assert.assertEquals(0, FunctionUtils.compose(c, id).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS);
79  
80          UnivariateDifferentiableFunction m = new Minus();
81          Assert.assertEquals(-1, FunctionUtils.compose(m).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS);
82          Assert.assertEquals(1, FunctionUtils.compose(m, m).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS);
83  
84          UnivariateDifferentiableFunction inv = new Inverse();
85          Assert.assertEquals(0.25, FunctionUtils.compose(inv, m, id).value(new DerivativeStructure(1, 1, 0, 2)).getPartialDerivative(1), EPS);
86  
87          UnivariateDifferentiableFunction pow = new Power(2);
88          Assert.assertEquals(108, FunctionUtils.compose(pow, pow).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS);
89  
90          UnivariateDifferentiableFunction log = new Log();
91          double a = 9876.54321;
92          Assert.assertEquals(pow.value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1) / pow.value(a),
93                              FunctionUtils.compose(log, pow).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS);
94      }
95  
96      @Test
97      public void testAdd() {
98          UnivariateFunction id = new Identity();
99          UnivariateFunction c = new Constant(4);
100         UnivariateFunction m = new Minus();
101         UnivariateFunction inv = new Inverse();
102 
103         Assert.assertEquals(4.5, FunctionUtils.add(inv, m, c, id).value(2), EPS);
104         Assert.assertEquals(4 + 2, FunctionUtils.add(c, id).value(2), EPS);
105         Assert.assertEquals(4 - 2, FunctionUtils.add(c, FunctionUtils.compose(m, id)).value(2), EPS);
106     }
107 
108     @Test
109     public void testAddDifferentiable() {
110         UnivariateDifferentiableFunction sin = new Sin();
111         UnivariateDifferentiableFunction c = new Constant(4);
112         UnivariateDifferentiableFunction m = new Minus();
113         UnivariateDifferentiableFunction inv = new Inverse();
114 
115         final double a = 123.456;
116         Assert.assertEquals(- 1 / (a * a) -1 + JdkMath.cos(a),
117                             FunctionUtils.add(inv, m, c, sin).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1),
118                             EPS);
119     }
120 
121     @Test
122     public void testMultiply() {
123         UnivariateFunction c = new Constant(4);
124         Assert.assertEquals(16, FunctionUtils.multiply(c, c).value(12345), EPS);
125 
126         UnivariateFunction inv = new Inverse();
127         UnivariateFunction pow = new Power(2);
128         Assert.assertEquals(1, FunctionUtils.multiply(FunctionUtils.compose(inv, pow), pow).value(3.5), EPS);
129     }
130 
131     @Test
132     public void testMultiplyDifferentiable() {
133         UnivariateDifferentiableFunction c = new Constant(4);
134         UnivariateDifferentiableFunction id = new Identity();
135         final double a = 1.2345678;
136         Assert.assertEquals(8 * a, FunctionUtils.multiply(c, id, id).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS);
137 
138         UnivariateDifferentiableFunction inv = new Inverse();
139         UnivariateDifferentiableFunction pow = new Power(2.5);
140         UnivariateDifferentiableFunction cos = new Cos();
141         Assert.assertEquals(1.5 * JdkMath.sqrt(a) * JdkMath.cos(a) - JdkMath.pow(a, 1.5) * JdkMath.sin(a),
142                             FunctionUtils.multiply(inv, pow, cos).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS);
143 
144         UnivariateDifferentiableFunction cosh = new Cosh();
145         Assert.assertEquals(1.5 * JdkMath.sqrt(a) * JdkMath.cosh(a) + JdkMath.pow(a, 1.5) * JdkMath.sinh(a),
146                             FunctionUtils.multiply(inv, pow, cosh).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), 8 * EPS);
147     }
148 
149     @Test
150     public void testCombine() {
151         BivariateFunction bi = new Add();
152         UnivariateFunction id = new Identity();
153         UnivariateFunction m = new Minus();
154         UnivariateFunction c = FunctionUtils.combine(bi, id, m);
155         Assert.assertEquals(0, c.value(2.3456), EPS);
156 
157         bi = new Multiply();
158         UnivariateFunction inv = new Inverse();
159         c = FunctionUtils.combine(bi, id, inv);
160         Assert.assertEquals(1, c.value(2.3456), EPS);
161     }
162 
163     @Test
164     public void testCollector() {
165         BivariateFunction bi = new Add();
166         MultivariateFunction coll = FunctionUtils.collector(bi, 0);
167         Assert.assertEquals(10, coll.value(new double[] {1, 2, 3, 4}), EPS);
168 
169         bi = new Multiply();
170         coll = FunctionUtils.collector(bi, 1);
171         Assert.assertEquals(24, coll.value(new double[] {1, 2, 3, 4}), EPS);
172 
173         bi = new Max();
174         coll = FunctionUtils.collector(bi, Double.NEGATIVE_INFINITY);
175         Assert.assertEquals(10, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0);
176 
177         bi = new Min();
178         coll = FunctionUtils.collector(bi, Double.POSITIVE_INFINITY);
179         Assert.assertEquals(-24, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0);
180     }
181 
182     @Test
183     public void testSinc() {
184         BivariateFunction div = new Divide();
185         UnivariateFunction sin = new Sin();
186         UnivariateFunction id = new Identity();
187         UnivariateFunction sinc1 = FunctionUtils.combine(div, sin, id);
188         UnivariateFunction sinc2 = new Sinc();
189 
190         for (int i = 0; i < 10; i++) {
191             double x = JdkMath.random();
192             Assert.assertEquals(sinc1.value(x), sinc2.value(x), EPS);
193         }
194     }
195 
196     @Test
197     public void testFixingArguments() {
198         UnivariateFunction scaler = FunctionUtils.fix1stArgument(new Multiply(), 10);
199         Assert.assertEquals(1.23456, scaler.value(0.123456), EPS);
200 
201         UnivariateFunction pow1 = new Power(2);
202         UnivariateFunction pow2 = FunctionUtils.fix2ndArgument(new Pow(), 2);
203 
204         for (int i = 0; i < 10; i++) {
205             double x = JdkMath.random() * 10;
206             Assert.assertEquals(pow1.value(x), pow2.value(x), 0);
207         }
208     }
209 
210     @Test
211     public void testToDifferentiableUnivariate() {
212 
213         final UnivariateFunction f0 = new UnivariateFunction() {
214             @Override
215             public double value(final double x) {
216                 return x * x;
217             }
218         };
219         final UnivariateFunction f1 = new UnivariateFunction() {
220             @Override
221             public double value(final double x) {
222                 return 2 * x;
223             }
224         };
225         final UnivariateFunction f2 = new UnivariateFunction() {
226             @Override
227             public double value(final double x) {
228                 return 2;
229             }
230         };
231         final UnivariateDifferentiableFunction f = FunctionUtils.toDifferentiable(f0, f1, f2);
232 
233         for (double t = -1.0; t < 1; t += 0.01) {
234             // x = sin(t)
235             DerivativeStructure dsT = new DerivativeStructure(1, 2, 0, t);
236             DerivativeStructure y = f.value(dsT.sin());
237             Assert.assertEquals(JdkMath.sin(t) * JdkMath.sin(t),               f.value(JdkMath.sin(t)),  1.0e-15);
238             Assert.assertEquals(JdkMath.sin(t) * JdkMath.sin(t),               y.getValue(),              1.0e-15);
239             Assert.assertEquals(2 * JdkMath.cos(t) * JdkMath.sin(t),           y.getPartialDerivative(1), 1.0e-15);
240             Assert.assertEquals(2 * (1 - 2 * JdkMath.sin(t) * JdkMath.sin(t)), y.getPartialDerivative(2), 1.0e-15);
241         }
242 
243         try {
244             f.value(new DerivativeStructure(1, 3, 0.0));
245             Assert.fail("an exception should have been thrown");
246         } catch (NumberIsTooLargeException e) {
247             Assert.assertEquals(2, e.getMax());
248             Assert.assertEquals(3, e.getArgument());
249         }
250     }
251 
252     @Test
253     public void testToDifferentiableMultivariate() {
254 
255         final double a = 1.5;
256         final double b = 0.5;
257         final MultivariateFunction f = new MultivariateFunction() {
258             @Override
259             public double value(final double[] point) {
260                 return a * point[0] + b * point[1];
261             }
262         };
263         final MultivariateVectorFunction gradient = new MultivariateVectorFunction() {
264             @Override
265             public double[] value(final double[] point) {
266                 return new double[] { a, b };
267             }
268         };
269         final MultivariateDifferentiableFunction mdf = FunctionUtils.toDifferentiable(f, gradient);
270 
271         for (double t = -1.0; t < 1; t += 0.01) {
272             // x = sin(t), y = cos(t), hence the method really becomes univariate
273             DerivativeStructure dsT = new DerivativeStructure(1, 1, 0, t);
274             DerivativeStructure y = mdf.value(new DerivativeStructure[] { dsT.sin(), dsT.cos() });
275             Assert.assertEquals(a * JdkMath.sin(t) + b * JdkMath.cos(t), y.getValue(),              1.0e-15);
276             Assert.assertEquals(a * JdkMath.cos(t) - b * JdkMath.sin(t), y.getPartialDerivative(1), 1.0e-15);
277         }
278 
279         for (double u = -1.0; u < 1; u += 0.01) {
280             DerivativeStructure dsU = new DerivativeStructure(2, 1, 0, u);
281             for (double v = -1.0; v < 1; v += 0.01) {
282                 DerivativeStructure dsV = new DerivativeStructure(2, 1, 1, v);
283                 DerivativeStructure y = mdf.value(new DerivativeStructure[] { dsU, dsV });
284                 Assert.assertEquals(a * u + b * v, mdf.value(new double[] { u, v }), 1.0e-15);
285                 Assert.assertEquals(a * u + b * v, y.getValue(),                     1.0e-15);
286                 Assert.assertEquals(a,             y.getPartialDerivative(1, 0),     1.0e-15);
287                 Assert.assertEquals(b,             y.getPartialDerivative(0, 1),     1.0e-15);
288             }
289         }
290 
291         try {
292             mdf.value(new DerivativeStructure[] { new DerivativeStructure(1, 3, 0.0), new DerivativeStructure(1, 3, 0.0) });
293             Assert.fail("an exception should have been thrown");
294         } catch (NumberIsTooLargeException e) {
295             Assert.assertEquals(1, e.getMax());
296             Assert.assertEquals(3, e.getArgument());
297         }
298     }
299 
300     @Test
301     public void testToDifferentiableMultivariateInconsistentGradient() {
302 
303         final double a = 1.5;
304         final double b = 0.5;
305         final MultivariateFunction f = new MultivariateFunction() {
306             @Override
307             public double value(final double[] point) {
308                 return a * point[0] + b * point[1];
309             }
310         };
311         final MultivariateVectorFunction gradient = new MultivariateVectorFunction() {
312             @Override
313             public double[] value(final double[] point) {
314                 return new double[] { a, b, 0.0 };
315             }
316         };
317         final MultivariateDifferentiableFunction mdf = FunctionUtils.toDifferentiable(f, gradient);
318 
319         try {
320             DerivativeStructure dsT = new DerivativeStructure(1, 1, 0, 0.0);
321             mdf.value(new DerivativeStructure[] { dsT.sin(), dsT.cos() });
322             Assert.fail("an exception should have been thrown");
323         } catch (DimensionMismatchException e) {
324             Assert.assertEquals(2, e.getDimension());
325             Assert.assertEquals(3, e.getArgument());
326         }
327     }
328 
329     @Test
330     public void testDerivativeUnivariate() {
331 
332         final UnivariateDifferentiableFunction f = new UnivariateDifferentiableFunction() {
333 
334             @Override
335             public double value(double x) {
336                 return x * x;
337             }
338 
339             @Override
340             public DerivativeStructure value(DerivativeStructure x) {
341                 return x.multiply(x);
342             }
343         };
344 
345         final UnivariateFunction f0 = FunctionUtils.derivative(f, 0);
346         final UnivariateFunction f1 = FunctionUtils.derivative(f, 1);
347         final UnivariateFunction f2 = FunctionUtils.derivative(f, 2);
348 
349         for (double t = -1.0; t < 1; t += 0.01) {
350             Assert.assertEquals(t * t, f0.value(t), 1.0e-15);
351             Assert.assertEquals(2 * t, f1.value(t), 1.0e-15);
352             Assert.assertEquals(2,     f2.value(t), 1.0e-15);
353         }
354     }
355 
356     @Test
357     public void testDerivativeMultivariate() {
358 
359         final double a = 1.5;
360         final double b = 0.5;
361         final double c = 0.25;
362         final MultivariateDifferentiableFunction mdf = new MultivariateDifferentiableFunction() {
363 
364             @Override
365             public double value(double[] point) {
366                 return a * point[0] * point[0] + b * point[1] * point[1] + c * point[0] * point[1];
367             }
368 
369             @Override
370             public DerivativeStructure value(DerivativeStructure[] point) {
371                 DerivativeStructure x  = point[0];
372                 DerivativeStructure y  = point[1];
373                 DerivativeStructure x2 = x.multiply(x);
374                 DerivativeStructure y2 = y.multiply(y);
375                 DerivativeStructure xy = x.multiply(y);
376                 return x2.multiply(a).add(y2.multiply(b)).add(xy.multiply(c));
377             }
378         };
379 
380         final MultivariateFunction f       = FunctionUtils.derivative(mdf, new int[] { 0, 0 });
381         final MultivariateFunction dfdx    = FunctionUtils.derivative(mdf, new int[] { 1, 0 });
382         final MultivariateFunction dfdy    = FunctionUtils.derivative(mdf, new int[] { 0, 1 });
383         final MultivariateFunction d2fdx2  = FunctionUtils.derivative(mdf, new int[] { 2, 0 });
384         final MultivariateFunction d2fdy2  = FunctionUtils.derivative(mdf, new int[] { 0, 2 });
385         final MultivariateFunction d2fdxdy = FunctionUtils.derivative(mdf, new int[] { 1, 1 });
386 
387         for (double x = -1.0; x < 1; x += 0.01) {
388             for (double y = -1.0; y < 1; y += 0.01) {
389                 Assert.assertEquals(a * x * x + b * y * y + c * x * y, f.value(new double[]       { x, y }), 1.0e-15);
390                 Assert.assertEquals(2 * a * x + c * y,                 dfdx.value(new double[]    { x, y }), 1.0e-15);
391                 Assert.assertEquals(2 * b * y + c * x,                 dfdy.value(new double[]    { x, y }), 1.0e-15);
392                 Assert.assertEquals(2 * a,                             d2fdx2.value(new double[]  { x, y }), 1.0e-15);
393                 Assert.assertEquals(2 * b,                             d2fdy2.value(new double[]  { x, y }), 1.0e-15);
394                 Assert.assertEquals(c,                                 d2fdxdy.value(new double[] { x, y }), 1.0e-15);
395             }
396         }
397     }
398 }