more_math 0.0.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.
@@ -0,0 +1,321 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+
6
+ class TestSequence < Test::Unit::TestCase
7
+ include MoreMath
8
+
9
+ def setup
10
+ @flat = Sequence.new([0.3] * 100)
11
+ @flat_fuzzy = Sequence.new(
12
+ [ 0.291296142070089, 0.398027886480809, 0.23101231921608,
13
+ 0.340627534773153, 0.254242992113383, 0.205044980114447,
14
+ 0.278185292370019, 0.291682510899134, 0.261657208149687,
15
+ 0.259844137242866, 0.378499162508619, 0.229358104972725,
16
+ 0.386112073235523, 0.235255070067096, 0.296721262492287,
17
+ 0.314698077842112, 0.363272733105109, 0.252507159666997,
18
+ 0.24662025484673, 0.267331187480918, 0.250067060724856,
19
+ 0.284270210340375, 0.262626100532033, 0.352433737639362,
20
+ 0.26289285183689, 0.320853587421585, 0.311156494750873,
21
+ 0.334225510831559, 0.356205648289854, 0.390488123286748,
22
+ 0.232295923957093, 0.306018392326888, 0.226951061989688,
23
+ 0.214593004467917, 0.28960026747272, 0.265265575971784,
24
+ 0.281799797229565, 0.337363136532618, 0.342475071004423,
25
+ 0.284553882868128, 0.389206786931739, 0.351602477407745,
26
+ 0.387218788482334, 0.251003385993573, 0.257866093151574,
27
+ 0.328824195781741, 0.328242240833557, 0.318885903421821,
28
+ 0.319274078029297, 0.29658003664557, 0.24884905382522,
29
+ 0.301770636812583, 0.248911378817175, 0.275471776328434,
30
+ 0.220091513752346, 0.283076025940448, 0.388218608044549,
31
+ 0.283229339831472, 0.224570945957831, 0.362485839646397,
32
+ 0.221550677368212, 0.269482540591461, 0.339070334243095,
33
+ 0.325182999080969, 0.308728933369353, 0.3863941656383,
34
+ 0.202792339314435, 0.362856265274183, 0.265505144702292,
35
+ 0.353077334823915, 0.324128317440701, 0.296975637938554,
36
+ 0.331591291884613, 0.205993447724166, 0.214480100701257,
37
+ 0.344614724259284, 0.370516595329498, 0.207412716360969,
38
+ 0.314721036012706, 0.228984115281106, 0.259032440399333,
39
+ 0.326352618555389, 0.353756258146666, 0.230771059239658,
40
+ 0.250581960820831, 0.21462520718052, 0.241570172219703,
41
+ 0.296495456059297, 0.336874993277199, 0.399203721142938,
42
+ 0.330151086176299, 0.327699314698143, 0.235627029696985,
43
+ 0.325564466304218, 0.398295977228244, 0.33192554316584,
44
+ 0.22526704197204, 0.342117813790757, 0.32678523559579,
45
+ 0.214938036987578 ]
46
+ )
47
+ @flat_fuzzy2 = Sequence.new(
48
+ [ 0.234651800685522, 0.291677132057536, 0.381325747665659,
49
+ 0.37072453863211, 0.368865699927557, 0.35787506718781,
50
+ 0.350720373167135, 0.258635475849321, 0.31707597552194,
51
+ 0.307893709010183, 0.237849819950067, 0.315881610046543,
52
+ 0.201585641064648, 0.344368312712124, 0.34501166666737,
53
+ 0.294042914293632, 0.211771331394304, 0.363815509779845,
54
+ 0.33673412152282, 0.37498088769697, 0.201244093764913,
55
+ 0.236387765961558, 0.296850838293593, 0.223829530755105,
56
+ 0.213694650150962, 0.227416795706971, 0.200625724917622,
57
+ 0.31227957802719, 0.385983037604518, 0.287242927867868,
58
+ 0.258470258047964, 0.344169516126964, 0.26994416010751,
59
+ 0.249768846393261, 0.354426097251265, 0.34021066927398,
60
+ 0.307077285175548, 0.3497224779728, 0.254650791783532,
61
+ 0.285180048375893, 0.201603698883297, 0.314417350151038,
62
+ 0.320909639401826, 0.287679809618447, 0.328814685203504,
63
+ 0.370476190838299, 0.291359505243309, 0.273781936455096,
64
+ 0.325113918862285, 0.367110740063297, 0.247073598694453,
65
+ 0.350942986897521, 0.232261700593331, 0.236635735267053,
66
+ 0.240796903369692, 0.323428956239516, 0.324614910738737,
67
+ 0.237871567371432, 0.310816928958706, 0.264609945655404,
68
+ 0.236819188672949, 0.28398352994042, 0.366840181124702,
69
+ 0.339882426068036, 0.397478482750453, 0.379375601208701,
70
+ 0.281206116730092, 0.203947998858132, 0.231558650797902,
71
+ 0.380785793096893, 0.334270739370193, 0.266229655641688,
72
+ 0.315762224650585, 0.243378114262551, 0.294001949668671,
73
+ 0.247508966656796, 0.382845661950797, 0.369479413879656,
74
+ 0.241683415140724, 0.218541361179393, 0.319914186441019,
75
+ 0.310250120051708, 0.234697684147101, 0.34734046492662,
76
+ 0.218217334366937, 0.312537298293074, 0.374319776312122,
77
+ 0.392178633368011, 0.314428694398314, 0.386204177791726,
78
+ 0.359061970124816, 0.362334074442194, 0.229293408035385,
79
+ 0.313763536361359, 0.239344793134688, 0.265237324138875,
80
+ 0.329259743982286, 0.351767216150251, 0.211193779699827,
81
+ 0.258235773260784 ]
82
+ )
83
+ @flat_higher = Sequence.new(
84
+ [ 0.417776755544947, 0.326476805772892, 0.332887733006402,
85
+ 0.410565271773857, 0.426114386030809, 0.435935520406595,
86
+ 0.339995159533461, 0.364761157546518, 0.378397233333935,
87
+ 0.35210733002035, 0.330688506187733, 0.492648864412129,
88
+ 0.33833199089868, 0.42789271416588, 0.302423735510181,
89
+ 0.305407403523733, 0.408725319360953, 0.444623946541953,
90
+ 0.494162827022184, 0.386239353430498, 0.306437290600178,
91
+ 0.376703331326491, 0.419906847790677, 0.301955977987602,
92
+ 0.487468198801442, 0.312290516021979, 0.495906290662686,
93
+ 0.303379939018008, 0.460384318463054, 0.473534870478338,
94
+ 0.333912270251847, 0.460143618655486, 0.419257177279749,
95
+ 0.355072829732943, 0.453419475031392, 0.468523177257953,
96
+ 0.405173514106214, 0.490981451264441, 0.333761262319564,
97
+ 0.405754543238307, 0.495673694657207, 0.302783349166472,
98
+ 0.432418922874345, 0.329915804259514, 0.356588738342812,
99
+ 0.354349707229742, 0.452693480248568, 0.474877692732008,
100
+ 0.405383243600942, 0.402915847080871, 0.492915699075631,
101
+ 0.462094206093751, 0.339883346924172, 0.451846443788079,
102
+ 0.464163288957183, 0.405878012725365, 0.467568948869427,
103
+ 0.419585038305752, 0.422900365624952, 0.494116259378179,
104
+ 0.300073213028546, 0.474018244228735, 0.38822872923958,
105
+ 0.441707083196939, 0.406814346112675, 0.403958151779294,
106
+ 0.307247538830431, 0.409650643221185, 0.493148685003474,
107
+ 0.36058138779566, 0.36321317486353, 0.393068747347969,
108
+ 0.468879326612198, 0.425234138346863, 0.421949132207673,
109
+ 0.306005645410334, 0.439055703332639, 0.317183300984821,
110
+ 0.470848293063698, 0.440820107004846, 0.438285035336276,
111
+ 0.434787376714648, 0.453596753001295, 0.399893734859051,
112
+ 0.458116608707833, 0.330973155542121, 0.31666421784907,
113
+ 0.467682075506155, 0.452806591013364, 0.379423936292945,
114
+ 0.357212688143182, 0.385848611013958, 0.349586136874291,
115
+ 0.46683976269393, 0.484776275752459, 0.30829081820033,
116
+ 0.41637633029041, 0.350847171677106, 0.416615749876575,
117
+ 0.382674729559805 ]
118
+ )
119
+ @half = Sequence.new(Array.new(100) { |i| 0.5 * i })
120
+ @rand = Sequence.new(rand = [
121
+ 97, 26, 9, 78, 15, 86, 82, 24, 57, 67, 46, 86, 28,
122
+ 50, 71, 92, 18, 19, 16, 70, 80, 45, 26, 4, 16, 55, 15,
123
+ 94, 12, 73, 89, 97, 10, 2, 77, 35, 76, 46, 48, 31, 39,
124
+ 52, 82, 53, 88, 90, 1, 39, 77, 71, 37, 37, 50, 19, 60,
125
+ 48, 0, 13, 62, 34, 90, 28, 42, 9, 63, 82, 43, 98, 86,
126
+ 3, 94, 5, 79, 11, 16, 0, 90, 81, 42, 64, 76, 92, 25,
127
+ 3, 90, 51, 15, 0, 74, 98, 93, 90, 14, 81, 85, 28, 30,
128
+ 73, 32, 88])
129
+ @rand_up = Sequence.new(Array.new(rand.size) { |i| rand[i] * (1 - 4.0 / (i + 1)) })
130
+ @rand_down = Sequence.new(Array.new(rand.size) { |i| rand[i] * (1 + 4.0 / (i + 1)) })
131
+ @rasi = Sequence.new(
132
+ [ 0.0, 11.7813239550446, 23.8742291678261, 0.368124552684678,
133
+ 20.233654312272, 7.64120827980215, 61.609239533582, 69.346191849821,
134
+ 66.7019061146592, 26.2399845215146, 2.85316954888546, 29.4686175218607,
135
+ 15.9684276548523, 15.9684276548523, 36.3446282769615, 66.5739561406607,
136
+ 85.9585699842718, 75.9895132951814, 9.24615891330947, 7.53001816521557,
137
+ 22.335839587114, 32.2774961648149, 31.2905869781976, 15.1700831170561,
138
+ 6.1413284446509, -2.95898288510399e-14, -4.63732964187926,
139
+ -2.2382089844837, -2.20874731610807, -0.0, -20.5724838302366,
140
+ -60.2401453217246, -39.2961753815653, -59.9472827106431,
141
+ -47.051006728233, -4.75528258147577, -20.6280322653025, -43.913176050844,
142
+ -78.8441115458335, -30.4509047725893, -38.0422606518061,
143
+ -77.8151265120777, -4.22163962751007, -32.3615561965831,
144
+ -42.4419205675787, -40.5571824081806, -6.2627977633223,
145
+ -5.52186829027017, -6.96331684061593, -10.4026583858372,
146
+ 3.8595428936139e-15, 9.0239928166299, 12.9318741325725, 34.9718325050444,
147
+ 46.7301063878664, 49.3739611925678, 58.1865040039386, 30.8205297110316,
148
+ 36.3061007965867, 29.8592927313786, 88.4482560154493, 54.0257987900779,
149
+ 16.9664543832806, 92.8164857438293, 62.8663840466361, 78.9376908524978,
150
+ 41.6220444134369, 78.5224970716874, 35.4436091676863, 66.4010692750828,
151
+ 14.6946313073118, 19.2701469640686, 31.6587115308823, 15.1700831170561,
152
+ 1.25333233564304, 1.83690953073357e-15, -4.76266287544359,
153
+ -17.6569819887047, -12.1481102385944, -39.5038012763407,
154
+ -20.5724838302365, -28.0664313430763, -20.8038575549463,
155
+ -10.1319351060242, -10.8579246295922, -33.2869780703304,
156
+ -77.6006928075664, -21.956588025422, -44.9112027792722,
157
+ -56.9726605422639, -19.0211303259031, -80.5296076694757,
158
+ -15.1979026590363, -51.6243872659778, -45.1801089912934,
159
+ -54.6640284631999, -17.3431322676617, -21.719348608396, -11.937114583913,
160
+ -3.38399730623621 ]
161
+ )
162
+ @rasi_mean = Sequence.new([ 3.48 ] * 100)
163
+ @book = Sequence.new(
164
+ [ 47, 64, 23, 71, 38, 64, 55, 41, 59, 48, 71, 35, 57, 40, 58,
165
+ 44, 89, 55, 37, 74, 51, 57, 50, 60, 45, 57, 50, 45, 25, 59,
166
+ 50, 71, 56, 74, 50, 58, 45, 54, 36, 54, 48, 55, 45, 57, 50,
167
+ 62, 44, 64, 43, 52, 38, 59, 55, 41, 53, 49, 34, 35, 54, 45,
168
+ 68, 38, 50, 60, 39, 59, 40, 57, 54, 23 ]
169
+ )
170
+ end
171
+
172
+ def test_flat
173
+ assert_equal 100, @flat.size
174
+ assert_in_delta 0.3, @flat.mean, 1E-8
175
+ assert_in_delta 0.3, @flat.geometric_mean, 1E-8
176
+ assert_in_delta 0.3, @flat.harmonic_mean, 1E-8
177
+ assert_in_delta 0, @flat.variance, 1E-8
178
+ assert_in_delta 0, @flat.standard_deviation, 1E-8
179
+ assert_in_delta 0, @flat.sample_standard_deviation, 1E-8
180
+ assert_in_delta 30, @flat.sum, 1E-8
181
+ assert_in_delta 0.3, @flat.min, 1E-8
182
+ assert_in_delta 0.3, @flat.max, 1E-8
183
+ assert_in_delta 0.3, @flat.percentile(25), 1E-8
184
+ assert_in_delta 0.3, @flat.median, 1E-8
185
+ assert_in_delta 0.3, @flat.percentile(75), 1E-8
186
+ assert_equal 100, @flat.histogram(10).to_a.first[1]
187
+ end
188
+
189
+ def test_half
190
+ assert_equal 100, @half.size
191
+ assert_in_delta 24.75, @half.mean, 1E-8
192
+ assert_in_delta 0.0, @half.geometric_mean, 1E-8
193
+ assert_equal 'NaN', @half.harmonic_mean.to_s
194
+ assert_in_delta 208.31, @half.variance, 1E-2
195
+ assert_in_delta 14.43, @half.standard_deviation, 1E-2
196
+ assert_in_delta 14.50, @half.sample_standard_deviation, 1E-2
197
+ assert_in_delta 2475, @half.sum, 1E-8
198
+ assert_in_delta 0, @half.min, 1E-8
199
+ assert_in_delta 99 / 2.0, @half.max, 1E-8
200
+ assert_in_delta 12.125, @half.percentile(25), 1E-8
201
+ assert_in_delta 24.75, @half.median, 1E-8
202
+ assert_in_delta 37.375, @half.percentile(75), 1E-8
203
+ assert_equal [10] * 10, counts = @half.histogram(10).to_a.transpose[1]
204
+ assert_equal 100, counts.inject { |s, x| s + x }
205
+ end
206
+
207
+ def test_rand
208
+ assert_equal 100, @rand.size
209
+ assert_in_delta 50.84, @rand.mean, 1E-2
210
+ assert_in_delta 0.0, @rand.geometric_mean, 1E-8
211
+ assert_equal 'NaN', @rand.harmonic_mean.to_s
212
+ assert_in_delta 976.95, @rand.variance, 1E-2
213
+ assert_in_delta 31.25, @rand.standard_deviation, 1E-2
214
+ assert_in_delta 31.41, @rand.sample_standard_deviation, 1E-2
215
+ assert_in_delta 5084, @rand.sum, 1E-8
216
+ assert_in_delta 0, @rand.min, 1E-8
217
+ assert_in_delta 98, @rand.max, 1E-8
218
+ assert_in_delta 20.25, @rand.percentile(25), 1E-8
219
+ assert_in_delta 50.0, @rand.median, 1E-8
220
+ assert_in_delta 81, @rand.percentile(75), 1E-8
221
+ assert_in_delta 0.05660, @rand.linear_regression.a, 1E-4
222
+ assert_in_delta 47.9812, @rand.linear_regression.b, 1E-4
223
+ assert @rand.linear_regression.slope_zero?
224
+ assert_in_delta(-0.4019, @rand_down.linear_regression.a, 1E-4)
225
+ assert_in_delta 82.7303, @rand_down.linear_regression.b, 1E-4
226
+ assert !@rand_down.linear_regression.slope_zero?
227
+ assert_in_delta 0.5151, @rand_up.linear_regression.a, 1E-4
228
+ assert_in_delta(13.2320, @rand_up.linear_regression.b, 1E-4)
229
+ assert !@rand_up.linear_regression.slope_zero?
230
+ assert_nil @rand.detect_outliers
231
+ assert !@rand.detect_autocorrelation[:detected]
232
+ assert_equal [11, 14, 7, 9, 8, 7, 5, 11, 13, 15],
233
+ counts = @rand.histogram(10).to_a.transpose[1]
234
+ assert_equal 100, counts.inject { |s, x| s + x }
235
+ end
236
+
237
+ def test_rasi
238
+ assert_equal 100, @rasi.size
239
+ assert_in_delta 3.48, @rasi.mean, 1E-2
240
+ assert_in_delta 0.0, @rasi.geometric_mean, 1E-8
241
+ assert_equal 'NaN', @rasi.harmonic_mean.to_s
242
+ assert_in_delta 1604.67, @rasi.variance, 1E-2
243
+ assert_in_delta 40.05, @rasi.standard_deviation, 1E-2
244
+ assert_in_delta 40.26, @rasi.sample_standard_deviation, 1E-2
245
+ assert_in_delta 348.007, @rasi.sum, 1E-3
246
+ assert_in_delta 92.81, @rasi.max, 1E-2
247
+ assert_in_delta(-20.75, @rasi.percentile(25), 1E-2)
248
+ assert_in_delta 0.0, @rasi.median, 1E-2
249
+ assert_in_delta 30.58, @rasi.percentile(75), 1E-2
250
+ assert_in_delta(-0.41, @rasi.linear_regression.a, 1E-2)
251
+ assert_in_delta(24.35, @rasi.linear_regression.b, 1E-2)
252
+ assert !@rasi.linear_regression.slope_zero?
253
+ assert_equal 13, @rasi.detect_outliers[:high]
254
+ assert @rasi.detect_autocorrelation[:detected]
255
+ assert_equal [4, 6, 11, 13, 22, 15, 12, 4, 7, 6],
256
+ counts = @rasi.histogram(10).to_a.transpose[1]
257
+ assert_equal 100, counts.inject { |s, x| s + x }
258
+ end
259
+
260
+ def test_book
261
+ assert_equal 70, @book.size
262
+ assert_in_delta 51.25, @book.mean, 1E-2
263
+ assert_in_delta 49.70, @book.geometric_mean, 1E-2
264
+ assert_in_delta 47.98, @book.harmonic_mean, 1E-2
265
+ assert_in_delta 148.36, @book.variance, 1E-2
266
+ assert_in_delta 12.18, @book.standard_deviation, 1E-2
267
+ assert_in_delta 12.26, @book.sample_standard_deviation, 1E-2
268
+ assert_in_delta 3588.0, @book.sum, 1E-2
269
+ assert_in_delta 23, @book.min, 1E-2
270
+ assert_in_delta 89, @book.max, 1E-2
271
+ assert_in_delta(43.75, @book.percentile(25), 1E-2)
272
+ assert_in_delta 51.5, @book.median, 1E-2
273
+ assert_in_delta 58.25, @book.percentile(75), 1E-2
274
+ assert_in_delta(-0.0952, @book.linear_regression.a, 1E-4)
275
+ assert_in_delta(54.6372, @book.linear_regression.b, 1E-4)
276
+ assert @book.linear_regression.slope_zero?
277
+ assert_equal 7, @book.detect_outliers[:high]
278
+ ought = [1.0, -0.39, 0.3, -0.17, 0.07, -0.10, 0.05, 0.04, -0.04, -0.01,
279
+ 0.01, 0.11, -0.07, 0.15, 0.04, -0.01
280
+ ]
281
+ @book.autocorrelation[0, ought.size].zip(ought) do |x, x_o|
282
+ assert_in_delta x, x_o, 8E-2
283
+ end
284
+ assert @book.detect_autocorrelation(10)[:detected]
285
+ assert_equal [3, 4, 9, 12, 18, 14, 4, 5, 0, 1],
286
+ counts = @book.histogram(10).to_a.transpose[1]
287
+ assert_equal 70, counts.inject { |s, x| s + x }
288
+ end
289
+
290
+ def test_cover
291
+ assert @flat.cover?(@flat)
292
+ assert @flat_fuzzy2.cover?(@flat_fuzzy2)
293
+ assert @flat_fuzzy.cover?(@flat_fuzzy)
294
+ assert @flat.cover?(@flat_fuzzy)
295
+ assert_operator @flat.suggested_sample_size(@flat_fuzzy), '>', 1000
296
+ assert @flat.cover?(@flat_fuzzy2)
297
+ assert_operator @flat.suggested_sample_size(@flat_fuzzy2), '>', 9000
298
+ assert @flat_fuzzy.cover?(@flat)
299
+ assert_operator @flat_fuzzy.suggested_sample_size(@flat), '>', 1000
300
+ assert @flat_fuzzy2.cover?(@flat)
301
+ assert_operator @flat_fuzzy2.suggested_sample_size(@flat), '>', 9000
302
+ assert !@flat.cover?(@flat_higher)
303
+ assert !@flat_higher.cover?(@flat)
304
+ assert !@flat_fuzzy.cover?(@flat_higher)
305
+ assert !@flat_fuzzy2.cover?(@flat_higher)
306
+ assert !@flat_higher.cover?(@flat_fuzzy)
307
+ assert !@flat_higher.cover?(@flat_fuzzy2)
308
+ assert @flat_fuzzy.cover?(@flat_fuzzy2)
309
+ assert_operator @flat_fuzzy.suggested_sample_size(@flat_fuzzy2), '>', 4000
310
+ assert @flat_fuzzy2.cover?(@flat_fuzzy)
311
+ assert_operator @flat_fuzzy2.suggested_sample_size(@flat_fuzzy), '>', 4000
312
+ assert @rasi.cover?(@rasi_mean)
313
+ assert_operator @rasi.suggested_sample_size(@rasi_mean), '>', 10_000
314
+ assert @rasi_mean.cover?(@rasi)
315
+ assert_operator @rasi_mean.suggested_sample_size(@rasi), '>', 10_000
316
+ assert @rasi.cover?(@flat)
317
+ assert_operator @rasi.suggested_sample_size(@flat), '>', 500
318
+ assert @flat.cover?(@rasi)
319
+ assert_operator @flat.suggested_sample_size(@rasi), '>', 500
320
+ end
321
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+
6
+ class TestCantorPairingFunction < Test::Unit::TestCase
7
+ include MoreMath::Functions
8
+
9
+ def test_cantor_pairing_function
10
+ assert_equal 69, cantor_pairing([ 1, 2, 3 ])
11
+ assert_equal 69, cantor_pairing(1, 2, 3)
12
+ assert_equal 172, cantor_pairing(3, 2, 1)
13
+ assert_raise(ArgumentError) { cantor_pairing([ 1 ]) }
14
+ assert_raise(ArgumentError) { cantor_pairing(1) }
15
+ end
16
+
17
+ def test_cantor_pairing_function_inv
18
+ assert_equal [ 8, 3 ], cantor_pairing_inv(69)
19
+ assert_equal [ 1, 2, 3 ], cantor_pairing_inv(69, 3)
20
+ assert_equal [ 17, 1 ], cantor_pairing_inv(172)
21
+ assert_equal [ 3, 2, 1 ], cantor_pairing_inv(172, 3)
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+
6
+ class TestContinuedFraction < Test::Unit::TestCase
7
+ include MoreMath
8
+
9
+ def setup
10
+ @none1 = ContinuedFraction.for_a proc {}
11
+ @none2 = ContinuedFraction.for_a {}
12
+ @phi = ContinuedFraction.new
13
+ @finite = ContinuedFraction.for_a [3, 4, 12, 3, 1]
14
+ @sqrt_2 = ContinuedFraction.for_a { |n| n == 0 ? 1 : 2 }
15
+ @pi_finite = ContinuedFraction.for_a [3, 7, 15, 1, 292, 1, 1, 1, 2]
16
+ @e = ContinuedFraction.for_a { |n| (n + 1) }.for_b { |n| (n + 1) }
17
+ @a113011 = ContinuedFraction.for_a { |n| 2 * n + 1 }.for_b { |n| 2 * n }
18
+ @a073333 = ContinuedFraction.for_a { |n| n }.for_b { |n| n }
19
+ @atan = ContinuedFraction.for_a do |n, x|
20
+ n == 0 ? 0 : 2 * n - 1
21
+ end.for_b do |n, x|
22
+ n <= 1 ? x : ((n - 1) * x) ** 2
23
+ end
24
+ @pi = lambda { 4 * @atan[1] }
25
+ end
26
+
27
+ def test_continued_fractions
28
+ assert @none1[1].nan?
29
+ assert @none2[1].nan?
30
+ assert_in_delta 1.618033, @phi[], 1E-6
31
+ assert_in_delta 3.245, @finite[], 1E-4
32
+ assert_in_delta Math.sqrt(2), @sqrt_2[], 1E-10
33
+ assert_in_delta Math::PI, @pi_finite[], 1E-10
34
+ assert_in_delta Math::E, 1 + @e[], 1E-10
35
+ assert_in_delta 1.541494, @a113011[], 1E-6
36
+ assert_in_delta 0.581976, @a073333[], 1E-6
37
+ assert_in_delta Math.atan(0.5), @atan[0.5], 1E-10
38
+ assert_in_delta Math::PI, @pi[], 1E-10
39
+ end
40
+ end
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+
6
+ class TestDistribution < Test::Unit::TestCase
7
+ include MoreMath
8
+
9
+ def setup
10
+ @td23 = TDistribution.new 23
11
+ @td100 = TDistribution.new 100
12
+ @chi23 = ChiSquareDistribution.new 23
13
+ @chi100 = ChiSquareDistribution.new 100
14
+ end
15
+
16
+ def test_tdistribution
17
+ xs = [ 75, 80, 85, 90, 95, 97.5, 99, 99.5, 99.75, 99.9, 99.95 ].map { |x|
18
+ x / 100.0 }
19
+ ys = [ 0.685, 0.858, 1.060, 1.319, 1.714, 2.069, 2.500, 2.807, 3.104,
20
+ 3.485, 3.767 ]
21
+ xs.zip(ys) do |x, y|
22
+ assert_in_delta y, @td23.inverse_probability(x), 1E-2
23
+ end
24
+ assert_equal @td23.inverse_probability(-0.1), -1 / 0.0
25
+ assert_equal @td23.inverse_probability(1.1), 1 / 0.0
26
+ ys = [ 0.677, 0.845, 1.042, 1.290, 1.660, 1.984, 2.364, 2.626, 2.871,
27
+ 3.174, 3.390 ]
28
+ xs.zip(ys) do |x, y|
29
+ assert_in_delta y, @td100.inverse_probability(x), 1E-2
30
+ end
31
+ assert_equal @td100.inverse_probability(-0.1), -1 / 0.0
32
+ assert_equal @td100.inverse_probability(1.1), 1 / 0.0
33
+ end
34
+
35
+ def test_standard_normal_distribution
36
+ std = STD_NORMAL_DISTRIBUTION
37
+ ps = [ 0.001, 0.005, 0.010, 0.025, 0.050, 0.100 ]
38
+ zs = [ -3.090, -2.576, -2.326, -1.960, -1.645, -1.282 ]
39
+ ps.zip(zs) do |p, z|
40
+ assert_in_delta z, std.inverse_probability(p), 1E-2
41
+ end
42
+ assert_equal std.inverse_probability(-0.1), -1 / 0.0
43
+ assert_equal std.inverse_probability(1.1), 1 / 0.0
44
+ ps.zip(zs) do |p, z|
45
+ assert_in_delta(-z, std.inverse_probability(1 - p), 1E-2)
46
+ end
47
+ end
48
+
49
+ def test_chisquaredistribution
50
+ xs = [ 0.0, 1, 9.260, 11.689, 28.429, 32.007, 35.172, 38.076, 38.968, 41.638,
51
+ 44.181, 47.391, 49.728 ]
52
+ ys = [ 0.0, 1.593887e-12, 0.004998304, 0.025006129, 0.800007427, 0.900002084, 0.949994698,
53
+ 0.975002306, 0.979998428, 0.989998939, 0.994999617, 0.997999729,
54
+ 0.998999930 ]
55
+ xs.zip(ys) do |x, y|
56
+ assert_in_delta y, @chi23.probability(x), 1E-6
57
+ assert_in_delta x, @chi23.inverse_probability(y), 1E-6
58
+ end
59
+ assert_in_delta @chi23.probability(-0.1), 0.0, 1E-6
60
+ ys = [ 0.0, 1.788777e-80, 6.705663e-34, 2.337654e-29, 1.321036e-13, 8.695875e-12,
61
+ 2.085529e-10, 2.691731e-09, 5.559949e-09, 4.192445e-08, 2.373762e-07,
62
+ 1.678744e-06, 6.034997e-06 ]
63
+ xs.zip(ys) do |x, y|
64
+ assert_in_delta y, @chi100.probability(x), 1E-6
65
+ assert_in_delta x, @chi100.inverse_probability(y), 1E-6
66
+ end
67
+ assert_in_delta @chi100.probability(-0.1), 0.0, 1E-6
68
+ end
69
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+
6
+ class TestFunctions < Test::Unit::TestCase
7
+ include MoreMath::Functions
8
+
9
+ def gammaP5_2(x)
10
+ gammaP_regularized(x, 5 / 2.0)
11
+ end
12
+
13
+ def gammaQ5_2(x)
14
+ gammaQ_regularized(x, 5 / 2.0)
15
+ end
16
+
17
+ X1 = [ 0.0, 0.1, 0.5, 0.9, 0.95, 0.995, 1.0, 2, 3, 5, 10, 100 ]
18
+
19
+ Y1 = [ 0.0000000000, 0.0008861388, 0.0374342268, 0.1239315997, 0.1371982774,
20
+ 0.1494730091, 0.1508549639, 0.4505840486, 0.6937810816, 0.9247647539,
21
+ 0.9987502694, 1.0000000000 ]
22
+
23
+ def test_gammaPQ
24
+ assert gammaQ_regularized(1, -1).nan?
25
+ assert gammaP_regularized(1, -1).nan?
26
+ assert gammaP5_2(-1).nan?
27
+ assert gammaQ5_2(-1).nan?
28
+ X1.zip(Y1) do |x, y|
29
+ assert_in_delta y, gammaP5_2(x), 1E-10
30
+ assert_in_delta y, 1 - gammaQ5_2(x), 1E-10
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+ require 'stringio'
6
+
7
+ class TestHistogram < Test::Unit::TestCase
8
+ include MoreMath
9
+
10
+ def test_histogram
11
+ sequence = Sequence.new [ 1, 2, 3, 0, 2 ]
12
+ histogram = Histogram.new sequence, 3
13
+ assert_equal [ [0.0, 2, 1.0], [1.0, 2, 2.0], [2.0, 1, 3.0] ],
14
+ histogram.to_a
15
+ end
16
+
17
+ def test_histogram_display
18
+ sequence = Sequence.new [ 1, 2, 3, 0, 2 ]
19
+ histogram = Histogram.new sequence, 3
20
+ assert_equal [[2.0, 25, 3.0], [1.0, 50, 2.0], [0.0, 50, 1.0]],
21
+ histogram.instance_eval { prepare_display(50) }
22
+ output = StringIO.new
23
+ histogram.display output
24
+ output_expected =
25
+ " 2.50000 -|*************************\n 1.50000 -|*******************************"\
26
+ "*******************\n 0.50000 -|**************************************************\n"
27
+ assert_equal output_expected, output.string
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'more_math'
5
+
6
+ class TestNewtonBisection < Test::Unit::TestCase
7
+ include MoreMath
8
+
9
+ def test_bracket
10
+ solver = NewtonBisection.new { |x| x ** 2 - 3 }
11
+ assert_raises(ArgumentError) { solver.bracket(1..1) }
12
+ assert_raises(ArgumentError) { solver.bracket(1..-1) }
13
+ range = solver.bracket(0..0.1)
14
+ assert_in_delta range.first, 0, 1E-6
15
+ assert_in_delta range.last, 1.7576, 1E-6
16
+ range = solver.bracket(2..3)
17
+ assert_in_delta range.first, 0.4, 1E-6
18
+ assert_in_delta range.last, 3, 1E-6
19
+ end
20
+
21
+ def test_zero
22
+ solver = NewtonBisection.new { |x| x ** 2 - 3 }
23
+ assert_in_delta 1.73205, solver.solve, 1E-6
24
+ assert_in_delta(-1.73205, solver.solve(-5..-1), 1E-6)
25
+ assert_in_delta 1.73205, solver.solve(solver.bracket(0..0.1)), 1E-6
26
+ assert_in_delta 1.73205, solver.solve(solver.bracket(2..3)), 1E-6
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: more_math
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Florian Frank
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-01 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: dslkit
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 2
30
+ version: "0.2"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Library that provides more mathematical functions/algorithms than standard Ruby.
34
+ email: flori@ping.de
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ files:
42
+ - CHANGES
43
+ - VERSION
44
+ - README
45
+ - make_doc.rb
46
+ - Rakefile
47
+ - lib/more_math/cantor_pairing_funtion.rb
48
+ - lib/more_math/functions.rb
49
+ - lib/more_math/distributions.rb
50
+ - lib/more_math/newton_bisection.rb
51
+ - lib/more_math/constants/functions_constants.rb
52
+ - lib/more_math/version.rb
53
+ - lib/more_math/sequence.rb
54
+ - lib/more_math/linear_regression.rb
55
+ - lib/more_math/histogram.rb
56
+ - lib/more_math/continued_fraction.rb
57
+ - lib/more_math/exceptions.rb
58
+ - lib/more_math.rb
59
+ - tests/test_continued_fraction.rb
60
+ - tests/test_functions.rb
61
+ - tests/test_analysis.rb
62
+ - tests/test_histogram.rb
63
+ - tests/test_distribution.rb
64
+ - tests/test_cantor_pairing_function.rb
65
+ - tests/test_newton_bisection.rb
66
+ - install.rb
67
+ - LICENSE
68
+ has_rdoc: true
69
+ homepage: http://flori.github.com/more_math
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --title
75
+ - MoreMath -- More Math in Ruby
76
+ - --main
77
+ - README
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project: more_math
97
+ rubygems_version: 1.3.6
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Library that provides more mathematics.
101
+ test_files:
102
+ - tests/test_continued_fraction.rb
103
+ - tests/test_functions.rb
104
+ - tests/test_analysis.rb
105
+ - tests/test_histogram.rb
106
+ - tests/test_distribution.rb
107
+ - tests/test_cantor_pairing_function.rb
108
+ - tests/test_newton_bisection.rb