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.function;
19  
20  import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
21  import org.apache.commons.math4.core.jdkmath.JdkMath;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Test for all classes in org.apache.commons.math4.legacy.analysis.function that implement UnivariateDifferentiableFunction explicitly.
27   */
28  public class UnivariateDifferentiableFunctionTest {
29  
30      private static final double EPS = Math.ulp(1d);
31  
32      @Test
33      public void testAcos() {
34          Acos acos = new Acos();
35          Assert.assertEquals(JdkMath.PI/3, acos.value(0.5), EPS);
36          Assert.assertEquals(JdkMath.PI/4, acos.value(Double.valueOf(1/JdkMath.sqrt(2))), EPS);
37          double a = 0.5;
38          Assert.assertEquals(-1/JdkMath.sqrt(1-JdkMath.pow(a,2)), acos.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
39      }
40  
41      @Test
42      public void testAcosh() {
43          Acosh acosh = new Acosh();
44          Assert.assertEquals(0,acosh.value(1), EPS);
45          double a = 1.2345;
46          Assert.assertEquals(a,acosh.value(JdkMath.cosh(a)), EPS);
47          Assert.assertEquals(1/(JdkMath.sqrt(a-1)*JdkMath.sqrt(a+1)),acosh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
48      }
49  
50      @Test
51      public void testAsin() {
52          Asin asin = new Asin();
53          double a = 1.2345;
54          Assert.assertEquals(a, asin.value(JdkMath.sin(a)), EPS);
55          Assert.assertEquals(1/JdkMath.sqrt(1 - JdkMath.pow(a,2)), asin.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
56      }
57  
58      @Test
59      public void testAsinh() {
60          Asinh asinh = new Asinh();
61          double a = 1.2345;
62          Assert.assertEquals(a, asinh.value(JdkMath.sinh(a)), EPS);
63          Assert.assertEquals(1/JdkMath.sqrt(JdkMath.pow(a,2.0) + 1), asinh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
64      }
65  
66      @Test
67      public void testAtan() {
68          Atan atan = new Atan();
69          double a = 1.2345;
70          Assert.assertEquals(a, atan.value(JdkMath.tan(a)), EPS);
71          Assert.assertEquals(1/(JdkMath.pow(a,2.0) + 1), atan.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
72      }
73  
74      @Test
75      public void testAtanh() {
76          Atanh atanh = new Atanh();
77          double a = 1.2345;
78          Assert.assertEquals(a, atanh.value(JdkMath.tanh(a)), EPS);
79          Assert.assertEquals(1/(1 - JdkMath.pow(a,2.0)), atanh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
80      }
81  
82      @Test
83      public void testCbrt() {
84          Cbrt cbrt = new Cbrt();
85          double a = 1.2345;
86          Assert.assertEquals(a, cbrt.value(JdkMath.pow(a,3)), EPS);
87          Assert.assertEquals(1.0/(3.0*JdkMath.pow(a, 2.0/3.0)), cbrt.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
88      }
89  
90      @Test
91      public void testConstant() {
92          double a = 123.456;
93          Constant constantNeg1 = new Constant(-1);
94          Constant constant0 = new Constant(0);
95          Constant constant5 = new Constant(5);
96          Assert.assertEquals(-1, constantNeg1.value(a), EPS);
97          Assert.assertEquals(0, constant0.value(a), EPS);
98          Assert.assertEquals(5, constant5.value(a), EPS);
99          Assert.assertEquals(0, constantNeg1.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
100         Assert.assertEquals(0, constant0.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
101         Assert.assertEquals(0, constant5.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
102     }
103 
104     @Test
105     public void testCos() {
106         Cos cos = new Cos();
107         double a = 0.987;
108         Assert.assertEquals(a, cos.value(JdkMath.acos(a)), EPS);
109         Assert.assertEquals(-JdkMath.sin(a), cos.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
110     }
111 
112     @Test
113     public void testCosh() {
114         Cosh cosh = new Cosh();
115         double a = 1.2345;
116         Assert.assertEquals(a, cosh.value(JdkMath.acosh(a)), EPS);
117         Assert.assertEquals(JdkMath.sinh(a), cosh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
118     }
119 
120     @Test
121     public void testExp() {
122         Exp exp= new Exp();
123         double a = 1.2345;
124         Assert.assertEquals(a, exp.value(JdkMath.log(a)), EPS);
125         Assert.assertEquals(exp.value(a), exp.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
126     }
127 
128     @Test
129     public void testExpm1() {
130         Expm1 expm1 = new Expm1();
131         double a = 1.2345;
132         Assert.assertEquals(a-1, expm1.value(JdkMath.log(a)), EPS);
133         Assert.assertEquals(JdkMath.exp(a), expm1.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
134     }
135 
136     @Test
137     public void testIdentity() {
138         Identity identity = new Identity();
139         double a = 123.456;
140         Assert.assertEquals(a, identity.value(a), EPS);
141         Assert.assertEquals(1, identity.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
142     }
143 
144     @Test
145     public void testInverse() {
146         Inverse inverse = new Inverse();
147         double a = 123.456;
148         Assert.assertEquals(1/a, inverse.value(a), EPS);
149         Assert.assertEquals(-1/JdkMath.pow(a,2), inverse.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
150     }
151 
152     @Test
153     public void testLog() {
154         double a = 123.456;
155         Log log = new Log();
156         Assert.assertEquals(Math.log(a), log.value(a), EPS);
157         Assert.assertEquals(1/a,log.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
158     }
159 
160     @Test
161     public void testLog10() {
162         Log10 log10 = new Log10();
163         double a =1.2345;
164         Assert.assertEquals(a, log10.value(JdkMath.pow(10, a)), EPS);
165         Assert.assertEquals(1/(a*JdkMath.log(10)), log10.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
166     }
167 
168     @Test
169     public void testLog1p() {
170         Log1p log1p = new Log1p();
171         double a = 1.2345;
172         Assert.assertEquals(a+1,JdkMath.exp(log1p.value(a)), EPS);
173         Assert.assertEquals(1/(1+a), log1p.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
174     }
175 
176     @Test
177     public void testMinus() {
178         Minus minus = new Minus();
179         double a = 123.456;
180         Assert.assertEquals(-a, minus.value(a), EPS);
181         Assert.assertEquals(-1, minus.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
182     }
183 
184     @Test
185     public void testPower() {
186         Power squared = new Power(2);
187         Power power2_5 = new Power(2.5);
188         double a = 123.456;
189         Assert.assertEquals(JdkMath.pow(a,2), squared.value(a), EPS);
190         Assert.assertEquals(JdkMath.pow(a, 2.5), power2_5.value(a), EPS);
191         Assert.assertEquals(2*a, squared.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
192         Assert.assertEquals(2.5*JdkMath.pow(a,1.5), power2_5.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
193     }
194 
195     @Test
196     public void testSin() {
197         Sin sin = new Sin();
198         double a = 0.987;
199         Assert.assertEquals(a, sin.value(JdkMath.asin(a)), EPS);
200         Assert.assertEquals(JdkMath.cos(a), sin.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
201     }
202 
203     @Test
204     public void testSinh() {
205         Sinh sinh = new Sinh();
206         double a = 1.2345;
207         Assert.assertEquals(a, sinh.value(JdkMath.asinh(a)), EPS);
208         Assert.assertEquals(JdkMath.cosh(a), sinh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
209     }
210 
211     @Test
212     public void testTan() {
213         Tan tan = new Tan();
214         double a = 0.987;
215         Assert.assertEquals(a, tan.value(JdkMath.atan(a)), EPS);
216         Assert.assertEquals(1/(JdkMath.pow(JdkMath.cos(a),2)), tan.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
217     }
218 
219     @Test
220     public void testTanh() {
221         Tanh tanh = new Tanh();
222         double a = 0.987;
223         Assert.assertEquals(a, tanh.value(JdkMath.atanh(a)), EPS);
224         Assert.assertEquals(1/JdkMath.pow(JdkMath.cosh(a),2), tanh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
225     }
226 }