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  package org.apache.commons.math3.analysis.function;
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  
24  import org.junit.Test;
25  import org.junit.Assert;
26  
27  public class SqrtTest {
28     @Test
29     public void testComparison() {
30         final Sqrt s = new Sqrt();
31         final UnivariateFunction f = new UnivariateFunction() {
32                 public double value(double x) {
33                     return Math.sqrt(x);
34                 }
35             };
36  
37         for (double x = 1e-30; x < 1e10; x *= 2) {
38             final double fX = f.value(x);
39             final double sX = s.value(x);
40             Assert.assertEquals("x=" + x, fX, sX, 0);
41         }
42     }
43  
44     @Test
45     public void testDerivativeComparison() {
46         final UnivariateDifferentiableFunction sPrime = new Sqrt();
47         final UnivariateFunction f = new UnivariateFunction() {
48                 public double value(double x) {
49                     return 1 / (2 * Math.sqrt(x));
50                 }
51             };
52  
53         for (double x = 1e-30; x < 1e10; x *= 2) {
54             final double fX = f.value(x);
55             final double sX = sPrime.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1);
56             Assert.assertEquals("x=" + x, fX, sX, FastMath.ulp(fX));
57         }
58     }
59  
60     @Test
61     public void testDerivativesHighOrder() {
62         DerivativeStructure s = new Sqrt().value(new DerivativeStructure(1, 5, 0, 1.2));
63         Assert.assertEquals(1.0954451150103322269, s.getPartialDerivative(0), 1.0e-16);
64         Assert.assertEquals(0.45643546458763842789, s.getPartialDerivative(1), 1.0e-16);
65         Assert.assertEquals(-0.1901814435781826783,  s.getPartialDerivative(2), 1.0e-16);
66         Assert.assertEquals(0.23772680447272834785,  s.getPartialDerivative(3), 1.0e-16);
67         Assert.assertEquals(-0.49526417598485072465,   s.getPartialDerivative(4), 1.0e-16);
68         Assert.assertEquals(1.4445205132891479465,  s.getPartialDerivative(5), 5.0e-16);
69     }
70  
71  }