1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37 final double x = 2;
38 final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
39
40
41 smoothstep.value(x);
42 });
43 }
44
45 @Test
46 void testExceptionOverBoundary() {
47 assertThrows(MathIllegalArgumentException.class, () -> {
48
49 final double x = 17;
50 final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
51
52
53 smoothstep.value(x);
54 });
55 }
56
57 @Test
58 void testEdgesConsistency() {
59 assertThrows(MathIllegalArgumentException.class, () -> {
60
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
67 smoothstep.value(leftEdge, rightEdge, x);
68 });
69 }
70
71 @Test
72 void testBoundaries() {
73
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
82 final double computedResult1 = clamp.value(leftEdge, rightEdge, x1);
83 final double computedResult2 = clamp.value(leftEdge, rightEdge, x2);
84
85
86 assertEquals(0, computedResult1, THRESHOLD);
87 assertEquals(1, computedResult2, THRESHOLD);
88 }
89
90 @Test
91 void testNormalizedInput() {
92
93
94 final double x = 0.4;
95 final SmoothStepFactory.SmoothStepFunction cubic = SmoothStepFactory.getCubic();
96
97
98 final double computedResult = cubic.value(x);
99
100
101 assertEquals(0.352, computedResult, THRESHOLD);
102
103 }
104
105 @Test
106 void testClampFunction() {
107
108
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
116 final double computedResult = clamp.value(leftEdge, rightEdge, x);
117
118
119 assertEquals(0.4, computedResult, THRESHOLD);
120
121 }
122
123 @Test
124 void testQuadraticFunction1() {
125
126
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
134 final double computedResult = quadratic.value(leftEdge, rightEdge, x);
135
136
137 assertEquals(0.32, computedResult, THRESHOLD);
138
139 }
140
141 @Test
142 void testQuadraticFunction2() {
143
144
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
152 final double computedResult = quadratic.value(leftEdge, rightEdge, x);
153
154
155 assertEquals(0.68, computedResult, THRESHOLD);
156
157 }
158
159 @Test
160 void testCubicFunction() {
161
162
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
170 final double computedResult = cubic.value(leftEdge, rightEdge, x);
171
172
173 assertEquals(0.352, computedResult, THRESHOLD);
174
175 }
176
177 @Test
178 void testQuinticFunction() {
179
180
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
188 final double computedResult = quintic.value(leftEdge, rightEdge, x);
189
190
191 assertEquals(0.31744, computedResult, THRESHOLD);
192
193 }
194
195 @Test
196 void testFieldEdgesConsistency() {
197 assertThrows(MathIllegalArgumentException.class, () -> {
198
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
208 smoothstep.value(leftEdge, rightEdge, x);
209 });
210 }
211
212 @Test
213 void testFieldBoundaries() {
214
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
225 final Binary64 computedResult1 = clamp.value(leftEdge, rightEdge, x1);
226 final Binary64 computedResult2 = clamp.value(leftEdge, rightEdge, x2);
227
228
229 assertEquals(0, computedResult1.getReal(), THRESHOLD);
230 assertEquals(1, computedResult2.getReal(), THRESHOLD);
231 }
232
233 @Test
234 void testFieldNormalizedInput() {
235
236
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
243 final Binary64 computedResult = cubic.value(x);
244
245
246 assertEquals(0.352, computedResult.getReal(), THRESHOLD);
247
248 }
249
250 @Test
251 void testFieldClampFunction() {
252
253
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
262 final Binary64 computedResult = clamp.value(leftEdge, rightEdge, x);
263
264
265 assertEquals(0.4, computedResult.getReal(), THRESHOLD);
266
267 }
268
269 @Test
270 void testFieldQuadraticFunction1() {
271
272
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
282 final Binary64 computedResult = quadratic.value(leftEdge, rightEdge, xField);
283 final Binary64 computedResult2 = quadratic.value((x - leftEdge) / (rightEdge - leftEdge));
284
285
286 assertEquals(0.32, computedResult.getReal(), THRESHOLD);
287 assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
288
289 }
290
291 @Test
292 void testFieldQuadraticFunction2() {
293
294
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
307 assertEquals(0.68, computedResult.getReal(), THRESHOLD);
308 assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
309
310 }
311
312 @Test
313 void testFieldCubicFunction() {
314
315
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
324 final Binary64 computedResult = cubic.value(leftEdge, rightEdge, x);
325
326
327 assertEquals(0.352, computedResult.getReal(), THRESHOLD);
328
329 }
330
331 @Test
332 void testFieldQuinticFunction() {
333
334
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
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
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
361 assertDoesNotThrow(() -> cubic.value(0.0));
362 assertDoesNotThrow(() -> cubic.value(0.5));
363 assertDoesNotThrow(() -> cubic.value(1.0));
364
365
366 assertDoesNotThrow(() -> cubic.value(0.0 - EPSILON / 2));
367 assertDoesNotThrow(() -> cubic.value(1.0 + EPSILON / 2));
368
369
370 assertDoesNotThrow(() -> cubic.value(0.0 - EPSILON));
371 assertDoesNotThrow(() -> cubic.value(1.0 + EPSILON));
372
373
374 assertThrows(MathIllegalArgumentException.class, () -> cubic.value(0.0 - EPSILON * 2));
375 assertThrows(MathIllegalArgumentException.class, () -> cubic.value(1.0 + EPSILON * 2));
376 }
377 }