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