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