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.polynomials;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.hipparchus.util.Binary64;
22  import org.hipparchus.util.Binary64Field;
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.assertThrows;
27  
28  class SmoothStepFactoryTest {
29  
30      final double THRESHOLD = 1e-15;
31  
32      @Test
33      void testExceptionBelowBoundary() {
34          assertThrows(MathIllegalArgumentException.class, () -> {
35              // Given
36              final double                               x = 2;
37              final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
38  
39              // When
40              smoothstep.value(x);
41          });
42      }
43  
44      @Test
45      void testExceptionOverBoundary() {
46          assertThrows(MathIllegalArgumentException.class, () -> {
47              // Given
48              final double                               x = 17;
49              final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
50  
51              // When
52              smoothstep.value(x);
53          });
54      }
55  
56      @Test
57      void testEdgesConsistency() {
58          assertThrows(MathIllegalArgumentException.class, () -> {
59              // Given
60              final double                               leftEdge = 5;
61              final double                               rightEdge = 2;
62              final double                               x = 3;
63              final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
64  
65              // When
66              smoothstep.value(leftEdge, rightEdge, x);
67          });
68      }
69  
70      @Test
71      void testBoundaries() {
72          // Given
73          final double leftEdge  = 5;
74          final double rightEdge = 10;
75          final double x1        = 2;
76          final double x2        = 11;
77  
78          final SmoothStepFactory.SmoothStepFunction clamp = SmoothStepFactory.getClamp();
79  
80          // When
81          final double computedResult1 = clamp.value(leftEdge, rightEdge, x1);
82          final double computedResult2 = clamp.value(leftEdge, rightEdge, x2);
83  
84          // Then
85          assertEquals(0, computedResult1, THRESHOLD);
86          assertEquals(1, computedResult2, THRESHOLD);
87      }
88  
89      @Test
90      void testNormalizedInput() {
91  
92          // Given
93          final double                               x     = 0.4;
94          final SmoothStepFactory.SmoothStepFunction cubic = SmoothStepFactory.getCubic();
95  
96          // When
97          final double computedResult = cubic.value(x);
98  
99          // Then
100         assertEquals(0.352, computedResult, THRESHOLD);
101 
102     }
103 
104     @Test
105     void testClampFunction() {
106 
107         // Given
108         final double leftEdge  = 5;
109         final double rightEdge = 10;
110         final double x         = 7;
111 
112         final SmoothStepFactory.SmoothStepFunction clamp = SmoothStepFactory.getClamp();
113 
114         // When
115         final double computedResult = clamp.value(leftEdge, rightEdge, x);
116 
117         // Then
118         assertEquals(0.4, computedResult, THRESHOLD);
119 
120     }
121 
122     @Test
123     void testQuadraticFunction1() {
124 
125         // Given
126         final double leftEdge  = 5;
127         final double rightEdge = 10;
128         final double x         = 7;
129 
130         final SmoothStepFactory.SmoothStepFunction quadratic = SmoothStepFactory.getQuadratic();
131 
132         // When
133         final double computedResult = quadratic.value(leftEdge, rightEdge, x);
134 
135         // Then
136         assertEquals(0.32, computedResult, THRESHOLD);
137 
138     }
139 
140     @Test
141     void testQuadraticFunction2() {
142 
143         // Given
144         final double leftEdge  = 5;
145         final double rightEdge = 10;
146         final double x         = 8;
147 
148         final SmoothStepFactory.SmoothStepFunction quadratic = SmoothStepFactory.getQuadratic();
149 
150         // When
151         final double computedResult = quadratic.value(leftEdge, rightEdge, x);
152 
153         // Then
154         assertEquals(0.68, computedResult, THRESHOLD);
155 
156     }
157 
158     @Test
159     void testCubicFunction() {
160 
161         // Given
162         final double leftEdge  = 5;
163         final double rightEdge = 10;
164         final double x         = 7;
165 
166         final SmoothStepFactory.SmoothStepFunction cubic = SmoothStepFactory.getCubic();
167 
168         // When
169         final double computedResult = cubic.value(leftEdge, rightEdge, x);
170 
171         // Then
172         assertEquals(0.352, computedResult, THRESHOLD);
173 
174     }
175 
176     @Test
177     void testQuinticFunction() {
178 
179         // Given
180         final double leftEdge  = 5;
181         final double rightEdge = 10;
182         final double x         = 7;
183 
184         final SmoothStepFactory.SmoothStepFunction quintic = SmoothStepFactory.getQuintic();
185 
186         // When
187         final double computedResult = quintic.value(leftEdge, rightEdge, x);
188 
189         // Then
190         assertEquals(0.31744, computedResult, THRESHOLD);
191 
192     }
193 
194     @Test
195     void testFieldEdgesConsistency() {
196         assertThrows(MathIllegalArgumentException.class, () -> {
197             // Given
198             final Field<Binary64> field = Binary64Field.getInstance();
199 
200             final double   leftEdge = 5;
201             final double   rightEdge = 2;
202             final Binary64 x = new Binary64(3);
203             final SmoothStepFactory.FieldSmoothStepFunction<Binary64> smoothstep =
204                 SmoothStepFactory.getFieldGeneralOrder(field, 1);
205 
206             // When
207             smoothstep.value(leftEdge, rightEdge, x);
208         });
209     }
210 
211     @Test
212     void testFieldBoundaries() {
213         // Given
214         final Field<Binary64> field = Binary64Field.getInstance();
215 
216         final double   leftEdge  = 5;
217         final double   rightEdge = 10;
218         final Binary64 x1        = new Binary64(2);
219         final Binary64 x2        = new Binary64(11);
220 
221         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> clamp = SmoothStepFactory.getClamp(field);
222 
223         // When
224         final Binary64 computedResult1 = clamp.value(leftEdge, rightEdge, x1);
225         final Binary64 computedResult2 = clamp.value(leftEdge, rightEdge, x2);
226 
227         // Then
228         assertEquals(0, computedResult1.getReal(), THRESHOLD);
229         assertEquals(1, computedResult2.getReal(), THRESHOLD);
230     }
231 
232     @Test
233     void testFieldNormalizedInput() {
234 
235         // Given
236         final Field<Binary64> field = Binary64Field.getInstance();
237 
238         final double                                              x     = 0.4;
239         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> cubic = SmoothStepFactory.getCubic(field);
240 
241         // When
242         final Binary64 computedResult = cubic.value(x);
243 
244         // Then
245         assertEquals(0.352, computedResult.getReal(), THRESHOLD);
246 
247     }
248 
249     @Test
250     void testFieldClampFunction() {
251 
252         // Given
253         final Field<Binary64> field     = Binary64Field.getInstance();
254         final double          leftEdge  = 5;
255         final double          rightEdge = 10;
256         final Binary64        x         = new Binary64(7);
257 
258         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> clamp = SmoothStepFactory.getClamp(field);
259 
260         // When
261         final Binary64 computedResult = clamp.value(leftEdge, rightEdge, x);
262 
263         // Then
264         assertEquals(0.4, computedResult.getReal(), THRESHOLD);
265 
266     }
267 
268     @Test
269     void testFieldQuadraticFunction1() {
270 
271         // Given
272         final Field<Binary64> field     = Binary64Field.getInstance();
273         final double          leftEdge  = 5;
274         final double          rightEdge = 10;
275         final double          x         = 7;
276         final Binary64        xField    = new Binary64(x);
277 
278         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> quadratic = SmoothStepFactory.getQuadratic(field);
279 
280         // When
281         final Binary64 computedResult  = quadratic.value(leftEdge, rightEdge, xField);
282         final Binary64 computedResult2 = quadratic.value((x - leftEdge) / (rightEdge - leftEdge));
283 
284         // Then
285         assertEquals(0.32, computedResult.getReal(), THRESHOLD);
286         assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
287 
288     }
289 
290     @Test
291     void testFieldQuadraticFunction2() {
292 
293         // Given
294         final Field<Binary64> field     = Binary64Field.getInstance();
295         final double          leftEdge  = 5;
296         final double          rightEdge = 10;
297         final double          x         = 8;
298         final Binary64        xField    = new Binary64(x);
299 
300         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> quadratic = SmoothStepFactory.getQuadratic(field);
301 
302         final Binary64 computedResult  = quadratic.value(leftEdge, rightEdge, xField);
303         final Binary64 computedResult2 = quadratic.value((x - leftEdge) / (rightEdge - leftEdge));
304 
305         // Then
306         assertEquals(0.68, computedResult.getReal(), THRESHOLD);
307         assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
308 
309     }
310 
311     @Test
312     void testFieldCubicFunction() {
313 
314         // Given
315         final Field<Binary64> field     = Binary64Field.getInstance();
316         final double          leftEdge  = 5;
317         final double          rightEdge = 10;
318         final Binary64        x         = new Binary64(7);
319 
320         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> cubic = SmoothStepFactory.getCubic(field);
321 
322         // When
323         final Binary64 computedResult = cubic.value(leftEdge, rightEdge, x);
324 
325         // Then
326         assertEquals(0.352, computedResult.getReal(), THRESHOLD);
327 
328     }
329 
330     @Test
331     void testFieldQuinticFunction() {
332 
333         // Given
334         final Field<Binary64> field       = Binary64Field.getInstance();
335         final double          leftEdge    = 5;
336         final double          rightEdge   = 10;
337         final double          x           = 7;
338         final double          xNormalized = (x - leftEdge) / (rightEdge - leftEdge);
339         final Binary64        xField      = new Binary64(x);
340 
341         final SmoothStepFactory.FieldSmoothStepFunction<Binary64> quintic = SmoothStepFactory.getQuintic(field);
342 
343         // When
344         final Binary64 computedResult  = quintic.value(leftEdge, rightEdge, xField);
345         final Binary64 computedResult2 = quintic.value(xNormalized);
346         final Binary64 computedResult3 = quintic.value(new Binary64(xNormalized));
347 
348         // Then
349         assertEquals(0.31744, computedResult.getReal(), THRESHOLD);
350         assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
351         assertEquals(computedResult2.getReal(), computedResult3.getReal(), THRESHOLD);
352     }
353 
354 }