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  package org.apache.commons.nabla.forward;
18  
19  import org.apache.commons.math3.analysis.UnivariateFunction;
20  import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
21  import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
22  import org.apache.commons.math3.util.FastMath;
23  import org.apache.commons.nabla.AbstractMathTest;
24  import org.apache.commons.nabla.DifferentiationException;
25  import org.apache.commons.nabla.forward.ForwardModeDifferentiator;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  public class AcosGeneratorTest extends AbstractMathTest {
30  
31      @Test
32      public void testReference(){
33          checkReference(new ReferenceFunction() {
34              public double value(double t) { return FastMath.acos(t); }
35              public double firstDerivative(double t) { return -1 / FastMath.sqrt(1 - t * t); }
36          }, -0.99, 0.99, 30, 9.0e-16);
37      }
38  
39      @Test
40      public void testSingularityPlus() throws DifferentiationException {
41          UnivariateDifferentiableFunction derivative =
42              new ForwardModeDifferentiator().differentiate(new UnivariateFunction() {
43                  public double value(double t) { return FastMath.acos(t); }
44              });
45  
46          double dPlus = derivative.value(new DerivativeStructure(1, 1, 1.0, 1.0)).getPartialDerivative(1);
47          Assert.assertTrue(Double.isInfinite(dPlus));
48          Assert.assertTrue(dPlus < 0);
49  
50          double dMinus = derivative.value(new DerivativeStructure(1, 1, 1.0, -1.0)).getPartialDerivative(1);
51          Assert.assertTrue(Double.isInfinite(dMinus));
52          Assert.assertTrue(dMinus > 0);
53  
54      }
55  
56      @Test
57      public void testSingularityMinus() throws DifferentiationException {
58          UnivariateDifferentiableFunction derivative =
59              new ForwardModeDifferentiator().differentiate(new UnivariateFunction() {
60                  public double value(double t) { return FastMath.acos(t); }
61              });
62  
63          double dPlus = derivative.value(new DerivativeStructure(1, 1, -1.0, 1.0)).getPartialDerivative(1);
64          Assert.assertTrue(Double.isInfinite(dPlus));
65          Assert.assertTrue(dPlus < 0);
66  
67          double dMinus = derivative.value(new DerivativeStructure(1, 1, -1.0, -1.0)).getPartialDerivative(1);
68          Assert.assertTrue(Double.isInfinite(dMinus));
69          Assert.assertTrue(dMinus > 0);
70  
71      }
72  
73  }