extmath 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test.rb ADDED
@@ -0,0 +1,622 @@
1
+ require 'runit/testcase'
2
+ require 'runit/cui/testrunner'
3
+ require 'runit/testsuite'
4
+ require 'rubygems'
5
+ require 'extmath'
6
+
7
+ class Testing_class < RUNIT::TestCase
8
+
9
+ def test_constants
10
+
11
+ assert_equal(Math::E, Extmath::E)
12
+ assert_equal(Math::PI, Extmath::PI)
13
+ assert_equal_float(0.577_215_664_901_532_861, Extmath::C, 1.0e-18)
14
+
15
+ end
16
+
17
+ def test_sign_functions
18
+
19
+ # Check behavior of abs
20
+ assert_equal(3.1, Extmath.abs(-3.1))
21
+ assert_equal(0.0, Extmath.abs( 0.0))
22
+ assert_equal(3.1, Extmath.abs( 3.1))
23
+
24
+ # Check behavior of sign
25
+ assert_equal(-1.0, Extmath.sign(-3.1))
26
+ assert_equal( 0.0, Extmath.sign( 0.0))
27
+ assert_equal( 1.0, Extmath.sign( 3.1))
28
+
29
+ end
30
+
31
+ def test_trigonometric_functions
32
+
33
+ # Check identity of Extmath.acsc(x) and Math.asin(1/x)
34
+ assert_equal_float(Math.asin(-1.0), Extmath.acsc(-1.0), 1.0e-6)
35
+ assert_equal_float(Math.asin(-0.5), Extmath.acsc(-2.0), 1.0e-6)
36
+ assert_equal_float(Math.asin( 0.5), Extmath.acsc( 2.0), 1.0e-6)
37
+ assert_equal_float(Math.asin( 1.0), Extmath.acsc( 1.0), 1.0e-6)
38
+
39
+ # Check identity of Extmath.acos and Math.acos
40
+ assert_equal(Math.acos(-1.0), Extmath.acos(-1.0))
41
+ assert_equal(Math.acos( 1.0), Extmath.acos( 1.0))
42
+
43
+ # Check behavior of acot
44
+ assert_equal_float(0.5 * Math::PI - Math.atan(-1.0), Extmath.acot(-1.0), 1.0e-6)
45
+ assert_equal_float(0.5 * Math::PI - Math.atan( 1.0), Extmath.acot( 1.0), 1.0e-6)
46
+
47
+ # Check identity of Extmath.asec(x) and Math.acos(1/x)
48
+ assert_equal_float(Math.acos(-1.0), Extmath.asec(-1.0), 1.0e-6)
49
+ assert_equal_float(Math.acos(-0.5), Extmath.asec(-2.0), 1.0e-6)
50
+ assert_equal_float(Math.acos( 0.5), Extmath.asec( 2.0), 1.0e-6)
51
+ assert_equal_float(Math.acos( 1.0), Extmath.asec( 1.0), 1.0e-6)
52
+
53
+ # Check identity of Extmath.asin and Math.asin
54
+ assert_equal(Math.asin(-1.0), Extmath.asin(-1.0))
55
+ assert_equal(Math.asin( 1.0), Extmath.asin( 1.0))
56
+
57
+ # Check identity of Extmath.atan and Math.atan
58
+ assert_equal(Math.atan(-1.0), Extmath.atan(-1.0))
59
+ assert_equal(Math.atan( 1.0), Extmath.atan( 1.0))
60
+
61
+ # Check identity of Extmath.atan2 and Math.atan2
62
+ assert_equal(Math.atan2(-3.1, 3.1), Extmath.atan2(-3.1, 3.1))
63
+ assert_equal(Math.atan2( 3.1, 3.1), Extmath.atan2( 3.1, 3.1))
64
+
65
+ # Check identity of Extmath.cos and Math.cos
66
+ assert_equal(Math.cos(-1.0), Extmath.cos(-1.0))
67
+ assert_equal(Math.cos( 1.0), Extmath.cos( 1.0))
68
+
69
+ # Check identity of Extmath.asin and 1.0 / Math.sin
70
+ assert_equal(1.0 / Math.sin(-1.0), Extmath.csc(-1.0))
71
+ assert_equal(1.0 / Math.sin( 1.0), Extmath.csc( 1.0))
72
+
73
+ # Check identity of Extmath.cot and 1.0 / Math.tan
74
+ assert_equal_float(1.0 / Math.tan(1.0), Extmath.cot(1.0), 1.0e-6)
75
+ assert_equal_float(1.0 / Math.tan(2.0), Extmath.cot(2.0), 1.0e-6)
76
+
77
+ # Check identity of Extmath.sec and 1.0 / Math.cos
78
+ assert_equal_float(1.0 / Math.cos(1.0), Extmath.sec(1.0), 1.0e-6)
79
+ assert_equal_float(1.0 / Math.cos(2.0), Extmath.sec(2.0), 1.0e-6)
80
+
81
+ # Check identity of Extmath.sin and Math.sin
82
+ assert_equal(Math.sin(-1.0), Extmath.sin(-1.0))
83
+ assert_equal(Math.sin( 1.0), Extmath.sin( 1.0))
84
+
85
+ # Check identity of Extmath.tan and Math.tan
86
+ assert_equal(Math.tan(-1.0), Extmath.tan(-1.0))
87
+ assert_equal(Math.tan( 1.0), Extmath.tan( 1.0))
88
+
89
+ end
90
+
91
+ def test_hyperbolic_functions
92
+
93
+ # Check behavior of acsch
94
+ assert_equal_float(-0.881_373_6, Extmath.acsch(-1.0), 1.0e-6)
95
+ assert_equal_float(-1.443_635_5, Extmath.acsch(-0.5), 1.0e-6)
96
+ assert_equal_float( 1.443_635_5, Extmath.acsch( 0.5), 1.0e-6)
97
+ assert_equal_float( 0.881_373_6, Extmath.acsch( 1.0), 1.0e-6)
98
+
99
+ # Check identity of Extmath.acosh and Math.acosh
100
+ assert_equal(Math.acosh(1.0), Extmath.acosh(1.0))
101
+ assert_equal(Math.acosh(2.0), Extmath.acosh(2.0))
102
+
103
+ # Check behavior of acoth
104
+ assert_equal_float(0.5 * Math.log(5.0), Extmath.acoth(1.5), 1.0e-6)
105
+ assert_equal_float(0.5 * Math.log(1.5), Extmath.acoth(5.0), 1.0e-6)
106
+
107
+ # Check behavior of asech
108
+ assert_equal_float(2.993_222_8, Extmath.asech(0.1), 1.0e-6)
109
+ assert_equal_float(1.098_612_2, Extmath.asech(0.6), 1.0e-6)
110
+ assert_equal_float(0.0, Extmath.asech(1.0), 1.0e-6)
111
+
112
+ # Check identity of Extmath.asinh and Math.asinh
113
+ assert_equal(Math.asinh(-1.0), Extmath.asinh(-1.0))
114
+ assert_equal(Math.asinh( 1.0), Extmath.asinh( 1.0))
115
+
116
+ # Check identity of Extmath.atanh and Math.atanh
117
+ assert_equal(Math.atanh(-0.5), Extmath.atanh(-0.5))
118
+ assert_equal(Math.atanh( 0.5), Extmath.atanh( 0.5))
119
+
120
+ # Check identity of Extmath.acsch and 1.0 / Math.sinh
121
+ assert_equal(1.0 / Math.sinh(-1.0), Extmath.csch(-1.0))
122
+ assert_equal(1.0 / Math.sinh( 1.0), Extmath.csch( 1.0))
123
+
124
+ # Check identity of Extmat.cosh and Math.cosh
125
+ assert_equal(Math.cosh(-1.0), Extmath.cosh(-1.0))
126
+ assert_equal(Math.cosh( 1.0), Extmath.cosh( 1.0))
127
+
128
+ # Check identity of Extmat.coth and 1.0 / Math.tanh
129
+ assert_equal_float(1.0 / Math.tanh(1.0), Extmath.coth(1.0), 1.0e-6)
130
+ assert_equal_float(1.0 / Math.tanh(2.0), Extmath.coth(2.0), 1.0e-6)
131
+
132
+ # Check identity of Extmat.sech and 1.0 / Math.cosh
133
+ assert_equal_float(1.0 / Math.cosh(1.0), Extmath.sech(1.0), 1.0e-6)
134
+ assert_equal_float(1.0 / Math.cosh(2.0), Extmath.sech(2.0), 1.0e-6)
135
+
136
+ # Check identity of Extmat.sinh and Math.sinh
137
+ assert_equal(Math.sinh(-1.0), Extmath.sinh(-1.0))
138
+ assert_equal(Math.sinh( 1.0), Extmath.sinh( 1.0))
139
+
140
+ # Check identity of Extmat.tanh and Math.tanh
141
+ assert_equal(Math.tanh(-1.0), Extmath.tanh(-1.0))
142
+ assert_equal(Math.tanh( 1.0), Extmath.tanh( 1.0))
143
+
144
+ end
145
+
146
+ def test_explog_functions
147
+
148
+ # Check behavior of exp
149
+ assert_equal_float( 1.0, Extmath.exp(0.0), 1.0e-6)
150
+ assert_equal_float(Math.sqrt(Math::E), Extmath.exp(0.5), 1.0e-6)
151
+ assert_equal_float( Math::E, Extmath.exp(1.0), 1.0e-6)
152
+
153
+ # Check behavior of exp10
154
+ assert_equal_float( 1.0, Extmath.exp10(0.0), 1.0e-6)
155
+ assert_equal_float(Math.sqrt(10.0), Extmath.exp10(0.5), 1.0e-6)
156
+ assert_equal_float( 10.0, Extmath.exp10(1.0), 1.0e-6)
157
+
158
+ # Check behavior of exp2
159
+ assert_equal_float( 1.0, Extmath.exp2(0.0), 1.0e-6)
160
+ assert_equal_float(Math.sqrt(2.0), Extmath.exp2(0.5), 1.0e-6)
161
+ assert_equal_float( 2.0, Extmath.exp2(1.0), 1.0e-6)
162
+
163
+ # Check behavior of frexp
164
+ assert_equal([-0.996_093_75, 8], Extmath.frexp(-255.0))
165
+ assert_equal([-0.5, 1], Extmath.frexp( -1.0))
166
+ assert_equal([ 0.0, 0], Extmath.frexp( 0.0))
167
+ assert_equal([ 0.5, 1], Extmath.frexp( 1.0))
168
+ assert_equal([ 0.996_093_75, 8], Extmath.frexp( 255.0))
169
+
170
+ # Check behavior of ldexp
171
+ assert_equal_float(1.0, Extmath.ldexp(0.25, 2), 1.0e-6)
172
+ assert_equal_float(1.0, Extmath.ldexp(0.5, 1), 1.0e-6)
173
+ assert_equal_float(1.0, Extmath.ldexp(2, -1), 1.0e-6)
174
+ assert_equal_float(1.0, Extmath.ldexp(4, -2), 1.0e-6)
175
+
176
+ # Check identity of Extmat.log and Math.log
177
+ assert_equal(Math.log(2.0), Extmath.log(2.0))
178
+ assert_equal(Math.log(3.0), Extmath.log(3.0))
179
+
180
+ # Check identity of Extmat.log10 and Math.log10
181
+ assert_equal(Math.log10(2.0), Extmath.log10(2.0))
182
+ assert_equal(Math.log10(3.0), Extmath.log10(3.0))
183
+
184
+ # Check behavior of log2
185
+ assert_equal_float(0.5, Extmath.log2(Math.sqrt(2.0)), 1.0e-6)
186
+ assert_equal_float(1.0, Extmath.log2(2.0), 1.0e-6)
187
+ assert_equal_float(2.0, Extmath.log2(4.0), 1.0e-6)
188
+
189
+ end
190
+
191
+ def test_pwr_functions
192
+
193
+ # Check behavior of pow
194
+ assert_equal_float(64.0, Extmath.pow( 4.0, 3.0), 1.0e-6)
195
+ assert_equal_float( 0.729, Extmath.pow( 0.9, 3.0), 1.0e-6)
196
+ assert_equal_float( 0.25, Extmath.pow(64.0, -1.0 / 3.0), 1.0e-6)
197
+ assert_equal_float( 2.0, Extmath.pow( 0.25, -0.5), 1.0e-6)
198
+
199
+ # Check behavior of root
200
+ assert_equal_float( 4.0, Extmath.root(64.0, 3.0), 1.0e-6)
201
+ assert_equal_float( 0.9, Extmath.root( 0.729, 3.0), 1.0e-6)
202
+ assert_equal_float(64.0, Extmath.root( 0.25, -1.0 / 3.0), 1.0e-6)
203
+ assert_equal_float( 0.25, Extmath.root( 2.0, -0.5), 1.0e-6)
204
+
205
+ # Check behavior of sqr
206
+ assert_equal_float(0.81, Extmath.sqr(-0.9), 1.0e-6)
207
+ assert_equal_float(0.49, Extmath.sqr(-0.7), 1.0e-6)
208
+ assert_equal_float(0.49, Extmath.sqr(-0.7), 1.0e-6)
209
+ assert_equal_float(0.81, Extmath.sqr( 0.9), 1.0e-6)
210
+
211
+ # Check behavior of sqrt
212
+ assert_equal_float(0.7, Extmath.sqrt(0.49), 1.0e-6)
213
+ assert_equal_float(0.9, Extmath.sqrt(0.81), 1.0e-6)
214
+
215
+ end
216
+
217
+ def test_rounding_functions
218
+
219
+ # Check behavior of ceil
220
+ assert_equal( 2, Extmath.ceil( 1.999_99))
221
+ assert_equal( 2, Extmath.ceil( 1.5))
222
+ assert_equal( 2, Extmath.ceil( 1.000_01))
223
+ assert_equal( 1, Extmath.ceil( 0.999_99))
224
+ assert_equal( 1, Extmath.ceil( 0.5))
225
+ assert_equal( 1, Extmath.ceil( 0.000_01))
226
+ assert_equal( 0, Extmath.ceil(-0.000_01))
227
+ assert_equal( 0, Extmath.ceil(-0.5))
228
+ assert_equal( 0, Extmath.ceil(-0.999_99))
229
+ assert_equal(-1, Extmath.ceil(-1.000_01))
230
+ assert_equal(-1, Extmath.ceil(-1.5))
231
+ assert_equal(-1, Extmath.ceil(-1.999_99))
232
+
233
+ # Check behavior of floor
234
+ assert_equal( 1, Extmath.floor( 1.999_99))
235
+ assert_equal( 1, Extmath.floor( 1.5))
236
+ assert_equal( 1, Extmath.floor( 1.000_01))
237
+ assert_equal( 0, Extmath.floor( 0.999_99))
238
+ assert_equal( 0, Extmath.floor( 0.5))
239
+ assert_equal( 0, Extmath.floor( 0.000_01))
240
+ assert_equal(-1, Extmath.floor(-0.000_01))
241
+ assert_equal(-1, Extmath.floor(-0.5))
242
+ assert_equal(-1, Extmath.floor(-0.999_99))
243
+ assert_equal(-2, Extmath.floor(-1.000_01))
244
+ assert_equal(-2, Extmath.floor(-1.5))
245
+ assert_equal(-2, Extmath.floor(-1.999_99))
246
+
247
+ # Check behavior of round
248
+ assert_equal( 2, Extmath.round( 1.5))
249
+ assert_equal( 1, Extmath.round( 1.4))
250
+ assert_equal( 1, Extmath.round( 0.5))
251
+ assert_equal( 0, Extmath.round( 0.4))
252
+ assert_equal( 0, Extmath.round(-0.4))
253
+ assert_equal(-1, Extmath.round(-0.5))
254
+ assert_equal(-1, Extmath.round(-1.4))
255
+ assert_equal(-2, Extmath.round(-1.5))
256
+
257
+ end
258
+
259
+ def test_tensors
260
+
261
+ 1.upto(3) { |i|
262
+ 1.upto(3) { |j|
263
+ assert_equal(i == j ? 1 : 0, Extmath.delta(i, j))
264
+ }
265
+ }
266
+
267
+ 1.upto(3) { |i|
268
+ 1.upto(3) { |j|
269
+ 1.upto(3) { |k|
270
+ case [i,j,k]
271
+ when [1,2,3] then assert_equal( 1, Extmath.epsilon(i, j, k))
272
+ when [1,3,2] then assert_equal(-1, Extmath.epsilon(i, j, k))
273
+ when [2,1,3] then assert_equal(-1, Extmath.epsilon(i, j, k))
274
+ when [2,3,1] then assert_equal( 1, Extmath.epsilon(i, j, k))
275
+ when [3,1,2] then assert_equal( 1, Extmath.epsilon(i, j, k))
276
+ when [3,2,1] then assert_equal(-1, Extmath.epsilon(i, j, k))
277
+ else assert_equal( 0, Extmath.epsilon(i, j, k))
278
+ end
279
+ }
280
+ }
281
+ }
282
+
283
+ end
284
+
285
+ def test_solvers
286
+
287
+ ###############################################################################################################################
288
+ # Check linsolve
289
+ ###############################################################################################################################
290
+
291
+ assert_nil(Extmath.linsolve(0.0, 1.0, 1.0))
292
+ assert_nil(Extmath.linsolve(0.0, 2.0))
293
+
294
+ assert_equal_float( 1.0, Extmath.linsolve(3.0, 6.0, 9.0), 1.0e-6)
295
+ assert_equal_float(-3.0, Extmath.linsolve(9.0, 27.0, 0.0), 1.0e-6)
296
+ assert_equal_float(-3.0, Extmath.linsolve(9.0, 27.0), 1.0e-6)
297
+
298
+ assert_equal(Extmath.linsolve(7.0, 21.0, 0.0), Extmath.linsolve(7.0, 21.0))
299
+
300
+ ###############################################################################################################################
301
+ # Check sqsolve
302
+ ###############################################################################################################################
303
+
304
+ assert_equal(nil, Extmath.sqsolve(0.0, 0.0, 1.0, 1.0))
305
+ assert_equal(nil, Extmath.sqsolve(0.0, 0.0, 2.0))
306
+ assert_equal(nil, Extmath.sqsolve(7.0, 0.0, 5.0, 3.0))
307
+
308
+ assert_equal(Array, Extmath.sqsolve(0.0, 3.0, 6.0, 9.0).class)
309
+ assert_equal(Array, Extmath.sqsolve(0.0, 9.0, 27.0, 0.0).class)
310
+ assert_equal(Array, Extmath.sqsolve(0.0, 9.0, 27.0 ).class)
311
+ assert_equal(Array, Extmath.sqsolve(7.0, 0.0, 3.0, 3.0).class)
312
+ assert_equal(Array, Extmath.sqsolve(2.0, -3.0, 3.0, 3.0).class)
313
+ assert_equal(Array, Extmath.sqsolve(4.0, 0.0, 1.0, 15.0).class)
314
+ assert_equal(Array, Extmath.sqsolve(3.0, 6.0, 1.0, -2.0).class)
315
+ assert_equal(Array, Extmath.sqsolve(9.0, 18.0, 6.0, 6.0).class)
316
+ assert_equal(Array, Extmath.sqsolve(3.0, 6.0, -21.0, 0.0).class)
317
+
318
+ assert_equal(1, Extmath.sqsolve(0.0, 3.0, 6.0, 9.0).length)
319
+ assert_equal(1, Extmath.sqsolve(0.0, 9.0, 27.0, 0.0).length)
320
+ assert_equal(1, Extmath.sqsolve(0.0, 9.0, 27.0 ).length)
321
+ assert_equal(2, Extmath.sqsolve(7.0, 0.0, 3.0, 3.0).length)
322
+ assert_equal(2, Extmath.sqsolve(2.0, -3.0, 3.0, 3.0).length)
323
+ assert_equal(2, Extmath.sqsolve(4.0, 0.0, 1.0, 15.0).length)
324
+ assert_equal(2, Extmath.sqsolve(3.0, 6.0, 1.0, -2.0).length)
325
+ assert_equal(2, Extmath.sqsolve(9.0, 18.0, 6.0, 6.0).length)
326
+ assert_equal(2, Extmath.sqsolve(9.0, 6.0, -21.0, 0.0).length)
327
+
328
+ assert_equal(Extmath.sqsolve(0.0, 7.0, 21.0, 0.0), Extmath.sqsolve(0.0, 7.0, 21.0))
329
+ assert_equal(Extmath.sqsolve(2.0, 3.0, 4.0, 4.0), Extmath.sqsolve(2.0, 3.0, 0.0))
330
+
331
+ assert_equal_float( 1.0, Extmath.sqsolve(0.0, 3.0, 6.0, 9.0)[0], 1.0e-6)
332
+ assert_equal_float(-3.0, Extmath.sqsolve(0.0, 9.0, 27.0, 0.0)[0], 1.0e-6)
333
+ assert_equal_float(-3.0, Extmath.sqsolve(0.0, 9.0, 27.0 )[0], 1.0e-6)
334
+ assert_equal_float( 0.0, Extmath.sqsolve(7.0, 0.0, 3.0, 3.0)[0], 1.0e-6)
335
+ assert_equal_float( 0.0, Extmath.sqsolve(2.0, -3.0, 3.0, 3.0)[0], 1.0e-6)
336
+ assert_equal_float(-2.0, Extmath.sqsolve(4.0, 0.0, -1.0, 15.0)[0], 1.0e-6)
337
+ assert_equal_float(-1.0, Extmath.sqsolve(3.0, 6.0, 1.0, -2.0)[0], 1.0e-6)
338
+ assert_equal_float(-2.0, Extmath.sqsolve(9.0, 18.0, 6.0, 6.0)[0], 1.0e-6)
339
+ assert_equal_float(-1.0 - Math.sqrt(8), Extmath.sqsolve(3.0, 6.0, -21.0, 0.0)[0], 1.0e-6)
340
+
341
+ assert_equal_float(0.0, Extmath.sqsolve(7.0, 0.0, 3.0, 3.0)[1], 1.0e-6)
342
+ assert_equal_float(1.5, Extmath.sqsolve(2.0, -3.0, 3.0, 3.0)[1], 1.0e-6)
343
+ assert_equal_float(2.0, Extmath.sqsolve(4.0, 0.0, -1.0, 15.0)[1], 1.0e-6)
344
+ assert_equal_float(-1.0, Extmath.sqsolve(3.0, 6.0, 1.0, -2.0)[1], 1.0e-6)
345
+ assert_equal_float( 0.0, Extmath.sqsolve(9.0, 18.0, 6.0, 6.0)[1], 1.0e-6)
346
+ assert_equal_float(-1.0 + Math.sqrt(8), Extmath.sqsolve(3.0, 6.0, -21.0, 0.0)[1], 1.0e-6)
347
+
348
+ end
349
+
350
+ def test_conversions
351
+
352
+ assert_equal_float( 90.0, Extmath.rad2deg(0.5 * Extmath::PI), 1.0e-6)
353
+ assert_equal_float(180.0 / Extmath::PI, Extmath.rad2deg(1.0), 1.0e-6)
354
+
355
+ assert_equal_float(100.0, Extmath.rad2gon(0.5 * Extmath::PI), 1.0e-6)
356
+ assert_equal_float(200.0 / Extmath::PI, Extmath.rad2gon(1.0), 1.0e-6)
357
+
358
+ assert_equal_float( 0.5 * Extmath::PI, Extmath.deg2rad( 90.0), 1.0e-6)
359
+ assert_equal_float(10.0 * Extmath::PI / 9.0, Extmath.deg2rad(200.0), 1.0e-6)
360
+
361
+ assert_equal_float( 100.0, Extmath.deg2gon( 90.0), 1.0e-6)
362
+ assert_equal_float(2000.0 / 9.0, Extmath.deg2gon(200.0), 1.0e-6)
363
+
364
+ assert_equal_float(0.5 * Extmath::PI, Extmath.gon2rad(100.0), 1.0e-6)
365
+ assert_equal_float(0.21 * Extmath::PI, Extmath.gon2rad( 42.0), 1.0e-6)
366
+
367
+ assert_equal_float(90.0, Extmath.gon2deg(100.0), 1.0e-6)
368
+ assert_equal_float(37.8, Extmath.gon2deg( 42.0), 1.0e-6)
369
+
370
+ end
371
+
372
+ def test_misc_functions
373
+
374
+ # Check behavior of beta
375
+ assert_equal_float( 1.0, Extmath.beta(1.0, 1.0), 1.0e-6)
376
+ assert_equal_float( 0.5, Extmath.beta(1.0, 2.0), 1.0e-6)
377
+ assert_equal_float( 1.0 / 3.0, Extmath.beta(1.0, 3.0), 1.0e-6)
378
+ assert_equal_float( 0.5, Extmath.beta(2.0, 1.0), 1.0e-6)
379
+ assert_equal_float( 1.0 / 6.0, Extmath.beta(2.0, 2.0), 1.0e-6)
380
+ assert_equal_float( 1.0 / 12.0, Extmath.beta(2.0, 3.0), 1.0e-6)
381
+ assert_equal_float( 1.0 / 3.0, Extmath.beta(3.0, 1.0), 1.0e-6)
382
+ assert_equal_float( 1.0 / 12.0, Extmath.beta(3.0, 2.0), 1.0e-6)
383
+ assert_equal_float( 1.0 / 30.0, Extmath.beta(3.0, 3.0), 1.0e-6)
384
+
385
+ # Check behavior of erf and erfc
386
+ assert_equal_float(0.0, Extmath.erf(0.0), 1.0e-6)
387
+ assert_equal_float(1.0, Extmath.erfc(0.0), 1.0e-6)
388
+
389
+ # Check behavior of factorial
390
+ fac = 1
391
+ 1.upto(100) {|i|
392
+ fac *= i
393
+ assert_equal(fac, Extmath.factorial(i))
394
+ }
395
+
396
+ # Check behavior of tgamma (assert data that can be found in Bronstein-Semendjajew).
397
+ assert_equal_float(1.000_00, Extmath.tgamma(1.00), 1.0e-5)
398
+ assert_equal_float(0.947_40, Extmath.tgamma(1.11), 1.0e-5)
399
+ assert_equal_float(0.913_11, Extmath.tgamma(1.22), 1.0e-5)
400
+ assert_equal_float(0.893_38, Extmath.tgamma(1.33), 1.0e-5)
401
+ assert_equal_float(0.885_81, Extmath.tgamma(1.44), 1.0e-5)
402
+ assert_equal_float(0.888_87, Extmath.tgamma(1.55), 1.0e-5)
403
+ assert_equal_float(0.901_67, Extmath.tgamma(1.66), 1.0e-5)
404
+ assert_equal_float(0.923_76, Extmath.tgamma(1.77), 1.0e-5)
405
+ assert_equal_float(0.955_07, Extmath.tgamma(1.88), 1.0e-5)
406
+ assert_equal_float(0.995_81, Extmath.tgamma(1.99), 1.0e-5)
407
+
408
+ # Check behavior of gcd
409
+ assert_equal( 4, Extmath.gcd( 4_732, 4_700))
410
+ assert_equal( 1, Extmath.gcd( 3_651, 5_023))
411
+ assert_equal(Extmath.factorial(47), Extmath.gcd(Extmath.factorial(90), Extmath.factorial(47)))
412
+ assert_equal( 5_565_651, Extmath.gcd( 612_221_610, 67_461_255_771))
413
+
414
+ # Check behavior of hypoth
415
+ assert_equal_float(Math.sqrt( 5.0), Extmath.hypot(1.0, 2.0), 1.0e-6)
416
+ assert_equal_float(Math.sqrt(13.0), Extmath.hypot(2.0, 3.0), 1.0e-6)
417
+ assert_equal_float( 5.0, Extmath.hypot(3.0, 4.0), 1.0e-6)
418
+
419
+ # Check behavior of lcm
420
+ assert_equal(17_017, Extmath.lcm( 77, 221))
421
+ assert_equal( 2_431, Extmath.lcm(143, 187))
422
+ assert_equal( 1_547, Extmath.lcm( 91, 119))
423
+
424
+ # Check behavior of lgamma
425
+ assert_equal_float( 0.0, Extmath.lgamma(2.0), 1.0e-6)
426
+ assert_equal_float(Extmath.log( 2.0), Extmath.lgamma(3.0), 1.0e-6)
427
+ assert_equal_float(Extmath.log( 6.0), Extmath.lgamma(4.0), 1.0e-6)
428
+ assert_equal_float(Extmath.log( 24.0), Extmath.lgamma(5.0), 1.0e-6)
429
+ assert_equal_float(Extmath.log(120.0), Extmath.lgamma(6.0), 1.0e-6)
430
+ assert_equal_float(Extmath.log(720.0), Extmath.lgamma(7.0), 1.0e-6)
431
+
432
+ # Check behavior of sinc
433
+ assert_equal_float( 1.0, Extmath.sinc( 0.0), 0.0)
434
+ assert_equal_float( Math.sin(1.0), Extmath.sinc( 1.0), 0.0)
435
+ assert_equal_float( Math.sin(1.0), Extmath.sinc(-1.0), 0.0)
436
+ assert_equal_float(0.5 * Math.sin(2.0), Extmath.sinc( 2.0), 0.0)
437
+ assert_equal_float(0.5 * Math.sin(2.0), Extmath.sinc(-2.0), 0.0)
438
+
439
+ end
440
+
441
+ def test_sign_consistency
442
+
443
+ # Check complementarity of abs and sign
444
+ assert_equal_float(-2.0, Extmath.abs(-2.0) * Extmath.sign(-2.0), 1e-6)
445
+ assert_equal_float( 0.0, Extmath.abs( 0.0) * Extmath.sign( 0.0), 1e-6)
446
+ assert_equal_float( 3.0, Extmath.abs( 3.0) * Extmath.sign( 3.0), 1e-6)
447
+
448
+ end
449
+
450
+ def test_trigonometric_consistency
451
+
452
+ # Check complementarity of acos and cos
453
+ assert_equal_float( 0.5, Extmath.acos(Extmath.cos(-0.5)), 1.0e-6)
454
+ assert_equal_float(-0.5, Extmath.cos(Extmath.acos(-0.5)), 1.0e-6)
455
+ assert_equal_float( 0.5, Extmath.acos(Extmath.cos( 0.5)), 1.0e-6)
456
+ assert_equal_float( 0.5, Extmath.cos(Extmath.acos( 0.5)), 1.0e-6)
457
+
458
+ # Check complementarity of acot and cot
459
+ assert_equal_float(1.0, Extmath.acot(Extmath.cot(1.0)), 1.0e-6)
460
+ assert_equal_float(1.0, Extmath.cot(Extmath.acot(1.0)), 1.0e-6)
461
+ assert_equal_float(2.0, Extmath.acot(Extmath.cot(2.0)), 1.0e-6)
462
+ assert_equal_float(2.0, Extmath.cot(Extmath.acot(2.0)), 1.0e-6)
463
+
464
+ # Check complementarity of acsc and csc
465
+ assert_equal_float(-0.5, Extmath.acsc(Extmath.csc(-0.5)), 1.0e-6)
466
+ assert_equal_float(-2.0, Extmath.csc(Extmath.acsc(-2.0)), 1.0e-6)
467
+ assert_equal_float( 0.5, Extmath.acsc(Extmath.csc( 0.5)), 1.0e-6)
468
+ assert_equal_float( 2.0, Extmath.csc(Extmath.acsc( 2.0)), 1.0e-6)
469
+
470
+ # Check complementarity of asec and sec
471
+ assert_equal_float( 0.5, Extmath.asec(Extmath.sec(-0.5)), 1.0e-6)
472
+ assert_equal_float(-2.0, Extmath.sec(Extmath.asec(-2.0)), 1.0e-6)
473
+ assert_equal_float( 0.5, Extmath.asec(Extmath.sec( 0.5)), 1.0e-6)
474
+ assert_equal_float( 2.0, Extmath.sec(Extmath.asec( 2.0)), 1.0e-6)
475
+
476
+ # Check complementarity of asin and sin
477
+ assert_equal_float( 0.5, Extmath.asin(Extmath.sin( 0.5)), 1.0e-6)
478
+ assert_equal_float( 0.5, Extmath.sin(Extmath.asin( 0.5)), 1.0e-6)
479
+ assert_equal_float(-0.5, Extmath.asin(Extmath.sin(-0.5)), 1.0e-6)
480
+ assert_equal_float(-0.5, Extmath.sin(Extmath.asin(-0.5)), 1.0e-6)
481
+
482
+ # Check consistency of atan2 and acot
483
+ assert_equal_float(Extmath.atan2(1.0, 1.0), Extmath.acot(1.0), 1.0e-6)
484
+ assert_equal_float(Extmath.atan2(1.0, 2.0), Extmath.acot(2.0), 1.0e-6)
485
+
486
+ # Check consistency of atan2 and atan
487
+ assert_equal_float(Extmath.atan2(-3.1, 3.1), Extmath.atan(-1.0), 1.0e-6)
488
+ assert_equal_float(Extmath.atan2( 3.1, 3.1), Extmath.atan( 1.0), 1.0e-6)
489
+
490
+ # Check complementarity of atan and tan
491
+ assert_equal_float( 0.5, Extmath.atan(Extmath.tan( 0.5)), 1.0e-6)
492
+ assert_equal_float( 0.5, Extmath.tan(Extmath.atan( 0.5)), 1.0e-6)
493
+ assert_equal_float(-0.5, Extmath.atan(Extmath.tan(-0.5)), 1.0e-6)
494
+ assert_equal_float(-0.5, Extmath.tan(Extmath.atan(-0.5)), 1.0e-6)
495
+
496
+ # Check complementarity of cos and sec
497
+ assert_equal_float(1.0, Extmath.sec(-2.0) * Extmath.cos(-2.0), 1.0e-6)
498
+ assert_equal_float(1.0, Extmath.sec(-1.0) * Extmath.cos(-1.0), 1.0e-6)
499
+ assert_equal_float(1.0, Extmath.sec( 1.0) * Extmath.cos( 1.0), 1.0e-6)
500
+ assert_equal_float(1.0, Extmath.sec( 2.0) * Extmath.cos( 2.0), 1.0e-6)
501
+
502
+ # Check complementarity of csc and sin
503
+ assert_equal_float(1.0, Extmath.csc(-2.0) * Extmath.sin(-2.0), 1.0e-6)
504
+ assert_equal_float(1.0, Extmath.csc(-1.0) * Extmath.sin(-1.0), 1.0e-6)
505
+ assert_equal_float(1.0, Extmath.csc( 1.0) * Extmath.sin( 1.0), 1.0e-6)
506
+ assert_equal_float(1.0, Extmath.csc( 2.0) * Extmath.sin( 2.0), 1.0e-6)
507
+
508
+ end
509
+
510
+ def test_hyperbolic_consistency
511
+
512
+ # Check complimentarity of acsch and csch
513
+ assert_equal_float(-1.0, Extmath.csch(Extmath.acsch(-1.0)), 1.0e-6)
514
+ assert_equal_float(-1.0, Extmath.acsch(Extmath.csch(-1.0)), 1.0e-6)
515
+ assert_equal_float(-0.5, Extmath.csch(Extmath.acsch(-0.5)), 1.0e-6)
516
+ assert_equal_float(-0.5, Extmath.acsch(Extmath.csch(-0.5)), 1.0e-6)
517
+ assert_equal_float( 0.5, Extmath.csch(Extmath.acsch( 0.5)), 1.0e-6)
518
+ assert_equal_float( 0.5, Extmath.acsch(Extmath.csch( 0.5)), 1.0e-6)
519
+ assert_equal_float( 1.0, Extmath.csch(Extmath.acsch( 1.0)), 1.0e-6)
520
+ assert_equal_float( 1.0, Extmath.acsch(Extmath.csch( 1.0)), 1.0e-6)
521
+
522
+ # Check complementarity of acosh and cosh
523
+ assert_equal_float(1.5, Extmath.acosh(Extmath.cosh( 1.5)), 1.0e-6)
524
+ assert_equal_float(1.5, Extmath.cosh(Extmath.acosh( 1.5)), 1.0e-6)
525
+ assert_equal_float(1.5, Extmath.acosh(Extmath.cosh(-1.5)), 1.0e-6)
526
+
527
+ # Check complementarity of acoth and coth
528
+ assert_equal_float( 1.5, Extmath.acoth(Extmath.coth( 1.5)), 1.0e-6)
529
+ assert_equal_float( 1.5, Extmath.coth(Extmath.acoth( 1.5)), 1.0e-6)
530
+ assert_equal_float(-1.5, Extmath.acoth(Extmath.coth(-1.5)), 1.0e-6)
531
+ assert_equal_float(-1.5, Extmath.coth(Extmath.acoth(-1.5)), 1.0e-6)
532
+
533
+ # Check complimentarity of asech and sech
534
+ assert_equal_float( 0.1, Extmath.sech(Extmath.asech(0.1)), 1.0e-6)
535
+ assert_equal_float( 0.1, Extmath.asech(Extmath.sech(0.1)), 1.0e-6)
536
+ assert_equal_float( 0.6, Extmath.sech(Extmath.asech(0.6)), 1.0e-6)
537
+ assert_equal_float( 0.6, Extmath.asech(Extmath.sech(0.6)), 1.0e-6)
538
+ assert_equal_float( 1.0, Extmath.sech(Extmath.asech(1.0)), 1.0e-6)
539
+ assert_equal_float( 1.0, Extmath.asech(Extmath.sech(1.0)), 1.0e-6)
540
+
541
+ # Check complementarity of asinh and sinh
542
+ assert_equal_float( 1.5, Extmath.asinh(Extmath.sinh( 1.5)), 1.0e-6)
543
+ assert_equal_float( 1.5, Extmath.sinh(Extmath.asinh( 1.5)), 1.0e-6)
544
+ assert_equal_float(-1.5, Extmath.asinh(Extmath.sinh(-1.5)), 1.0e-6)
545
+ assert_equal_float(-1.5, Extmath.sinh(Extmath.asinh(-1.5)), 1.0e-6)
546
+
547
+ # Check complementarity of atanh and tanh
548
+ assert_equal_float( 0.5, Extmath.atanh(Extmath.tanh( 0.5)), 1.0e-6)
549
+ assert_equal_float( 0.5, Extmath.tanh(Extmath.atanh( 0.5)), 1.0e-6)
550
+ assert_equal_float(-0.5, Extmath.atanh(Extmath.tanh(-0.5)), 1.0e-6)
551
+ assert_equal_float(-0.5, Extmath.tanh(Extmath.atanh(-0.5)), 1.0e-6)
552
+
553
+ # Check complementarity of csch and sinh
554
+ assert_equal_float(1.0, Extmath.csch(-2.0) * Extmath.sinh(-2.0), 1.0e-6)
555
+ assert_equal_float(1.0, Extmath.csch(-1.0) * Extmath.sinh(-1.0), 1.0e-6)
556
+ assert_equal_float(1.0, Extmath.csch( 1.0) * Extmath.sinh( 1.0), 1.0e-6)
557
+ assert_equal_float(1.0, Extmath.csch( 2.0) * Extmath.sinh( 2.0), 1.0e-6)
558
+
559
+ # Check complementarity of cosh and sech
560
+ assert_equal_float(1.0, Extmath.sech(-2.0) * Extmath.cosh(-2.0), 1.0e-6)
561
+ assert_equal_float(1.0, Extmath.sech(-1.0) * Extmath.cosh(-1.0), 1.0e-6)
562
+ assert_equal_float(1.0, Extmath.sech( 1.0) * Extmath.cosh( 1.0), 1.0e-6)
563
+ assert_equal_float(1.0, Extmath.sech( 2.0) * Extmath.cosh( 2.0), 1.0e-6)
564
+
565
+ end
566
+
567
+ def test_explog_consistency
568
+
569
+ # Check complementarity of exp and log
570
+ assert_equal_float(0.5, Extmath.log(Extmath.exp(0.5)), 1.0e-6)
571
+ assert_equal_float(1.0, Extmath.log(Extmath.exp(1.0)), 1.0e-6)
572
+ assert_equal_float(2.0, Extmath.log(Extmath.exp(2.0)), 1.0e-6)
573
+ assert_equal_float(0.5, Extmath.exp(Extmath.log(0.5)), 1.0e-6)
574
+ assert_equal_float(1.0, Extmath.exp(Extmath.log(1.0)), 1.0e-6)
575
+ assert_equal_float(2.0, Extmath.exp(Extmath.log(2.0)), 1.0e-6)
576
+
577
+ # Check complementarity of exp10 and log10
578
+ assert_equal_float(0.5, Extmath.log10(Extmath.exp10(0.5)), 1.0e-6)
579
+ assert_equal_float(1.0, Extmath.log10(Extmath.exp10(1.0)), 1.0e-6)
580
+ assert_equal_float(2.0, Extmath.log10(Extmath.exp10(2.0)), 1.0e-6)
581
+ assert_equal_float(0.5, Extmath.exp10(Extmath.log10(0.5)), 1.0e-6)
582
+ assert_equal_float(1.0, Extmath.exp10(Extmath.log10(1.0)), 1.0e-6)
583
+ assert_equal_float(2.0, Extmath.exp10(Extmath.log10(2.0)), 1.0e-6)
584
+
585
+ # Check complementarity of exp2 and log2
586
+ assert_equal_float(0.5, Extmath.log2(Extmath.exp2(0.5)), 1.0e-6)
587
+ assert_equal_float(1.0, Extmath.log2(Extmath.exp2(1.0)), 1.0e-6)
588
+ assert_equal_float(2.0, Extmath.log2(Extmath.exp2(2.0)), 1.0e-6)
589
+ assert_equal_float(0.5, Extmath.exp2(Extmath.log2(0.5)), 1.0e-6)
590
+ assert_equal_float(1.0, Extmath.exp2(Extmath.log2(1.0)), 1.0e-6)
591
+ assert_equal_float(2.0, Extmath.exp2(Extmath.log2(2.0)), 1.0e-6)
592
+
593
+ end
594
+
595
+ def test_pwr_consistency
596
+
597
+ # Check complementarity of pow and root
598
+ assert_equal_float( 4.0, Extmath.root(Extmath.pow(2.0, 6.0), 3.0), 1.0e-6)
599
+ assert_equal_float( 2.0, Extmath.root(Extmath.pow(4.0, 4.0), 8.0), 1.0e-6)
600
+ assert_equal_float(Math.sqrt(2.0), Extmath.pow(Extmath.root(2.0, 6.0), 3.0), 1.0e-6)
601
+ assert_equal_float( 16.0, Extmath.pow(Extmath.root(4.0, 4.0), 8.0), 1.0e-6)
602
+
603
+ # Check complementarity of sqrt and sqr
604
+ assert_equal_float(0.3, Extmath.sqrt(Extmath.sqr( 0.3)), 1.0e-6)
605
+ assert_equal_float(0.3, Extmath.sqrt(Extmath.sqr(-0.3)), 1.0e-6)
606
+ assert_equal_float(0.3, Extmath.sqr(Extmath.sqrt( 0.3)), 1.0e-6)
607
+
608
+ end
609
+
610
+ def test_misc_consistency
611
+
612
+ # Check complimentarity of erf and erfc
613
+ assert_equal_float(1.0, Extmath.erf(1.0) + Extmath.erfc(1.0), 1.0e-6)
614
+ assert_equal_float(1.0, Extmath.erf(2.0) + Extmath.erfc(2.0), 1.0e-6)
615
+ assert_equal_float(1.0, Extmath.erf(3.0) + Extmath.erfc(3.0), 1.0e-6)
616
+ assert_equal_float(1.0, Extmath.erf(4.0) + Extmath.erfc(4.0), 1.0e-6)
617
+
618
+ end
619
+
620
+ end
621
+
622
+ RUNIT::CUI::TestRunner.run(Testing_class.suite)