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.jupiter.api.Test;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  import static org.junit.jupiter.api.Assertions.assertNotEquals;
26  import static org.junit.jupiter.api.Assertions.assertNotSame;
27  import static org.junit.jupiter.api.Assertions.assertSame;
28  import static org.junit.jupiter.api.Assertions.fail;
29  
30  /**
31   * Test for class {@link UnivariateDerivative2}.
32   */
33  class UnivariateDerivative2Test extends UnivariateDerivativeAbstractTest<UnivariateDerivative2> {
34  
35      @Override
36      protected UnivariateDerivative2 build(final double x) {
37          return new UnivariateDerivative2(x, 1.0, 0.0);
38      }
39  
40      @Override
41      protected int getMaxOrder() {
42          return 2;
43      }
44  
45      @Test
46      void testGetFirstAndSecondDerivative() {
47          UnivariateDerivative2 ud1 = new UnivariateDerivative2(-0.5, 2.5, 4.5);
48          assertEquals(-0.5, ud1.getReal(), 1.0e-15);
49          assertEquals(-0.5, ud1.getValue(), 1.0e-15);
50          assertEquals(+2.5, ud1.getFirstDerivative(), 1.0e-15);
51          assertEquals(+4.5, ud1.getSecondDerivative(), 1.0e-15);
52      }
53  
54      @Test
55      void testConversion() {
56          UnivariateDerivative2 udA = new UnivariateDerivative2(-0.5, 2.5, 4.5);
57          DerivativeStructure ds = udA.toDerivativeStructure();
58          assertEquals(1, ds.getFreeParameters());
59          assertEquals(2, ds.getOrder());
60          assertEquals(-0.5, ds.getValue(), 1.0e-15);
61          assertEquals(-0.5, ds.getPartialDerivative(0), 1.0e-15);
62          assertEquals( 2.5, ds.getPartialDerivative(1), 1.0e-15);
63          assertEquals( 4.5, ds.getPartialDerivative(2), 1.0e-15);
64          UnivariateDerivative2 udB = new UnivariateDerivative2(ds);
65          assertNotSame(udA, udB);
66          assertEquals(udA, udB);
67          try {
68              new UnivariateDerivative2(new DSFactory(2, 2).variable(0, 1.0));
69              fail("an exception should have been thrown");
70          } catch (MathIllegalArgumentException miae) {
71              assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
72          }
73          try {
74              new UnivariateDerivative2(new DSFactory(1, 1).variable(0, 1.0));
75              fail("an exception should have been thrown");
76          } catch (MathIllegalArgumentException miae) {
77              assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
78          }
79      }
80  
81      @Test
82      void testDoublePow() {
83          assertSame(build(3).getField().getZero(), UnivariateDerivative2.pow(0.0, build(1.5)));
84          UnivariateDerivative2 ud = UnivariateDerivative2.pow(2.0, build(1.5));
85          DSFactory factory = new DSFactory(1, 2);
86          DerivativeStructure ds = factory.constant(2.0).pow(factory.variable(0, 1.5));
87          assertEquals(ds.getValue(), ud.getValue(), 1.0e-15);
88          assertEquals(ds.getPartialDerivative(1), ud.getFirstDerivative(), 1.0e-15);
89          assertEquals(ds.getPartialDerivative(2), ud.getSecondDerivative(), 1.0e-15);
90      }
91  
92      @Test
93      void testTaylor() {
94          assertEquals(-0.125, new UnivariateDerivative2(1, -3, 4).taylor(0.75), 1.0e-15);
95      }
96  
97      @Test
98      void testHashcode() {
99          assertEquals(-1025507011, new UnivariateDerivative2(2, 1, -1).hashCode());
100     }
101 
102     @Test
103     void testEquals() {
104         UnivariateDerivative2 ud2 = new UnivariateDerivative2(12, -34, 56);
105         assertEquals(ud2, ud2);
106         assertNotEquals("", ud2);
107         assertEquals(ud2, new UnivariateDerivative2(12, -34, 56));
108         assertNotEquals(ud2, new UnivariateDerivative2(21, -34, 56));
109         assertNotEquals(ud2, new UnivariateDerivative2(12, -43, 56));
110         assertNotEquals(ud2, new UnivariateDerivative2(12, -34, 65));
111         assertNotEquals(ud2, new UnivariateDerivative2(21, -43, 65));
112     }
113 
114     @Test
115     void testComparableFirstTerm() {
116         // GIVEN
117         final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
118         final UnivariateDerivative2 ud2b = new UnivariateDerivative2(2, 0, 25);
119         // WHEN
120         final int actualComparison = ud2a.compareTo(ud2b);
121         // THEN
122         final int expectedComparison = 1;
123         assertEquals(expectedComparison, actualComparison);
124     }
125 
126     @Test
127     void testComparableSecondTerm() {
128         // GIVEN
129         final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
130         final UnivariateDerivative2 ud2b = new UnivariateDerivative2(12, 0, 25);
131         // WHEN
132         final int actualComparison = ud2a.compareTo(ud2b);
133         // THEN
134         final int expectedComparison = -1;
135         assertEquals(expectedComparison, actualComparison);
136     }
137 
138     @Test
139     void testComparableThirdTerm() {
140         // GIVEN
141         final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
142         final UnivariateDerivative2 ud2b = new UnivariateDerivative2(12, -34, 25);
143         // WHEN
144         final int actualComparison = ud2a.compareTo(ud2b);
145         // THEN
146         final int expectedComparison = 0;
147         assertEquals(expectedComparison, actualComparison);
148     }
149 
150     @Test
151     void testRunTimeClass() {
152         Field<UnivariateDerivative2> field = build(0.0).getField();
153         assertEquals(UnivariateDerivative2.class, field.getRuntimeClass());
154     }
155 
156 }