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,469 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
require 'complex'
|
4
|
+
require 'bigdecimal'
|
5
|
+
require 'polynomial/multivariate'
|
6
|
+
|
7
|
+
MVP = Multivariate
|
8
|
+
|
9
|
+
class TestPoly < Test::Unit::TestCase
|
10
|
+
|
11
|
+
@@epsilon = 1e-7
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@one = MVP::Unity
|
15
|
+
@zero = MVP::Zero
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_incorrect_initialization
|
19
|
+
assert_raise(ArgumentError) { MVP.new }
|
20
|
+
assert_raise(ArgumentError) { MVP.new [] }
|
21
|
+
assert_raise(ArgumentError) { MVP.new [1], [2] }
|
22
|
+
assert_raise(ArgumentError) { MVP.new [1,2], [3,4] }
|
23
|
+
assert_raise(ArgumentError) { MVP.new [1,2,3], [4,5] }
|
24
|
+
assert_raise(TypeError) { MVP.new :oops }
|
25
|
+
assert_raise(TypeError) { MVP.new ['foo'] }
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_initialization
|
29
|
+
assert_equal(MVP.new([1,0,0]), MVP.new([1,0,0],[0,0,3]))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_equal
|
33
|
+
assert_equal(MVP.new([1,0,0]), MVP.new([1,0,0]))
|
34
|
+
assert_not_equal(MVP.new([1,0,0]), MVP.new([1,1,0]))
|
35
|
+
assert_not_equal(MVP.new([1,0,0]), MVP.new([1,0,1]))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_degree
|
39
|
+
q = MVP.new([1,0,0],[1,0,3])
|
40
|
+
assert_equal(0, q.degree(0))
|
41
|
+
assert_equal(0, q.degree(1))
|
42
|
+
assert_equal(3, q.degree(2))
|
43
|
+
assert_raise(RangeError) { q.degree(42) }
|
44
|
+
end
|
45
|
+
|
46
|
+
CoefficientsVariablesValues = {
|
47
|
+
[[1,0,0]] => {
|
48
|
+
[0,0] => 1, [0,1] => 1, [1,0] => 1, [1,1] => 1, [1,-1] => 1, [-1,-1] => 1,
|
49
|
+
},
|
50
|
+
[[1,0,0],[1,1,0],[1,0,1]] => {
|
51
|
+
[0,0] => 1, [0,1] => 2, [1,0] => 2, [1,1] => 3, [1,-1] => 1,
|
52
|
+
[-1,-1] => -1, [0,-1] => 0,
|
53
|
+
},
|
54
|
+
[[2,2,0],[3,0,3]] => {
|
55
|
+
[0,0] => 0, [0,1] => 3, [1,0] => 2, [1,1] => 5, [1,-1] => -1,
|
56
|
+
[-1,-1] => -1, [0,-1] => -3, [-1,0] => 2, [2,0] => 8, [0,2] => 24,
|
57
|
+
[3,0] => 18, [0,3] => 81,
|
58
|
+
},
|
59
|
+
}
|
60
|
+
def test_substitute
|
61
|
+
CoefficientsVariablesValues.each_pair do |coefs_ary, var_val|
|
62
|
+
q = MVP.new(*coefs_ary)
|
63
|
+
var_val.each_pair do |var, val|
|
64
|
+
error_msg = "coefs_ary=#{coefs_ary.inspect}\nvar=#{var.inspect}\nval=#{val}"
|
65
|
+
assert_equal val, q.substitute(*var), error_msg
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_multiplication
|
71
|
+
a = MVP.new([2,0,0])
|
72
|
+
b = MVP.new([1,1,0])
|
73
|
+
c = MVP.new([2,1,0])
|
74
|
+
assert_equal c, a*b
|
75
|
+
d = MVP.new([2,2,0])
|
76
|
+
assert_equal d, b*c
|
77
|
+
e = MVP.new([3,0,3])
|
78
|
+
f = MVP.new([6,2,3])
|
79
|
+
assert_equal f, d*e
|
80
|
+
a = MVP.new([2,0,0], [3,5,0], [7,0,11])
|
81
|
+
aa = MVP.new([4, 0, 0], [9, 10, 0], [49, 0, 22], [28, 0, 11], [42, 5, 11], [12, 5, 0])
|
82
|
+
assert_equal aa, a*a
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_unity_and_zero
|
86
|
+
assert_equal @one, @one*@one
|
87
|
+
assert_not_equal @zero, @one
|
88
|
+
assert_equal @zero, @one*@zero
|
89
|
+
q = MVP.new([2,0,0], [3,5,0], [7,0,11])
|
90
|
+
assert_equal q, @one*q
|
91
|
+
assert_equal @zero, q*@zero
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_power
|
95
|
+
assert_equal @one, @zero**0
|
96
|
+
assert_equal @zero, @zero**1
|
97
|
+
assert_equal @zero, @zero**2
|
98
|
+
q = MVP.new([2,0,0], [3,5,0], [7,0,11])
|
99
|
+
assert_equal @one, q**0
|
100
|
+
assert_equal q*q, q**2
|
101
|
+
assert_equal q*q*q, q**3
|
102
|
+
assert_raise(RangeError) { q**(-1) }
|
103
|
+
assert_raise(TypeError) { q**(2.3) }
|
104
|
+
end
|
105
|
+
|
106
|
+
=begin
|
107
|
+
def test_new_from_coefficients
|
108
|
+
assert_nothing_raised { Poly.new 0 }
|
109
|
+
assert_nothing_raised { Poly.new [0] }
|
110
|
+
assert_nothing_raised { Poly.new [1,2] }
|
111
|
+
assert_nothing_raised { Poly.new [1, 2.2] }
|
112
|
+
assert_nothing_raised { Poly.new [1, Complex(0,1)] }
|
113
|
+
assert_nothing_raised { Poly.new [1, BigDecimal('1e-2')] }
|
114
|
+
assert_nothing_raised { Poly.new 1,2 }
|
115
|
+
assert_equal Poly.new(1,2), Poly.new([1,2])
|
116
|
+
assert_equal Poly.new(1), Poly.new(1,0)
|
117
|
+
assert_equal Poly.new(1), Poly.new([1,0,0])
|
118
|
+
assert_equal Poly.new(1,2,3), Poly[1,2,3]
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_new_from_block
|
122
|
+
assert_equal Poly.new(0,1,2), Poly.new(2) {|n| n }
|
123
|
+
assert_equal Poly.new(1,2,3), Poly.new(2) {|n| n+1 }
|
124
|
+
assert_equal Poly.new(0,1,4,9), Poly.new(3) {|n| n**2 }
|
125
|
+
assert_equal Poly.new(3.2), Poly.new(3.2) {|n| n**2 }
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_new_from_power_coefficients_hash
|
129
|
+
assert_equal Poly[0,2], Poly.new({1=>2})
|
130
|
+
assert_equal Poly[0,0,3], Poly.new({2=>3})
|
131
|
+
assert_equal Poly[0,2,3], Poly[1=>2, 2=>3]
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_new_from_valid_strings
|
135
|
+
assert_equal Poly[0], Poly['0']
|
136
|
+
assert_equal Poly[1], Poly[' 1 ']
|
137
|
+
assert_equal Poly[-1], Poly['-1']
|
138
|
+
assert_equal Poly[-1.3], Poly['-1.3']
|
139
|
+
assert_equal Poly[0,1], Poly['x']
|
140
|
+
assert_equal Poly[0,1], Poly['+x']
|
141
|
+
assert_equal Poly[0,1], Poly['+1x', {:multiplication_symbol=>''}]
|
142
|
+
assert_equal Poly[1,1], Poly['1+x']
|
143
|
+
assert_equal Poly[1,1], Poly['1+1x', {:multiplication_symbol=>''}]
|
144
|
+
assert_equal Poly[0,0,3], Poly['3*x**2']
|
145
|
+
assert_equal Poly[0,0,1], Poly['x**2']
|
146
|
+
assert_equal Poly[3,0,1], Poly['3+x**2']
|
147
|
+
assert_equal Poly[3,0,1], Poly['3+x^2', {:power_symbol=>'^'}]
|
148
|
+
assert_equal Poly[3,2], Poly['3+2*x']
|
149
|
+
assert_equal Poly[2,1], Poly['2+y', {:variable_name=>'y'}]
|
150
|
+
assert_equal Poly['4*x**2+2'], Poly[2,0,4]
|
151
|
+
assert_equal Poly[' 4*x**2 + 2 '], Poly[2,0,4]
|
152
|
+
assert_equal Poly[1,0,-7], Poly.from_string("1 - 7*x**2")
|
153
|
+
assert_equal Poly[3,4.1,5], Poly.from_string("3 + 4.1x + 5x^2", :multiplication_symbol=>'', :power_symbol=>'^')
|
154
|
+
assert_equal Poly[0,-3], Poly.from_string("-3*y", :variable_name=>'y')
|
155
|
+
assert_equal Poly[1e-2], Poly.from_string('1e-2')
|
156
|
+
assert_equal Poly[0,1e-2], Poly.from_string('1e-2*x')
|
157
|
+
assert_equal Poly[BigDecimal('1.1')], Poly.from_string(BigDecimal('1.1').to_s)
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_from_invalid_strings_misc
|
161
|
+
assert_raise(ArgumentError) { Poly[''] }
|
162
|
+
assert_raise(ArgumentError) { Poly['1..'] }
|
163
|
+
assert_raise(ArgumentError) { Poly['1..1'] }
|
164
|
+
assert_raise(ArgumentError) { Poly['+'] }
|
165
|
+
assert_raise(ArgumentError) { Poly['++1'] }
|
166
|
+
assert_raise(ArgumentError) { Poly['-+1'] }
|
167
|
+
assert_raise(ArgumentError) { Poly['--1'] }
|
168
|
+
assert_raise(ArgumentError) { Poly['1-'] }
|
169
|
+
assert_raise(ArgumentError) { Poly['1.2-'] }
|
170
|
+
assert_raise(ArgumentError) { Poly['xx'] }
|
171
|
+
assert_raise(ArgumentError) { Poly['xx**2'] }
|
172
|
+
assert_raise(ArgumentError) { Poly['x*x'] }
|
173
|
+
assert_raise(ArgumentError) { Poly['x**x'] }
|
174
|
+
assert_raise(ArgumentError) { Poly['2**3'] }
|
175
|
+
assert_raise(ArgumentError) { Poly['x**3*4'] }
|
176
|
+
assert_raise(ArgumentError) { Poly['e2'] }
|
177
|
+
assert_raise(ArgumentError) { Poly['1e--2'] }
|
178
|
+
assert_raise(ArgumentError) { Poly['1ee2'] }
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_from_invalid_strings_incomplete
|
182
|
+
assert_raise(ArgumentError) { Poly['1+'] }
|
183
|
+
assert_raise(ArgumentError) { Poly['1.2+'] }
|
184
|
+
assert_raise(ArgumentError) { Poly['1*'] }
|
185
|
+
assert_raise(ArgumentError) { Poly['1.2*'] }
|
186
|
+
assert_raise(ArgumentError) { Poly['2*x**'] }
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_from_invalid_strings_symbol_omission
|
190
|
+
assert_raise(ArgumentError) { Poly['1x'] }
|
191
|
+
assert_raise(ArgumentError) { Poly['1.2x'] }
|
192
|
+
assert_raise(ArgumentError) { Poly['2*x3'] }
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_from_invalid_strings_spaces
|
196
|
+
assert_raise(ArgumentError) { Poly['1 * x'] }
|
197
|
+
assert_raise(ArgumentError) { Poly['1 *x'] }
|
198
|
+
assert_raise(ArgumentError) { Poly['1* x'] }
|
199
|
+
assert_raise(ArgumentError) { Poly['1*x ** 2'] }
|
200
|
+
assert_raise(ArgumentError) { Poly['1*x **2'] }
|
201
|
+
assert_raise(ArgumentError) { Poly['1*x** 2'] }
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_from_string_repeated
|
205
|
+
assert_equal Poly['1+2'], Poly[3]
|
206
|
+
assert_equal Poly['x+3*x'], Poly[0,4]
|
207
|
+
assert_equal Poly['1+x+x**2+2*x**2'], Poly[1,1,3]
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_substitute
|
211
|
+
poly = Poly[0]
|
212
|
+
pairs = [[0,0], [1,0], [-1,0], [2,0], [0.33, 0]]
|
213
|
+
assert_in_out_pairs(poly, pairs)
|
214
|
+
|
215
|
+
poly = Poly[0, 1]
|
216
|
+
pairs = [[0,0], [1,1], [-1,-1], [2,2], [0.33, 0.33]]
|
217
|
+
assert_in_out_pairs(poly, pairs)
|
218
|
+
|
219
|
+
poly = Poly[0, 0, 1]
|
220
|
+
pairs = [[0,0], [1,1], [-1,1], [2,4], [0.33, 0.33**2]]
|
221
|
+
assert_in_out_pairs(poly, pairs)
|
222
|
+
|
223
|
+
poly = Poly[1, 1]
|
224
|
+
pairs = [[0,1], [1,2], [-1,0], [2,3], [0.33, 1.33]]
|
225
|
+
assert_in_out_pairs(poly, pairs)
|
226
|
+
|
227
|
+
poly = Poly[1, -1]
|
228
|
+
pairs = [[0,1], [1,0], [-1,2], [2,-1], [0.33, 1-0.33]]
|
229
|
+
assert_in_out_pairs(poly, pairs)
|
230
|
+
|
231
|
+
poly = Poly[Complex(0,1), 1]
|
232
|
+
pairs = [[0,Complex(0,1)], [1,Complex(1,1)], [-1,Complex(-1,1)], [2,Complex(2,1)], [0.33, Complex(0.33,1)]]
|
233
|
+
assert_in_out_pairs(poly, pairs)
|
234
|
+
|
235
|
+
poly = Poly[BigDecimal('0'), BigDecimal('1.11')]
|
236
|
+
pairs = [[0,BigDecimal('0')], [1,BigDecimal('1.11')], [-1,BigDecimal('-1.11')], [2,BigDecimal('2.22')], [BigDecimal('0.33'), BigDecimal('0.3663')]]
|
237
|
+
assert_in_out_pairs(poly, pairs)
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_degree
|
241
|
+
assert_equal 0, Poly[0].degree
|
242
|
+
assert_equal 0, Poly[1].degree
|
243
|
+
assert_equal 1, Poly[1,2].degree
|
244
|
+
assert_equal 1, Poly[0,-1].degree
|
245
|
+
assert_equal 2, Poly[1,0,1].degree
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_multiplication
|
249
|
+
assert_equal Poly[0], Poly[1] * 0
|
250
|
+
assert_equal Poly[0], 0 * Poly[1]
|
251
|
+
assert_equal Poly[1], Poly[1] * 1
|
252
|
+
assert_equal Poly[1], 1 * Poly[1]
|
253
|
+
assert_equal Poly[2], Poly[1] * 2
|
254
|
+
assert_equal Poly[2], Poly[1] * Poly[2]
|
255
|
+
assert_equal Poly[2,4,6], Poly[1,2,3] * 2
|
256
|
+
assert_equal Poly[2,4,6], 2 * Poly[1,2,3]
|
257
|
+
assert_equal Poly[0,0,1,2], Poly[0,1,2] * Poly[0,1]
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_division_by_numeric
|
261
|
+
assert_equal Poly[0], Poly[0] / 1
|
262
|
+
assert_equal Poly[1], Poly[1] / 1
|
263
|
+
assert_equal Poly[0.5], Poly[1] / 2
|
264
|
+
assert_equal Poly[0.5], Poly[1] / 2.0
|
265
|
+
assert_equal Poly[1,2,3], Poly[2,4,6] / 2
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_divmod
|
269
|
+
assert_equal [Poly[3], Poly[-8,-4]], Poly[1,2,3].divmod(Poly[3,2,1])
|
270
|
+
assert_equal [Poly[0], Poly[1,2]], Poly[1,2].divmod(Poly[1,2,3])
|
271
|
+
assert_equal [Poly[-27,-9,1], Poly[-123]], Poly[-42,0,-12,1].divmod(Poly[-3,1])
|
272
|
+
assert_equal [Poly[4,6], Poly[2]], Poly[2,4,6].divmod(Poly[0,1])
|
273
|
+
assert_equal [Poly[1,2,3], 0], Poly[2,4,6].divmod(2)
|
274
|
+
assert_raise(ArgumentError) { Poly[1,2,3].divmod(:foo) }
|
275
|
+
assert_raise(ArgumentError) { Poly[1,2,3].divmod(nil) }
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_division_by_polynomial
|
279
|
+
assert_equal Poly[2], Poly[2,4,6] / Poly[1,2,3]
|
280
|
+
assert_equal Poly[0.5], Poly[1,2,3] / Poly[2,4,6]
|
281
|
+
assert_equal Poly[3], Poly[1,2,3] / Poly[3,2,1]
|
282
|
+
assert_equal Poly[1,2,3], Poly[2,4,6] / Poly[2]
|
283
|
+
end
|
284
|
+
|
285
|
+
@@quomod_data = [
|
286
|
+
[Poly[1,2,3], Poly[3,2,1], Poly[3], Poly[-8,-4]],
|
287
|
+
[Poly[1,2], Poly[1,2,3], Poly[0], Poly[1,2]],
|
288
|
+
[Poly[-42,0,-12,1], Poly[-3,1], Poly[-27,-9,1], Poly[-123]],
|
289
|
+
[Poly[1,3.quo(2)], 2, Poly[1.quo(2), 3.quo(4)], 0],
|
290
|
+
[Poly[2,4,6], Poly[0,3], Poly[4.quo(3),6.quo(3)], Poly[2]],
|
291
|
+
[Poly[1,3.quo(2)], Poly[2], Poly[1.quo(2), 3.quo(4)], 0],
|
292
|
+
[Poly[2,4,6], 2, Poly[1,2,3], 0],
|
293
|
+
]
|
294
|
+
def test_quomod
|
295
|
+
@@quomod_data.each do |dividend, divisor, quotient, rest|
|
296
|
+
assert_equal [quotient, rest], dividend.quomod(divisor)
|
297
|
+
end
|
298
|
+
assert_raise(ArgumentError) { Poly[1,2,3].quomod(:foo) }
|
299
|
+
assert_raise(ArgumentError) { Poly[1,2,3].quomod(nil) }
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_quo_by_numeric
|
303
|
+
assert_equal Poly[1.quo(2), 3.quo(4)], Poly[1,3.quo(2)].quo(2)
|
304
|
+
assert_equal Poly[1/2.0, 3.quo(2)/2.0], Poly[1,3.quo(2)].quo(2.0)
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_quo_by_polynomial
|
308
|
+
assert_equal Poly[4.quo(3),6.quo(3)], Poly[2,4,6].quo(Poly[0,3])
|
309
|
+
assert_equal Poly[1.quo(2), 3.quo(4)], Poly[1,3.quo(2)].quo(2)
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_remainder
|
313
|
+
@@quomod_data.each do |dividend, divisor, quotient, rest|
|
314
|
+
assert_equal rest, dividend % divisor
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
@@power_data = [
|
319
|
+
[Poly[1], 0, Poly[1]],
|
320
|
+
[Poly[1], 1, Poly[1]],
|
321
|
+
[Poly[1], 2, Poly[1]],
|
322
|
+
[Poly[0,1], 1, Poly[0,1]],
|
323
|
+
[Poly[0,1], 2, Poly[0,0,1]],
|
324
|
+
[Poly[3,4], 2, Poly[9,24,16]],
|
325
|
+
]
|
326
|
+
def test_power
|
327
|
+
@@power_data.each do |polynomial, power, result|
|
328
|
+
assert_equal result, polynomial**power
|
329
|
+
end
|
330
|
+
assert_raise(ArgumentError) { Poly[1,2,3]**(-1) }
|
331
|
+
assert_raise(NoMethodError) { Poly[1,2,3]**(1.1) }
|
332
|
+
# Exceptions raised in Ruby 1.8 and 1.9 are different for the following
|
333
|
+
assert_raise(ArgumentError, NoMethodError) { Poly[1,2,3]**(:foo) }
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_addition
|
337
|
+
assert_equal Poly[1], Poly[1] + 0
|
338
|
+
assert_equal Poly[2], Poly[1] + 1
|
339
|
+
assert_equal Poly[1,1], Poly[1] + Poly[0,1]
|
340
|
+
assert_equal Poly[2,3,4], Poly[2] + Poly[0,3,4]
|
341
|
+
end
|
342
|
+
|
343
|
+
def test_subtraction
|
344
|
+
assert_equal Poly[1], Poly[1] - 0
|
345
|
+
assert_equal Poly[0], Poly[1] - 1
|
346
|
+
assert_equal Poly[1,-1], Poly[1] - Poly[0,1]
|
347
|
+
assert_equal Poly[2,-3,-4], Poly[2] - Poly[0,3,4]
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_to_s
|
351
|
+
assert_equal "0", Poly[0].to_s
|
352
|
+
assert_equal "1", Poly[1].to_s
|
353
|
+
assert_equal "x", Poly[0,1].to_s
|
354
|
+
assert_equal "1 + x", Poly[1,Complex(1,0)].to_s
|
355
|
+
assert_equal "0 + x", Poly[0,1].to_s(:verbose=>true)
|
356
|
+
assert_equal "1 - 2*x + 3*x**2", Poly[1,-2,3].to_s
|
357
|
+
assert_equal "3*x**2 - 2*x + 1", Poly[1,-2,3].to_s(:decreasing=>true)
|
358
|
+
assert_equal "1 + 0*x + 3*x**2", Poly[1,0,3].to_s(:verbose=>true)
|
359
|
+
assert_equal "-1 - 3*x^2", Poly[-1,0,-3].to_s(:power_symbol=>'^')
|
360
|
+
assert_equal "-1*y - 3*y**2", Poly[0,-1,-3].to_s(:variable_name=>'y')
|
361
|
+
assert_equal "0+x", Poly[0,1].to_s(:verbose=>true, :spaced=>false)
|
362
|
+
assert_equal "1-2*x+3*x**2", Poly[1,-2,3].to_s(:spaced=>false)
|
363
|
+
assert_equal "1+0*x+3*x**2", Poly[1,0,3].to_s(:verbose=>true, :spaced=>false)
|
364
|
+
assert_equal "-1-3*x^2", Poly[-1,0,-3].to_s(:power_symbol=>'^', :spaced=>false)
|
365
|
+
|
366
|
+
# Complex#to_s results in Ruby 1.8 and 1.9 are different
|
367
|
+
assert ["1 + (0 + 1i)*x", "1 + 1i*x"].include?(Poly[1,Complex(0,1)].to_s)
|
368
|
+
|
369
|
+
assert_equal "1 + (1 + 1i)*x", Poly[1,Complex(1,1)].to_s
|
370
|
+
assert_equal "1 + x", Poly[1,Complex(1,0)].to_s
|
371
|
+
assert_equal "-3.33694235763727e-05", Poly[-3.33694235763727e-05].to_s
|
372
|
+
assert_equal "1 + 3.33694235763727e-05*x", Poly[1,3.33694235763727e-05].to_s
|
373
|
+
assert_equal "1 + (2/3)*x", Poly[1,2.quo(3)].to_s
|
374
|
+
assert_equal "(-1/2) + (3/2)*x**2", Poly[-1.quo(2), 0, 3.quo(2)].to_s
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_to_i
|
378
|
+
assert_raise(ArgumentError) { Poly[1,2].to_i }
|
379
|
+
assert_equal 0, Poly[0].to_i
|
380
|
+
assert_equal 3, Poly[3].to_i
|
381
|
+
assert_equal(-2, Poly[-2].to_i)
|
382
|
+
assert_equal 3, Poly[3.3].to_i
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_to_f
|
386
|
+
assert_raise(ArgumentError) { Poly[1,2].to_f }
|
387
|
+
assert_equal 0.0, Poly[0].to_f
|
388
|
+
assert_equal 3.1, Poly[3.1].to_f
|
389
|
+
assert_equal(-2.2, Poly[-2.2].to_f)
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_integral
|
393
|
+
assert_equal Poly[0], Poly[0].integral
|
394
|
+
assert_equal Poly[0,1], Poly[1].integral
|
395
|
+
assert_equal Poly[0,1], Poly[1].integral
|
396
|
+
assert_equal Poly[0,1,1/2.0], Poly[1,1].integral
|
397
|
+
assert_equal Poly[0,1,1/2.0,1/3.0], Poly[1,1,1].integral
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_derivative
|
401
|
+
assert_equal Poly[0], Poly[0].derivative
|
402
|
+
assert_equal Poly[0], Poly[1].derivative
|
403
|
+
assert_equal Poly[0], Poly[2].derivative
|
404
|
+
assert_equal Poly[1], Poly[0,1].derivative
|
405
|
+
assert_equal Poly[1,2], Poly[1,1,1].derivative
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_derivatives
|
409
|
+
z = Poly[0]
|
410
|
+
assert_equal [z], z.derivatives
|
411
|
+
assert_equal [Poly[1], z], Poly[0,1].derivatives
|
412
|
+
assert_equal [Poly[2], z], Poly[1,2].derivatives
|
413
|
+
assert_equal [Poly[4,10], Poly[10], z], Poly[3,4,5].derivatives
|
414
|
+
assert_equal [Poly[2,6,12], Poly[6,24], Poly[24], z], Poly[1,2,3,4].derivatives
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_integral_derivative_cascading
|
418
|
+
coefs = Array.new(16) { rand }
|
419
|
+
assert Poly[coefs].equal_in_delta Poly[coefs].integral.derivative, @@epsilon
|
420
|
+
assert Poly[0, *coefs[1..-1]].equal_in_delta Poly[coefs].derivative.integral, @@epsilon
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_equal_in_delta
|
424
|
+
assert Poly[0].equal_in_delta(Poly[@@epsilon], @@epsilon)
|
425
|
+
assert Poly[1].equal_in_delta(Poly[1-@@epsilon], @@epsilon)
|
426
|
+
assert Poly[-@@epsilon,@@epsilon].equal_in_delta(Poly[@@epsilon, -@@epsilon], 2*@@epsilon)
|
427
|
+
end
|
428
|
+
|
429
|
+
def test_derivative_integral
|
430
|
+
[ [0], [1], [0, 42], [1,11], [3,4,5] ].each do |coefs|
|
431
|
+
poly = Poly.new(coefs)
|
432
|
+
assert poly.equal_in_delta(poly.integral.derivative, @@epsilon)
|
433
|
+
end
|
434
|
+
1.upto(10) do |n|
|
435
|
+
poly = Poly.new( Array.new(n) {|m| 2*m + rand } )
|
436
|
+
assert poly.equal_in_delta(poly.integral.derivative, @@epsilon)
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_compare
|
441
|
+
a = Poly[2]
|
442
|
+
b = Poly[2,1]
|
443
|
+
c = Poly[2,2]
|
444
|
+
d = Poly[2,1,2]
|
445
|
+
assert_equal [a,b,c,d], [d,b,c,a].sort
|
446
|
+
assert_equal 0, Poly[0]
|
447
|
+
assert_equal Poly[1], 1
|
448
|
+
assert_equal Poly[0.5], 0.5
|
449
|
+
assert_equal 0.5, Poly[0.5]
|
450
|
+
assert_not_equal 1, Poly[1,2]
|
451
|
+
assert_raise(TypeError) { Poly[1] <=> 'foo' }
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_coerce
|
455
|
+
assert_equal [Poly[2],Poly[1]], Poly[1].coerce(2)
|
456
|
+
assert_equal [Poly[2],Poly[1]], Poly[1].coerce(Poly[2])
|
457
|
+
assert_raise(TypeError) { Poly[1].coerce('foo') }
|
458
|
+
end
|
459
|
+
|
460
|
+
# HELPER FUNCTIONS
|
461
|
+
|
462
|
+
def assert_in_out_pairs(polynomial, input_output_pairs, delta=@@epsilon)
|
463
|
+
input_output_pairs.each do |x, y|
|
464
|
+
assert_equal y, polynomial.substitute(x)
|
465
|
+
end
|
466
|
+
end
|
467
|
+
=end
|
468
|
+
|
469
|
+
end
|