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.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
36 final double x = 2;
37 final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
38
39
40 smoothstep.value(x);
41 });
42 }
43
44 @Test
45 void testExceptionOverBoundary() {
46 assertThrows(MathIllegalArgumentException.class, () -> {
47
48 final double x = 17;
49 final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
50
51
52 smoothstep.value(x);
53 });
54 }
55
56 @Test
57 void testEdgesConsistency() {
58 assertThrows(MathIllegalArgumentException.class, () -> {
59
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
66 smoothstep.value(leftEdge, rightEdge, x);
67 });
68 }
69
70 @Test
71 void testBoundaries() {
72
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
81 final double computedResult1 = clamp.value(leftEdge, rightEdge, x1);
82 final double computedResult2 = clamp.value(leftEdge, rightEdge, x2);
83
84
85 assertEquals(0, computedResult1, THRESHOLD);
86 assertEquals(1, computedResult2, THRESHOLD);
87 }
88
89 @Test
90 void testNormalizedInput() {
91
92
93 final double x = 0.4;
94 final SmoothStepFactory.SmoothStepFunction cubic = SmoothStepFactory.getCubic();
95
96
97 final double computedResult = cubic.value(x);
98
99
100 assertEquals(0.352, computedResult, THRESHOLD);
101
102 }
103
104 @Test
105 void testClampFunction() {
106
107
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
115 final double computedResult = clamp.value(leftEdge, rightEdge, x);
116
117
118 assertEquals(0.4, computedResult, THRESHOLD);
119
120 }
121
122 @Test
123 void testQuadraticFunction1() {
124
125
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
133 final double computedResult = quadratic.value(leftEdge, rightEdge, x);
134
135
136 assertEquals(0.32, computedResult, THRESHOLD);
137
138 }
139
140 @Test
141 void testQuadraticFunction2() {
142
143
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
151 final double computedResult = quadratic.value(leftEdge, rightEdge, x);
152
153
154 assertEquals(0.68, computedResult, THRESHOLD);
155
156 }
157
158 @Test
159 void testCubicFunction() {
160
161
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
169 final double computedResult = cubic.value(leftEdge, rightEdge, x);
170
171
172 assertEquals(0.352, computedResult, THRESHOLD);
173
174 }
175
176 @Test
177 void testQuinticFunction() {
178
179
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
187 final double computedResult = quintic.value(leftEdge, rightEdge, x);
188
189
190 assertEquals(0.31744, computedResult, THRESHOLD);
191
192 }
193
194 @Test
195 void testFieldEdgesConsistency() {
196 assertThrows(MathIllegalArgumentException.class, () -> {
197
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
207 smoothstep.value(leftEdge, rightEdge, x);
208 });
209 }
210
211 @Test
212 void testFieldBoundaries() {
213
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
224 final Binary64 computedResult1 = clamp.value(leftEdge, rightEdge, x1);
225 final Binary64 computedResult2 = clamp.value(leftEdge, rightEdge, x2);
226
227
228 assertEquals(0, computedResult1.getReal(), THRESHOLD);
229 assertEquals(1, computedResult2.getReal(), THRESHOLD);
230 }
231
232 @Test
233 void testFieldNormalizedInput() {
234
235
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
242 final Binary64 computedResult = cubic.value(x);
243
244
245 assertEquals(0.352, computedResult.getReal(), THRESHOLD);
246
247 }
248
249 @Test
250 void testFieldClampFunction() {
251
252
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
261 final Binary64 computedResult = clamp.value(leftEdge, rightEdge, x);
262
263
264 assertEquals(0.4, computedResult.getReal(), THRESHOLD);
265
266 }
267
268 @Test
269 void testFieldQuadraticFunction1() {
270
271
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
281 final Binary64 computedResult = quadratic.value(leftEdge, rightEdge, xField);
282 final Binary64 computedResult2 = quadratic.value((x - leftEdge) / (rightEdge - leftEdge));
283
284
285 assertEquals(0.32, computedResult.getReal(), THRESHOLD);
286 assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
287
288 }
289
290 @Test
291 void testFieldQuadraticFunction2() {
292
293
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
306 assertEquals(0.68, computedResult.getReal(), THRESHOLD);
307 assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
308
309 }
310
311 @Test
312 void testFieldCubicFunction() {
313
314
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
323 final Binary64 computedResult = cubic.value(leftEdge, rightEdge, x);
324
325
326 assertEquals(0.352, computedResult.getReal(), THRESHOLD);
327
328 }
329
330 @Test
331 void testFieldQuinticFunction() {
332
333
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
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
349 assertEquals(0.31744, computedResult.getReal(), THRESHOLD);
350 assertEquals(computedResult.getReal(), computedResult2.getReal(), THRESHOLD);
351 assertEquals(computedResult2.getReal(), computedResult3.getReal(), THRESHOLD);
352 }
353
354 }