View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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    *      https://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.hipparchus.analysis.differentiation;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.exception.LocalizedCoreFormats;
21  import org.hipparchus.exception.MathIllegalArgumentException;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Test for class {@link UnivariateDerivative1}.
27   */
28  public class UnivariateDerivative1Test extends UnivariateDerivativeAbstractTest<UnivariateDerivative1> {
29  
30      @Override
31      protected UnivariateDerivative1 build(final double x) {
32          return new UnivariateDerivative1(x, 1.0);
33      }
34  
35      @Override
36      protected int getMaxOrder() {
37          return 1;
38      }
39  
40      @Test
41      public void testGetFirstDerivative() {
42          UnivariateDerivative1 ud1 = new UnivariateDerivative1(-0.5, 2.5);
43          Assert.assertEquals(-0.5, ud1.getReal(), 1.0e-15);
44          Assert.assertEquals(-0.5, ud1.getValue(), 1.0e-15);
45          Assert.assertEquals(+2.5, ud1.getFirstDerivative(), 1.0e-15);
46      }
47  
48      @Test
49      public void testConversion() {
50          UnivariateDerivative1 udA = new UnivariateDerivative1(-0.5, 2.5);
51          DerivativeStructure ds = udA.toDerivativeStructure();
52          Assert.assertEquals(1, ds.getFreeParameters());
53          Assert.assertEquals(1, ds.getOrder());
54          Assert.assertEquals(-0.5, ds.getValue(), 1.0e-15);
55          Assert.assertEquals(-0.5, ds.getPartialDerivative(0), 1.0e-15);
56          Assert.assertEquals( 2.5, ds.getPartialDerivative(1), 1.0e-15);
57          UnivariateDerivative1 udB = new UnivariateDerivative1(ds);
58          Assert.assertNotSame(udA, udB);
59          Assert.assertEquals(udA, udB);
60          try {
61              new UnivariateDerivative1(new DSFactory(2, 2).variable(0, 1.0));
62              Assert.fail("an exception should have been thrown");
63          } catch (MathIllegalArgumentException miae) {
64              Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
65          }
66          try {
67              new UnivariateDerivative1(new DSFactory(1, 2).variable(0, 1.0));
68              Assert.fail("an exception should have been thrown");
69          } catch (MathIllegalArgumentException miae) {
70              Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
71          }
72      }
73  
74      @Test
75      public void testDoublePow() {
76          Assert.assertSame(build(3).getField().getZero(), UnivariateDerivative1.pow(0.0, build(1.5)));
77          UnivariateDerivative1 ud = UnivariateDerivative1.pow(2.0, build(1.5));
78          DSFactory factory = new DSFactory(1, 1);
79          DerivativeStructure ds = factory.constant(2.0).pow(factory.variable(0, 1.5));
80          Assert.assertEquals(ds.getValue(), ud.getValue(), 1.0e-15);
81          Assert.assertEquals(ds.getPartialDerivative(1), ud.getFirstDerivative(), 1.0e-15);
82      }
83  
84      @Test
85      public void testTaylor() {
86          Assert.assertEquals(2.5, new UnivariateDerivative1(2, 1).taylor(0.5), 1.0e-15);
87      }
88  
89      @Test
90      public void testHashcode() {
91          Assert.assertEquals(2108686789, new UnivariateDerivative1(2, 1).hashCode());
92      }
93  
94      @Test
95      public void testEquals() {
96          UnivariateDerivative1 ud1 = new UnivariateDerivative1(12, -34);
97          Assert.assertEquals(ud1, ud1);
98          Assert.assertNotEquals(ud1, "");
99          Assert.assertEquals(ud1, new UnivariateDerivative1(12, -34));
100         Assert.assertNotEquals(ud1, new UnivariateDerivative1(21, -34));
101         Assert.assertNotEquals(ud1, new UnivariateDerivative1(12, -43));
102         Assert.assertNotEquals(ud1, new UnivariateDerivative1(21, -43));
103     }
104 
105 
106     @Test
107     public void testComparableFirstTerm() {
108         // GIVEN
109         final UnivariateDerivative1 ud1a = new UnivariateDerivative1(12, -34);
110         final UnivariateDerivative1 ud1b = new UnivariateDerivative1(2, 0);
111         // WHEN
112         final int actualComparison = ud1a.compareTo(ud1b);
113         // THEN
114         final int expectedComparison = 1;
115         Assert.assertEquals(expectedComparison, actualComparison);
116     }
117 
118     @Test
119     public void testComparableSecondTerm() {
120         // GIVEN
121         final UnivariateDerivative1 ud1a = new UnivariateDerivative1(12, -34);
122         final UnivariateDerivative1 ud1b = new UnivariateDerivative1(12, 0);
123         // WHEN
124         final int actualComparison = ud1a.compareTo(ud1b);
125         // THEN
126         final int expectedComparison = -1;
127         Assert.assertEquals(expectedComparison, actualComparison);
128     }
129 
130     @Test
131     public void testRunTimeClass() {
132         Field<UnivariateDerivative1> field = build(0.0).getField();
133         Assert.assertEquals(UnivariateDerivative1.class, field.getRuntimeClass());
134     }
135 
136 }