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.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
33 final double x = 2;
34 final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
35
36
37 smoothstep.value(x);
38 }
39
40 @Test(expected = MathIllegalArgumentException.class)
41 public void testExceptionOverBoundary() {
42
43 final double x = 17;
44 final SmoothStepFactory.SmoothStepFunction smoothstep = SmoothStepFactory.getGeneralOrder(1);
45
46
47 smoothstep.value(x);
48 }
49
50 @Test(expected = MathIllegalArgumentException.class)
51 public void testEdgesConsistency() {
52
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
59 smoothstep.value(leftEdge, rightEdge, x);
60 }
61
62 @Test
63 public void testBoundaries() {
64
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
73 final double computedResult1 = clamp.value(leftEdge, rightEdge, x1);
74 final double computedResult2 = clamp.value(leftEdge, rightEdge, x2);
75
76
77 Assert.assertEquals(0, computedResult1, THRESHOLD);
78 Assert.assertEquals(1, computedResult2, THRESHOLD);
79 }
80
81 @Test
82 public void testNormalizedInput() {
83
84
85 final double x = 0.4;
86 final SmoothStepFactory.SmoothStepFunction cubic = SmoothStepFactory.getCubic();
87
88
89 final double computedResult = cubic.value(x);
90
91
92 Assert.assertEquals(0.352, computedResult, THRESHOLD);
93
94 }
95
96 @Test
97 public void testClampFunction() {
98
99
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
107 final double computedResult = clamp.value(leftEdge, rightEdge, x);
108
109
110 Assert.assertEquals(0.4, computedResult, THRESHOLD);
111
112 }
113
114 @Test
115 public void testQuadraticFunction1() {
116
117
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
125 final double computedResult = quadratic.value(leftEdge, rightEdge, x);
126
127
128 Assert.assertEquals(0.32, computedResult, THRESHOLD);
129
130 }
131
132 @Test
133 public void testQuadraticFunction2() {
134
135
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
143 final double computedResult = quadratic.value(leftEdge, rightEdge, x);
144
145
146 Assert.assertEquals(0.68, computedResult, THRESHOLD);
147
148 }
149
150 @Test
151 public void testCubicFunction() {
152
153
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
161 final double computedResult = cubic.value(leftEdge, rightEdge, x);
162
163
164 Assert.assertEquals(0.352, computedResult, THRESHOLD);
165
166 }
167
168 @Test
169 public void testQuinticFunction() {
170
171
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
179 final double computedResult = quintic.value(leftEdge, rightEdge, x);
180
181
182 Assert.assertEquals(0.31744, computedResult, THRESHOLD);
183
184 }
185
186 @Test(expected = MathIllegalArgumentException.class)
187 public void testFieldEdgesConsistency() {
188
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
198 smoothstep.value(leftEdge, rightEdge, x);
199 }
200
201 @Test
202 public void testFieldBoundaries() {
203
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
214 final Binary64 computedResult1 = clamp.value(leftEdge, rightEdge, x1);
215 final Binary64 computedResult2 = clamp.value(leftEdge, rightEdge, x2);
216
217
218 Assert.assertEquals(0, computedResult1.getReal(), THRESHOLD);
219 Assert.assertEquals(1, computedResult2.getReal(), THRESHOLD);
220 }
221
222 @Test
223 public void testFieldNormalizedInput() {
224
225
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
232 final Binary64 computedResult = cubic.value(x);
233
234
235 Assert.assertEquals(0.352, computedResult.getReal(), THRESHOLD);
236
237 }
238
239 @Test
240 public void testFieldClampFunction() {
241
242
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
251 final Binary64 computedResult = clamp.value(leftEdge, rightEdge, x);
252
253
254 Assert.assertEquals(0.4, computedResult.getReal(), THRESHOLD);
255
256 }
257
258 @Test
259 public void testFieldQuadraticFunction1() {
260
261
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
271 final Binary64 computedResult = quadratic.value(leftEdge, rightEdge, xField);
272 final Binary64 computedResult2 = quadratic.value((x - leftEdge) / (rightEdge - leftEdge));
273
274
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
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
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
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
313 final Binary64 computedResult = cubic.value(leftEdge, rightEdge, x);
314
315
316 Assert.assertEquals(0.352, computedResult.getReal(), THRESHOLD);
317
318 }
319
320 @Test
321 public void testFieldQuinticFunction() {
322
323
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
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
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 }