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