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