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