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.Assertions;
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 UnivariateDerivative1}.
33   */
34  class UnivariateDerivative1Test extends UnivariateDerivativeAbstractTest<UnivariateDerivative1> {
35  
36      @Override
37      protected UnivariateDerivative1 build(final double x) {
38          return new UnivariateDerivative1(x, 1.0);
39      }
40  
41      @Override
42      protected int getMaxOrder() {
43          return 1;
44      }
45  
46      @Test
47      void testGetFirstDerivative() {
48          UnivariateDerivative1 ud1 = new UnivariateDerivative1(-0.5, 2.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      }
53  
54      @Test
55      void testConversion() {
56          UnivariateDerivative1 udA = new UnivariateDerivative1(-0.5, 2.5);
57          DerivativeStructure ds = udA.toDerivativeStructure();
58          assertEquals(1, ds.getFreeParameters());
59          assertEquals(1, 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          UnivariateDerivative1 udB = new UnivariateDerivative1(ds);
64          assertNotSame(udA, udB);
65          assertEquals(udA, udB);
66          try {
67              new UnivariateDerivative1(new DSFactory(2, 2).variable(0, 1.0));
68              fail("an exception should have been thrown");
69          } catch (MathIllegalArgumentException miae) {
70              assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
71          }
72          try {
73              new UnivariateDerivative1(new DSFactory(1, 2).variable(0, 1.0));
74              fail("an exception should have been thrown");
75          } catch (MathIllegalArgumentException miae) {
76              assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
77          }
78      }
79  
80      @Test
81      void testDoublePow() {
82          assertSame(build(3).getField().getZero(), UnivariateDerivative1.pow(0.0, build(1.5)));
83          UnivariateDerivative1 ud = UnivariateDerivative1.pow(2.0, build(1.5));
84          DSFactory factory = new DSFactory(1, 1);
85          DerivativeStructure ds = factory.constant(2.0).pow(factory.variable(0, 1.5));
86          assertEquals(ds.getValue(), ud.getValue(), 1.0e-15);
87          assertEquals(ds.getPartialDerivative(1), ud.getFirstDerivative(), 1.0e-15);
88      }
89  
90      @Test
91      void testTaylor() {
92          assertEquals(2.5, new UnivariateDerivative1(2, 1).taylor(0.5), 1.0e-15);
93      }
94  
95      @Test
96      void testHashcode() {
97          assertEquals(2108686789, new UnivariateDerivative1(2, 1).hashCode());
98      }
99  
100     @Test
101     void testEquals() {
102         UnivariateDerivative1 ud1 = new UnivariateDerivative1(12, -34);
103         assertEquals(ud1, ud1);
104         assertNotEquals("", ud1);
105         assertEquals(new UnivariateDerivative1(12, -34), ud1);
106         assertNotEquals(new UnivariateDerivative1(21, -34), ud1);
107         assertNotEquals(new UnivariateDerivative1(12, -43), ud1);
108         assertNotEquals(new UnivariateDerivative1(21, -43), ud1);
109     }
110 
111 
112     @Test
113     void testComparableFirstTerm() {
114         // GIVEN
115         final UnivariateDerivative1 ud1a = new UnivariateDerivative1(12, -34);
116         final UnivariateDerivative1 ud1b = new UnivariateDerivative1(2, 0);
117         // WHEN
118         final int actualComparison = ud1a.compareTo(ud1b);
119         // THEN
120         final int expectedComparison = 1;
121         assertEquals(expectedComparison, actualComparison);
122     }
123 
124     @Test
125     void testComparableSecondTerm() {
126         // GIVEN
127         final UnivariateDerivative1 ud1a = new UnivariateDerivative1(12, -34);
128         final UnivariateDerivative1 ud1b = new UnivariateDerivative1(12, 0);
129         // WHEN
130         final int actualComparison = ud1a.compareTo(ud1b);
131         // THEN
132         final int expectedComparison = -1;
133         assertEquals(expectedComparison, actualComparison);
134     }
135 
136     @Test
137     void testRunTimeClass() {
138         Field<UnivariateDerivative1> field = build(0.0).getField();
139         assertEquals(UnivariateDerivative1.class, field.getRuntimeClass());
140     }
141 
142     @Test
143     public void testIsSmall() {
144         Assertions.assertTrue(new UnivariateDerivative1(1.0e-13, 2.5e-13).
145                               isSmall(new UnivariateDerivative1(1.0, 1.0), 1.0e-12));
146         Assertions.assertFalse(new UnivariateDerivative1(1.0e-11, 2.5e-13).
147                                isSmall(new UnivariateDerivative1(1.0, 1.0), 1.0e-12));
148         Assertions.assertFalse(new UnivariateDerivative1(1.0e-13, 2.5e-11).
149                                isSmall(new UnivariateDerivative1(1.0, 1.0), 1.0e-12));
150     }
151 
152 }