polynomial 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +176 -0
- data/License.txt +20 -0
- data/Manifest.txt +34 -0
- data/README.txt +72 -0
- data/lib/polynomial/chebyshev.rb +95 -0
- data/lib/polynomial/legendre.rb +57 -0
- data/lib/polynomial/multivariate.rb +143 -0
- data/lib/polynomial/ultraspherical.rb +47 -0
- data/lib/polynomial/version.rb +9 -0
- data/lib/polynomial.rb +532 -0
- data/test/test_chebyshev.rb +103 -0
- data/test/test_helper.rb +6 -0
- data/test/test_legendre.rb +53 -0
- data/test/test_multivariate.rb +469 -0
- data/test/test_polynomial.rb +404 -0
- data/test/test_suite.rb +16 -0
- data/test/test_ultraspherical.rb +45 -0
- data/website/index.txt +57 -0
- metadata +127 -0
@@ -0,0 +1,404 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
require 'complex'
|
4
|
+
require 'bigdecimal'
|
5
|
+
require 'polynomial'
|
6
|
+
|
7
|
+
Poly = Polynomial
|
8
|
+
|
9
|
+
class TestPoly < Test::Unit::TestCase
|
10
|
+
|
11
|
+
@@epsilon = 1e-7
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@one = Poly::Unity
|
15
|
+
@zero = Poly::Zero
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_incorrect_initialization
|
19
|
+
assert_raise(ArgumentError) { Poly.new }
|
20
|
+
assert_raise(ArgumentError) { Poly.new [] }
|
21
|
+
assert_raise(TypeError) { Poly.new :oops }
|
22
|
+
assert_raise(TypeError) { Poly.new ['foo'] }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_new_from_coefficients
|
26
|
+
assert_nothing_raised { Poly.new 0 }
|
27
|
+
assert_nothing_raised { Poly.new [0] }
|
28
|
+
assert_nothing_raised { Poly.new [1,2] }
|
29
|
+
assert_nothing_raised { Poly.new [1, 2.2] }
|
30
|
+
assert_nothing_raised { Poly.new [1, Complex(0,1)] }
|
31
|
+
assert_nothing_raised { Poly.new [1, BigDecimal('1e-2')] }
|
32
|
+
assert_nothing_raised { Poly.new 1,2 }
|
33
|
+
assert_equal Poly.new(1,2), Poly.new([1,2])
|
34
|
+
assert_equal Poly.new(1), Poly.new(1,0)
|
35
|
+
assert_equal Poly.new(1), Poly.new([1,0,0])
|
36
|
+
assert_equal Poly.new(1,2,3), Poly[1,2,3]
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_new_from_block
|
40
|
+
assert_equal Poly.new(0,1,2), Poly.new(2) {|n| n }
|
41
|
+
assert_equal Poly.new(1,2,3), Poly.new(2) {|n| n+1 }
|
42
|
+
assert_equal Poly.new(0,1,4,9), Poly.new(3) {|n| n**2 }
|
43
|
+
assert_equal Poly.new(3.2), Poly.new(3.2) {|n| n**2 }
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_new_from_power_coefficients_hash
|
47
|
+
assert_equal Poly[0,2], Poly.new({1=>2})
|
48
|
+
assert_equal Poly[0,0,3], Poly.new({2=>3})
|
49
|
+
assert_equal Poly[0,2,3], Poly[1=>2, 2=>3]
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_new_from_valid_strings
|
53
|
+
assert_equal Poly[0], Poly['0']
|
54
|
+
assert_equal Poly[1], Poly[' 1 ']
|
55
|
+
assert_equal Poly[-1], Poly['-1']
|
56
|
+
assert_equal Poly[-1.3], Poly['-1.3']
|
57
|
+
assert_equal Poly[0,1], Poly['x']
|
58
|
+
assert_equal Poly[0,1], Poly['+x']
|
59
|
+
assert_equal Poly[0,1], Poly['+1x', {:multiplication_symbol=>''}]
|
60
|
+
assert_equal Poly[1,1], Poly['1+x']
|
61
|
+
assert_equal Poly[1,1], Poly['1+1x', {:multiplication_symbol=>''}]
|
62
|
+
assert_equal Poly[0,0,3], Poly['3*x**2']
|
63
|
+
assert_equal Poly[0,0,1], Poly['x**2']
|
64
|
+
assert_equal Poly[3,0,1], Poly['3+x**2']
|
65
|
+
assert_equal Poly[3,0,1], Poly['3+x^2', {:power_symbol=>'^'}]
|
66
|
+
assert_equal Poly[3,2], Poly['3+2*x']
|
67
|
+
assert_equal Poly[2,1], Poly['2+y', {:variable_name=>'y'}]
|
68
|
+
assert_equal Poly['4*x**2+2'], Poly[2,0,4]
|
69
|
+
assert_equal Poly[' 4*x**2 + 2 '], Poly[2,0,4]
|
70
|
+
assert_equal Poly[1,0,-7], Poly.from_string("1 - 7*x**2")
|
71
|
+
assert_equal Poly[3,4.1,5], Poly.from_string("3 + 4.1x + 5x^2", :multiplication_symbol=>'', :power_symbol=>'^')
|
72
|
+
assert_equal Poly[0,-3], Poly.from_string("-3*y", :variable_name=>'y')
|
73
|
+
assert_equal Poly[1e-2], Poly.from_string('1e-2')
|
74
|
+
assert_equal Poly[0,1e-2], Poly.from_string('1e-2*x')
|
75
|
+
assert_equal Poly[BigDecimal('1.1')], Poly.from_string(BigDecimal('1.1').to_s)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_from_invalid_strings_misc
|
79
|
+
assert_raise(ArgumentError) { Poly[''] }
|
80
|
+
assert_raise(ArgumentError) { Poly['1..'] }
|
81
|
+
assert_raise(ArgumentError) { Poly['1..1'] }
|
82
|
+
assert_raise(ArgumentError) { Poly['+'] }
|
83
|
+
assert_raise(ArgumentError) { Poly['++1'] }
|
84
|
+
assert_raise(ArgumentError) { Poly['-+1'] }
|
85
|
+
assert_raise(ArgumentError) { Poly['--1'] }
|
86
|
+
assert_raise(ArgumentError) { Poly['1-'] }
|
87
|
+
assert_raise(ArgumentError) { Poly['1.2-'] }
|
88
|
+
assert_raise(ArgumentError) { Poly['xx'] }
|
89
|
+
assert_raise(ArgumentError) { Poly['xx**2'] }
|
90
|
+
assert_raise(ArgumentError) { Poly['x*x'] }
|
91
|
+
assert_raise(ArgumentError) { Poly['x**x'] }
|
92
|
+
assert_raise(ArgumentError) { Poly['2**3'] }
|
93
|
+
assert_raise(ArgumentError) { Poly['x**3*4'] }
|
94
|
+
assert_raise(ArgumentError) { Poly['e2'] }
|
95
|
+
assert_raise(ArgumentError) { Poly['1e--2'] }
|
96
|
+
assert_raise(ArgumentError) { Poly['1ee2'] }
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_from_invalid_strings_incomplete
|
100
|
+
assert_raise(ArgumentError) { Poly['1+'] }
|
101
|
+
assert_raise(ArgumentError) { Poly['1.2+'] }
|
102
|
+
assert_raise(ArgumentError) { Poly['1*'] }
|
103
|
+
assert_raise(ArgumentError) { Poly['1.2*'] }
|
104
|
+
assert_raise(ArgumentError) { Poly['2*x**'] }
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_from_invalid_strings_symbol_omission
|
108
|
+
assert_raise(ArgumentError) { Poly['1x'] }
|
109
|
+
assert_raise(ArgumentError) { Poly['1.2x'] }
|
110
|
+
assert_raise(ArgumentError) { Poly['2*x3'] }
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_from_invalid_strings_spaces
|
114
|
+
assert_raise(ArgumentError) { Poly['1 * x'] }
|
115
|
+
assert_raise(ArgumentError) { Poly['1 *x'] }
|
116
|
+
assert_raise(ArgumentError) { Poly['1* x'] }
|
117
|
+
assert_raise(ArgumentError) { Poly['1*x ** 2'] }
|
118
|
+
assert_raise(ArgumentError) { Poly['1*x **2'] }
|
119
|
+
assert_raise(ArgumentError) { Poly['1*x** 2'] }
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_from_string_repeated
|
123
|
+
assert_equal Poly['1+2'], Poly[3]
|
124
|
+
assert_equal Poly['x+3*x'], Poly[0,4]
|
125
|
+
assert_equal Poly['1+x+x**2+2*x**2'], Poly[1,1,3]
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_substitute
|
129
|
+
poly = Poly[0]
|
130
|
+
pairs = [[0,0], [1,0], [-1,0], [2,0], [0.33, 0]]
|
131
|
+
assert_in_out_pairs(poly, pairs)
|
132
|
+
|
133
|
+
poly = Poly[0, 1]
|
134
|
+
pairs = [[0,0], [1,1], [-1,-1], [2,2], [0.33, 0.33]]
|
135
|
+
assert_in_out_pairs(poly, pairs)
|
136
|
+
|
137
|
+
poly = Poly[0, 0, 1]
|
138
|
+
pairs = [[0,0], [1,1], [-1,1], [2,4], [0.33, 0.33**2]]
|
139
|
+
assert_in_out_pairs(poly, pairs)
|
140
|
+
|
141
|
+
poly = Poly[1, 1]
|
142
|
+
pairs = [[0,1], [1,2], [-1,0], [2,3], [0.33, 1.33]]
|
143
|
+
assert_in_out_pairs(poly, pairs)
|
144
|
+
|
145
|
+
poly = Poly[1, -1]
|
146
|
+
pairs = [[0,1], [1,0], [-1,2], [2,-1], [0.33, 1-0.33]]
|
147
|
+
assert_in_out_pairs(poly, pairs)
|
148
|
+
|
149
|
+
poly = Poly[Complex(0,1), 1]
|
150
|
+
pairs = [[0,Complex(0,1)], [1,Complex(1,1)], [-1,Complex(-1,1)], [2,Complex(2,1)], [0.33, Complex(0.33,1)]]
|
151
|
+
assert_in_out_pairs(poly, pairs)
|
152
|
+
|
153
|
+
poly = Poly[BigDecimal('0'), BigDecimal('1.11')]
|
154
|
+
pairs = [[0,BigDecimal('0')], [1,BigDecimal('1.11')], [-1,BigDecimal('-1.11')], [2,BigDecimal('2.22')], [BigDecimal('0.33'), BigDecimal('0.3663')]]
|
155
|
+
assert_in_out_pairs(poly, pairs)
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_degree
|
159
|
+
assert_equal 0, Poly[0].degree
|
160
|
+
assert_equal 0, Poly[1].degree
|
161
|
+
assert_equal 1, Poly[1,2].degree
|
162
|
+
assert_equal 1, Poly[0,-1].degree
|
163
|
+
assert_equal 2, Poly[1,0,1].degree
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_multiplication
|
167
|
+
assert_equal Poly[0], Poly[1] * 0
|
168
|
+
assert_equal Poly[0], 0 * Poly[1]
|
169
|
+
assert_equal Poly[1], Poly[1] * 1
|
170
|
+
assert_equal Poly[1], 1 * Poly[1]
|
171
|
+
assert_equal Poly[2], Poly[1] * 2
|
172
|
+
assert_equal Poly[2], Poly[1] * Poly[2]
|
173
|
+
assert_equal Poly[2,4,6], Poly[1,2,3] * 2
|
174
|
+
assert_equal Poly[2,4,6], 2 * Poly[1,2,3]
|
175
|
+
assert_equal Poly[0,0,1,2], Poly[0,1,2] * Poly[0,1]
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_division_by_numeric
|
179
|
+
assert_equal Poly[0], Poly[0] / 1
|
180
|
+
assert_equal Poly[1], Poly[1] / 1
|
181
|
+
assert_equal Poly[0.5], Poly[1] / 2
|
182
|
+
assert_equal Poly[0.5], Poly[1] / 2.0
|
183
|
+
assert_equal Poly[1,2,3], Poly[2,4,6] / 2
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_divmod
|
187
|
+
assert_equal [Poly[3], Poly[-8,-4]], Poly[1,2,3].divmod(Poly[3,2,1])
|
188
|
+
assert_equal [Poly[0], Poly[1,2]], Poly[1,2].divmod(Poly[1,2,3])
|
189
|
+
assert_equal [Poly[-27,-9,1], Poly[-123]], Poly[-42,0,-12,1].divmod(Poly[-3,1])
|
190
|
+
assert_equal [Poly[4,6], Poly[2]], Poly[2,4,6].divmod(Poly[0,1])
|
191
|
+
assert_equal [Poly[1,2,3], 0], Poly[2,4,6].divmod(2)
|
192
|
+
assert_raise(ArgumentError) { Poly[1,2,3].divmod(:foo) }
|
193
|
+
assert_raise(ArgumentError) { Poly[1,2,3].divmod(nil) }
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_division_by_polynomial
|
197
|
+
assert_equal Poly[2], Poly[2,4,6] / Poly[1,2,3]
|
198
|
+
assert_equal Poly[0.5], Poly[1,2,3] / Poly[2,4,6]
|
199
|
+
assert_equal Poly[3], Poly[1,2,3] / Poly[3,2,1]
|
200
|
+
assert_equal Poly[1,2,3], Poly[2,4,6] / Poly[2]
|
201
|
+
end
|
202
|
+
|
203
|
+
@@quomod_data = [
|
204
|
+
[Poly[1,2,3], Poly[3,2,1], Poly[3], Poly[-8,-4]],
|
205
|
+
[Poly[1,2], Poly[1,2,3], Poly[0], Poly[1,2]],
|
206
|
+
[Poly[-42,0,-12,1], Poly[-3,1], Poly[-27,-9,1], Poly[-123]],
|
207
|
+
[Poly[1,3.quo(2)], 2, Poly[1.quo(2), 3.quo(4)], 0],
|
208
|
+
[Poly[2,4,6], Poly[0,3], Poly[4.quo(3),6.quo(3)], Poly[2]],
|
209
|
+
[Poly[1,3.quo(2)], Poly[2], Poly[1.quo(2), 3.quo(4)], 0],
|
210
|
+
[Poly[2,4,6], 2, Poly[1,2,3], 0],
|
211
|
+
]
|
212
|
+
def test_quomod
|
213
|
+
@@quomod_data.each do |dividend, divisor, quotient, rest|
|
214
|
+
dividend_bak = dividend.dup
|
215
|
+
divisor_bak = begin divisor.dup rescue divisor end
|
216
|
+
assert_equal [quotient, rest], dividend.quomod(divisor)
|
217
|
+
assert_equal dividend_bak, dividend
|
218
|
+
assert_equal divisor_bak, divisor
|
219
|
+
end
|
220
|
+
assert_raise(ArgumentError) { Poly[1,2,3].quomod(:foo) }
|
221
|
+
assert_raise(ArgumentError) { Poly[1,2,3].quomod(nil) }
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_quo_by_numeric
|
225
|
+
assert_equal Poly[1.quo(2), 3.quo(4)], Poly[1,3.quo(2)].quo(2)
|
226
|
+
assert_equal Poly[1/2.0, 3.quo(2)/2.0], Poly[1,3.quo(2)].quo(2.0)
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_quo_by_polynomial
|
230
|
+
assert_equal Poly[4.quo(3),6.quo(3)], Poly[2,4,6].quo(Poly[0,3])
|
231
|
+
assert_equal Poly[1.quo(2), 3.quo(4)], Poly[1,3.quo(2)].quo(2)
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_remainder
|
235
|
+
@@quomod_data.each do |dividend, divisor, quotient, rest|
|
236
|
+
assert_equal rest, dividend % divisor
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
@@power_data = [
|
241
|
+
[Poly[1], 0, Poly[1]],
|
242
|
+
[Poly[1], 1, Poly[1]],
|
243
|
+
[Poly[1], 2, Poly[1]],
|
244
|
+
[Poly[0,1], 1, Poly[0,1]],
|
245
|
+
[Poly[0,1], 2, Poly[0,0,1]],
|
246
|
+
[Poly[3,4], 2, Poly[9,24,16]],
|
247
|
+
]
|
248
|
+
def test_power
|
249
|
+
@@power_data.each do |polynomial, power, result|
|
250
|
+
assert_equal result, polynomial**power
|
251
|
+
end
|
252
|
+
assert_raise(ArgumentError) { Poly[1,2,3]**(-1) }
|
253
|
+
assert_raise(NoMethodError) { Poly[1,2,3]**(1.1) }
|
254
|
+
# Exceptions raised in Ruby 1.8 and 1.9 are different for the following
|
255
|
+
assert_raise(ArgumentError, NoMethodError) { Poly[1,2,3]**(:foo) }
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_addition
|
259
|
+
assert_equal Poly[1], Poly[1] + 0
|
260
|
+
assert_equal Poly[2], Poly[1] + 1
|
261
|
+
assert_equal Poly[1,1], Poly[1] + Poly[0,1]
|
262
|
+
assert_equal Poly[2,3,4], Poly[2] + Poly[0,3,4]
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_subtraction
|
266
|
+
assert_equal Poly[1], Poly[1] - 0
|
267
|
+
assert_equal Poly[0], Poly[1] - 1
|
268
|
+
assert_equal Poly[1,-1], Poly[1] - Poly[0,1]
|
269
|
+
assert_equal Poly[2,-3,-4], Poly[2] - Poly[0,3,4]
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_to_s
|
273
|
+
assert_equal "0", Poly[0].to_s
|
274
|
+
assert_equal "1", Poly[1].to_s
|
275
|
+
assert_equal "x", Poly[0,1].to_s
|
276
|
+
assert_equal "1 + x", Poly[1,Complex(1,0)].to_s
|
277
|
+
assert_equal "0 + x", Poly[0,1].to_s(:verbose=>true)
|
278
|
+
assert_equal "1 - 2*x + 3*x**2", Poly[1,-2,3].to_s
|
279
|
+
assert_equal "3*x**2 - 2*x + 1", Poly[1,-2,3].to_s(:decreasing=>true)
|
280
|
+
assert_equal "1 + 0*x + 3*x**2", Poly[1,0,3].to_s(:verbose=>true)
|
281
|
+
assert_equal "-1 - 3*x^2", Poly[-1,0,-3].to_s(:power_symbol=>'^')
|
282
|
+
assert_equal "-1*y - 3*y**2", Poly[0,-1,-3].to_s(:variable_name=>'y')
|
283
|
+
assert_equal "0+x", Poly[0,1].to_s(:verbose=>true, :spaced=>false)
|
284
|
+
assert_equal "1-2*x+3*x**2", Poly[1,-2,3].to_s(:spaced=>false)
|
285
|
+
assert_equal "1+0*x+3*x**2", Poly[1,0,3].to_s(:verbose=>true, :spaced=>false)
|
286
|
+
assert_equal "-1-3*x^2", Poly[-1,0,-3].to_s(:power_symbol=>'^', :spaced=>false)
|
287
|
+
|
288
|
+
case RUBY_VERSION # Complex#to_s results in Ruby 1.8 and 1.9 are different
|
289
|
+
when /^1[.]9/
|
290
|
+
assert_equal "1 + (0 + 1i)*x", Poly[1,Complex(0,1)].to_s
|
291
|
+
else
|
292
|
+
assert_equal "1 + 1i*x", Poly[1,Complex(0,1)].to_s
|
293
|
+
end
|
294
|
+
|
295
|
+
assert_equal "1 + (1 + 1i)*x", Poly[1,Complex(1,1)].to_s
|
296
|
+
assert_equal "1 + x", Poly[1,Complex(1,0)].to_s
|
297
|
+
assert_equal "-3.33694235763727e-05", Poly[-3.33694235763727e-05].to_s
|
298
|
+
assert_equal "1 + 3.33694235763727e-05*x", Poly[1,3.33694235763727e-05].to_s
|
299
|
+
assert_equal "1 + (2/3)*x", Poly[1,2.quo(3)].to_s
|
300
|
+
assert_equal "(-1/2) + (3/2)*x**2", Poly[-1.quo(2), 0, 3.quo(2)].to_s
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_to_i
|
304
|
+
assert_raise(ArgumentError) { Poly[1,2].to_i }
|
305
|
+
assert_equal 0, Poly[0].to_i
|
306
|
+
assert_equal 3, Poly[3].to_i
|
307
|
+
assert_equal(-2, Poly[-2].to_i)
|
308
|
+
assert_equal 3, Poly[3.3].to_i
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_to_f
|
312
|
+
assert_raise(ArgumentError) { Poly[1,2].to_f }
|
313
|
+
assert_equal 0.0, Poly[0].to_f
|
314
|
+
assert_equal 3.1, Poly[3.1].to_f
|
315
|
+
assert_equal(-2.2, Poly[-2.2].to_f)
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_integral
|
319
|
+
assert_equal Poly[0], Poly[0].integral
|
320
|
+
assert_equal Poly[0,1], Poly[1].integral
|
321
|
+
assert_equal Poly[0,1], Poly[1].integral
|
322
|
+
assert_equal Poly[0,1,1/2.0], Poly[1,1].integral
|
323
|
+
assert_equal Poly[0,1,1/2.0,1/3.0], Poly[1,1,1].integral
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_derivative
|
327
|
+
assert_equal Poly[0], Poly[0].derivative
|
328
|
+
assert_equal Poly[0], Poly[1].derivative
|
329
|
+
assert_equal Poly[0], Poly[2].derivative
|
330
|
+
assert_equal Poly[1], Poly[0,1].derivative
|
331
|
+
assert_equal Poly[1,2], Poly[1,1,1].derivative
|
332
|
+
end
|
333
|
+
|
334
|
+
def test_derivatives
|
335
|
+
z = Poly[0]
|
336
|
+
assert_equal [z], z.derivatives
|
337
|
+
assert_equal [Poly[1], z], Poly[0,1].derivatives
|
338
|
+
assert_equal [Poly[2], z], Poly[1,2].derivatives
|
339
|
+
assert_equal [Poly[4,10], Poly[10], z], Poly[3,4,5].derivatives
|
340
|
+
assert_equal [Poly[2,6,12], Poly[6,24], Poly[24], z], Poly[1,2,3,4].derivatives
|
341
|
+
end
|
342
|
+
|
343
|
+
def test_integral_derivative_cascading
|
344
|
+
coefs = Array.new(16) { rand }
|
345
|
+
assert Poly[coefs].equal_in_delta Poly[coefs].integral.derivative, @@epsilon
|
346
|
+
assert Poly[0, *coefs[1..-1]].equal_in_delta Poly[coefs].derivative.integral, @@epsilon
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_equal_in_delta
|
350
|
+
assert Poly[0].equal_in_delta(Poly[@@epsilon], @@epsilon)
|
351
|
+
assert Poly[1].equal_in_delta(Poly[1-@@epsilon], @@epsilon)
|
352
|
+
assert Poly[-@@epsilon,@@epsilon].equal_in_delta(Poly[@@epsilon, -@@epsilon], 2*@@epsilon)
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_derivative_integral
|
356
|
+
[ [0], [1], [0, 42], [1,11], [3,4,5] ].each do |coefs|
|
357
|
+
poly = Poly.new(coefs)
|
358
|
+
assert poly.equal_in_delta(poly.integral.derivative, @@epsilon)
|
359
|
+
end
|
360
|
+
1.upto(10) do |n|
|
361
|
+
poly = Poly.new( Array.new(n) {|m| 2*m + rand } )
|
362
|
+
assert poly.equal_in_delta(poly.integral.derivative, @@epsilon)
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_compare
|
367
|
+
a = Poly[2]
|
368
|
+
b = Poly[2,1]
|
369
|
+
c = Poly[2,2]
|
370
|
+
d = Poly[2,1,2]
|
371
|
+
assert_equal [a,b,c,d], [d,b,c,a].sort
|
372
|
+
assert_equal 0, Poly[0]
|
373
|
+
assert_equal Poly[1], 1
|
374
|
+
assert_equal Poly[0.5], 0.5
|
375
|
+
assert_equal 0.5, Poly[0.5]
|
376
|
+
assert_not_equal 1, Poly[1,2]
|
377
|
+
assert_raise(TypeError) { Poly[1] <=> 'foo' }
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_coerce
|
381
|
+
assert_equal [Poly[2],Poly[1]], Poly[1].coerce(2)
|
382
|
+
assert_equal [Poly[2],Poly[1]], Poly[1].coerce(Poly[2])
|
383
|
+
assert_raise(TypeError) { Poly[1].coerce('foo') }
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_unity_and_zero
|
387
|
+
assert_equal @one, @one*@one
|
388
|
+
assert_not_equal @zero, @one
|
389
|
+
assert_equal @zero, @one*@zero
|
390
|
+
q = Poly.new(2,3,5,7)
|
391
|
+
assert_equal q, @one*q
|
392
|
+
assert_equal @zero, q*@zero
|
393
|
+
assert @zero.zero?
|
394
|
+
end
|
395
|
+
|
396
|
+
# HELPER FUNCTIONS
|
397
|
+
|
398
|
+
def assert_in_out_pairs(polynomial, input_output_pairs, delta=@@epsilon)
|
399
|
+
input_output_pairs.each do |x, y|
|
400
|
+
assert_equal y, polynomial.substitute(x)
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
end
|
data/test/test_suite.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class String
|
4
|
+
def same_file?(other)
|
5
|
+
File.expand_path(self) == File.expand_path(other)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
candidates = Dir.glob('test_*.rb')
|
10
|
+
to_exclude = [__FILE__, 'test_helper.rb']
|
11
|
+
|
12
|
+
test_files = candidates.reject {|f| to_exclude.any? {|ex| f.same_file?(ex) } }
|
13
|
+
|
14
|
+
test_files.each do |tf|
|
15
|
+
system "ruby #{tf}"
|
16
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
require 'polynomial/ultraspherical'
|
4
|
+
|
5
|
+
class TestUltraspherical < Test::Unit::TestCase
|
6
|
+
|
7
|
+
@@epsilon = 1e-12
|
8
|
+
|
9
|
+
def test_invalid_range
|
10
|
+
assert_raise(RangeError) { Polynomial::Ultraspherical.us(-3,-3) }
|
11
|
+
assert_raise(RangeError) { Polynomial::Ultraspherical.us(-2,1) }
|
12
|
+
assert_raise(RangeError) { Polynomial::Ultraspherical.us(-1,0) }
|
13
|
+
assert_raise(RangeError) { Polynomial::Ultraspherical.us(2,-1) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_ok_range
|
17
|
+
assert_nothing_raised { Polynomial::Ultraspherical.us(1,-0.5) }
|
18
|
+
assert_nothing_raised { Polynomial::Ultraspherical.us(2,-0.99) }
|
19
|
+
assert_nothing_raised { Polynomial::Ultraspherical.us(3,0.1) }
|
20
|
+
assert_nothing_raised { Polynomial::Ultraspherical.us(0,-3) }
|
21
|
+
assert_nothing_raised { Polynomial::Ultraspherical.us(7,1) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_coefficients
|
25
|
+
in_out = {
|
26
|
+
[0,3] => [1],
|
27
|
+
[1,3] => [0,6],
|
28
|
+
[2,3] => [-3,0,24],
|
29
|
+
[3,3] => [0,-24,0,80],
|
30
|
+
[4,3] => [6, 0, -120, 0, 240],
|
31
|
+
[5,2] => [0, 24, 0, -160, 0, 192],
|
32
|
+
[6,1] => [-1, 0, 24, 0, -80, 0, 64],
|
33
|
+
[2,-0.5] => [0.5, 0, -0.5],
|
34
|
+
[4,0] => [1, 0, -8, 0, 8],
|
35
|
+
[4,1.quo(2)] => [Rational(3, 8), Rational(0, 1), Rational(-15, 4), Rational(0, 1), Rational(35, 8)],
|
36
|
+
[4,0.5] => [Rational(3, 8), Rational(0, 1), Rational(-15, 4), Rational(0, 1), Rational(35, 8)],
|
37
|
+
[4,1] => [1, 0, -12, 0, 16],
|
38
|
+
}
|
39
|
+
in_out.each_pair do |args, coefs|
|
40
|
+
assert_equal Polynomial[*coefs], Polynomial::Ultraspherical.us(*args)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/website/index.txt
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
h1. Polynomials in Ruby
|
2
|
+
|
3
|
+
h1. polynomial
|
4
|
+
|
5
|
+
|
6
|
+
h2. What
|
7
|
+
|
8
|
+
Rich-featured single and multiple variables polynomials classes for Ruby.
|
9
|
+
|
10
|
+
h2. Installing
|
11
|
+
|
12
|
+
<pre syntax="ruby">sudo gem install polynomial</pre>
|
13
|
+
|
14
|
+
h2. The basics
|
15
|
+
|
16
|
+
<pre syntax="ruby">Polynomial.new(1, 2) #=> #<Polynomial:0x7f25286cea68 @coefs=[1, 2]></pre>
|
17
|
+
|
18
|
+
h2. Demonstration of usage
|
19
|
+
|
20
|
+
<pre syntax="ruby">
|
21
|
+
foo = Polynomial.new(1, 2) #=> #<Polynomial:0x7f25286cea68 @coefs=[1, 2]>
|
22
|
+
foo.to_s #=> "1 + 2*x"
|
23
|
+
foo.plot # open gnuplot window with 'foo' plotted (require EasyPlot gem)
|
24
|
+
|
25
|
+
bar = Polynomial.new(3) {|n| n+1 } #=> #<Polynomial:0x7f25287461a8 @coefs=[1, 2, 3, 4]>
|
26
|
+
bar.to_s(:power_symbol=>'^') #=> "1 + 2*x + 3*x^2 + 4*x^3"
|
27
|
+
|
28
|
+
require 'rational'
|
29
|
+
baz = bar.quo(foo) #=> #<Polynomial:0x7f00a1ff0570 @coefs=[Rational(3, 4), Rational(1, 2), Rational(2, 1)]>
|
30
|
+
baz.to_s #=> "(3/4) + (1/2)*x"
|
31
|
+
</pre>
|
32
|
+
|
33
|
+
h2. How to submit patches
|
34
|
+
|
35
|
+
Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
|
36
|
+
|
37
|
+
You can fetch the source from:
|
38
|
+
|
39
|
+
* github: "http://github.com/adrianomitre/Polynomial/tree/master":http://github.com/adrianomitre/Polynomial/tree/master
|
40
|
+
|
41
|
+
<pre>git clone git@github.com:adrianomitre/Polynomial.git</pre>
|
42
|
+
|
43
|
+
h3. Build and test instructions
|
44
|
+
|
45
|
+
<pre>cd polynomial
|
46
|
+
rake test
|
47
|
+
rake install_gem</pre>
|
48
|
+
|
49
|
+
|
50
|
+
h2. License
|
51
|
+
|
52
|
+
This code is free to use under the terms of the MIT license.
|
53
|
+
|
54
|
+
h2. Contact
|
55
|
+
|
56
|
+
Comments are welcome. Send an email to "Adriano Mitre":mailto:adriano.mitre@gmail.com.
|
57
|
+
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polynomial
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Adriano Brito Mitre
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-18 00:00:00 -02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rubyforge
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 4
|
34
|
+
version: 2.0.4
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 19
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 6
|
49
|
+
- 2
|
50
|
+
version: 2.6.2
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: description of gem
|
54
|
+
email:
|
55
|
+
- adriano@mitre.com.br
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
extra_rdoc_files:
|
61
|
+
- History.txt
|
62
|
+
- License.txt
|
63
|
+
- Manifest.txt
|
64
|
+
- README.txt
|
65
|
+
- website/index.txt
|
66
|
+
files:
|
67
|
+
- lib/polynomial.rb
|
68
|
+
- lib/polynomial/ultraspherical.rb
|
69
|
+
- lib/polynomial/version.rb
|
70
|
+
- lib/polynomial/legendre.rb
|
71
|
+
- lib/polynomial/multivariate.rb
|
72
|
+
- lib/polynomial/chebyshev.rb
|
73
|
+
- test/test_chebyshev.rb
|
74
|
+
- test/test_polynomial.rb
|
75
|
+
- test/test_helper.rb
|
76
|
+
- test/test_suite.rb
|
77
|
+
- test/test_ultraspherical.rb
|
78
|
+
- test/test_multivariate.rb
|
79
|
+
- test/test_legendre.rb
|
80
|
+
- History.txt
|
81
|
+
- License.txt
|
82
|
+
- Manifest.txt
|
83
|
+
- README.txt
|
84
|
+
- website/index.txt
|
85
|
+
has_rdoc: true
|
86
|
+
homepage: http://polynomial.rubyforge.org
|
87
|
+
licenses: []
|
88
|
+
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options:
|
91
|
+
- --main
|
92
|
+
- README.txt
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project: polynomial
|
116
|
+
rubygems_version: 1.3.7
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: description of gem
|
120
|
+
test_files:
|
121
|
+
- test/test_chebyshev.rb
|
122
|
+
- test/test_polynomial.rb
|
123
|
+
- test/test_helper.rb
|
124
|
+
- test/test_suite.rb
|
125
|
+
- test/test_ultraspherical.rb
|
126
|
+
- test/test_multivariate.rb
|
127
|
+
- test/test_legendre.rb
|