rubysl-mathn 1.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/mathn.rb +1 -0
- data/lib/rubysl/mathn.rb +2 -0
- data/lib/rubysl/mathn/mathn.rb +313 -0
- data/lib/rubysl/mathn/version.rb +5 -0
- data/rubysl-mathn.gemspec +23 -0
- data/spec/bignum/exponent_spec.rb +19 -0
- data/spec/complex/Complex_spec.rb +12 -0
- data/spec/fixnum/exponent_spec.rb +15 -0
- data/spec/float/exponent_spec.rb +15 -0
- data/spec/integer/from_prime_division_spec.rb +7 -0
- data/spec/integer/gcd2_spec.rb +17 -0
- data/spec/integer/prime_division_spec.rb +19 -0
- data/spec/math/fixtures/classes.rb +3 -0
- data/spec/math/rsqrt_spec.rb +13 -0
- data/spec/math/shared/rsqrt.rb +31 -0
- data/spec/math/shared/sqrt.rb +25 -0
- data/spec/math/sqrt_spec.rb +13 -0
- data/spec/prime/each_spec.rb +28 -0
- data/spec/prime/new_spec.rb +11 -0
- data/spec/prime/next_spec.rb +10 -0
- data/spec/rational/Rational_spec.rb +12 -0
- data/spec/rational/exponent_spec.rb +70 -0
- data/spec/rational/inspect_spec.rb +23 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 467e937606c8e419d779633cb724dd5327bfeeff
|
4
|
+
data.tar.gz: 1bcc431f67bb1a5581d5574aa816a7a58eea7971
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3d2f5ab89cab030c46699a7500a816980b5742b6148bb50c63c74cd4cf9bff608e4f520dcd416dd0a1f285251325ece2b646e81675d34447099b29e41ff2d22
|
7
|
+
data.tar.gz: 7f4f2d6254a66cdb96c398e8800f11d7da0440680ce5dce5591874cf27d9edef8be03a73182696e0464c9082cb9a3575298b4d62dc86189ecab6715146391f9f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2013, Brian Shirai
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
3. Neither the name of the library nor the names of its contributors may be
|
13
|
+
used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
|
20
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
21
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
22
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
23
|
+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
24
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
25
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Rubysl::Mathn
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rubysl-mathn'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rubysl-mathn
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mathn.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rubysl/mathn"
|
data/lib/rubysl/mathn.rb
ADDED
@@ -0,0 +1,313 @@
|
|
1
|
+
#
|
2
|
+
# mathn.rb -
|
3
|
+
# $Release Version: 0.5 $
|
4
|
+
# $Revision: 1.1.1.1.4.1 $
|
5
|
+
# $Date: 1998/01/16 12:36:05 $
|
6
|
+
# by Keiju ISHITSUKA(SHL Japan Inc.)
|
7
|
+
#
|
8
|
+
# --
|
9
|
+
#
|
10
|
+
#
|
11
|
+
#
|
12
|
+
|
13
|
+
require "complex.rb"
|
14
|
+
require "rational.rb"
|
15
|
+
require "matrix.rb"
|
16
|
+
|
17
|
+
class Integer
|
18
|
+
|
19
|
+
def gcd2(int)
|
20
|
+
a = self.abs
|
21
|
+
b = int.abs
|
22
|
+
a, b = b, a if a < b
|
23
|
+
|
24
|
+
pd_a = a.prime_division
|
25
|
+
pd_b = b.prime_division
|
26
|
+
|
27
|
+
gcd = 1
|
28
|
+
for pair in pd_a
|
29
|
+
as = pd_b.assoc(pair[0])
|
30
|
+
if as
|
31
|
+
gcd *= as[0] ** [as[1], pair[1]].min
|
32
|
+
end
|
33
|
+
end
|
34
|
+
return gcd
|
35
|
+
end
|
36
|
+
|
37
|
+
def Integer.from_prime_division(pd)
|
38
|
+
value = 1
|
39
|
+
for prime, index in pd
|
40
|
+
value *= prime**index
|
41
|
+
end
|
42
|
+
value
|
43
|
+
end
|
44
|
+
|
45
|
+
def prime_division
|
46
|
+
raise ZeroDivisionError if self == 0
|
47
|
+
ps = Prime.new
|
48
|
+
value = self
|
49
|
+
if value < 0
|
50
|
+
value = -value
|
51
|
+
pv = [[-1, 1]]
|
52
|
+
else
|
53
|
+
pv = []
|
54
|
+
end
|
55
|
+
for prime in ps
|
56
|
+
count = 0
|
57
|
+
while (value1, mod = value.divmod(prime)
|
58
|
+
mod) == 0
|
59
|
+
value = value1
|
60
|
+
count += 1
|
61
|
+
end
|
62
|
+
if count != 0
|
63
|
+
pv.push [prime, count]
|
64
|
+
end
|
65
|
+
break if prime * prime >= value
|
66
|
+
end
|
67
|
+
if value > 1
|
68
|
+
pv.push [value, 1]
|
69
|
+
end
|
70
|
+
return pv
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class Prime
|
75
|
+
include Enumerable
|
76
|
+
|
77
|
+
def initialize
|
78
|
+
@seed = 1
|
79
|
+
@primes = []
|
80
|
+
@counts = []
|
81
|
+
end
|
82
|
+
|
83
|
+
def succ
|
84
|
+
i = -1
|
85
|
+
size = @primes.size
|
86
|
+
while i < size
|
87
|
+
if i == -1
|
88
|
+
@seed += 1
|
89
|
+
i += 1
|
90
|
+
else
|
91
|
+
while @seed > @counts[i]
|
92
|
+
@counts[i] += @primes[i]
|
93
|
+
end
|
94
|
+
if @seed != @counts[i]
|
95
|
+
i += 1
|
96
|
+
else
|
97
|
+
i = -1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
@primes.push @seed
|
102
|
+
@counts.push @seed + @seed
|
103
|
+
return @seed
|
104
|
+
end
|
105
|
+
alias next succ
|
106
|
+
|
107
|
+
def each
|
108
|
+
loop do
|
109
|
+
yield succ
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Fixnum
|
115
|
+
alias / quo
|
116
|
+
end
|
117
|
+
|
118
|
+
class Bignum
|
119
|
+
alias / quo
|
120
|
+
end
|
121
|
+
|
122
|
+
class Rational
|
123
|
+
Unify = true
|
124
|
+
|
125
|
+
def inspect
|
126
|
+
format "%s/%s", numerator.inspect, denominator.inspect
|
127
|
+
end
|
128
|
+
|
129
|
+
alias power! **
|
130
|
+
|
131
|
+
def ** (other)
|
132
|
+
if other.kind_of?(Rational)
|
133
|
+
other2 = other
|
134
|
+
if self < 0
|
135
|
+
return Complex.new!(self, 0) ** other
|
136
|
+
elsif other == 0
|
137
|
+
return Rational(1,1)
|
138
|
+
elsif self == 0
|
139
|
+
return Rational(0,1)
|
140
|
+
elsif self == 1
|
141
|
+
return Rational(1,1)
|
142
|
+
end
|
143
|
+
|
144
|
+
npd = numerator.prime_division
|
145
|
+
dpd = denominator.prime_division
|
146
|
+
if other < 0
|
147
|
+
other = -other
|
148
|
+
npd, dpd = dpd, npd
|
149
|
+
end
|
150
|
+
|
151
|
+
for elm in npd
|
152
|
+
elm[1] = elm[1] * other
|
153
|
+
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
|
154
|
+
return Float(self) ** other2
|
155
|
+
end
|
156
|
+
elm[1] = elm[1].to_i
|
157
|
+
end
|
158
|
+
|
159
|
+
for elm in dpd
|
160
|
+
elm[1] = elm[1] * other
|
161
|
+
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
|
162
|
+
return Float(self) ** other2
|
163
|
+
end
|
164
|
+
elm[1] = elm[1].to_i
|
165
|
+
end
|
166
|
+
|
167
|
+
num = Integer.from_prime_division(npd)
|
168
|
+
den = Integer.from_prime_division(dpd)
|
169
|
+
|
170
|
+
Rational(num,den)
|
171
|
+
|
172
|
+
elsif other.kind_of?(Integer)
|
173
|
+
if other > 0
|
174
|
+
num = numerator ** other
|
175
|
+
den = denominator ** other
|
176
|
+
elsif other < 0
|
177
|
+
num = denominator ** -other
|
178
|
+
den = numerator ** -other
|
179
|
+
elsif other == 0
|
180
|
+
num = 1
|
181
|
+
den = 1
|
182
|
+
end
|
183
|
+
Rational.new!(num, den)
|
184
|
+
elsif other.kind_of?(Float)
|
185
|
+
Float(self) ** other
|
186
|
+
else
|
187
|
+
x , y = other.coerce(self)
|
188
|
+
x ** y
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def power2(other)
|
193
|
+
if other.kind_of?(Rational)
|
194
|
+
if self < 0
|
195
|
+
return Complex(self, 0) ** other
|
196
|
+
elsif other == 0
|
197
|
+
return Rational(1,1)
|
198
|
+
elsif self == 0
|
199
|
+
return Rational(0,1)
|
200
|
+
elsif self == 1
|
201
|
+
return Rational(1,1)
|
202
|
+
end
|
203
|
+
|
204
|
+
dem = nil
|
205
|
+
x = self.denominator.to_f.to_i
|
206
|
+
neard = self.denominator.to_f ** (1.0/other.denominator.to_f)
|
207
|
+
loop do
|
208
|
+
if (neard**other.denominator == self.denominator)
|
209
|
+
dem = neaed
|
210
|
+
break
|
211
|
+
end
|
212
|
+
end
|
213
|
+
nearn = self.numerator.to_f ** (1.0/other.denominator.to_f)
|
214
|
+
Rational(num,den)
|
215
|
+
|
216
|
+
elsif other.kind_of?(Integer)
|
217
|
+
if other > 0
|
218
|
+
num = numerator ** other
|
219
|
+
den = denominator ** other
|
220
|
+
elsif other < 0
|
221
|
+
num = denominator ** -other
|
222
|
+
den = numerator ** -other
|
223
|
+
elsif other == 0
|
224
|
+
num = 1
|
225
|
+
den = 1
|
226
|
+
end
|
227
|
+
Rational.new!(num, den)
|
228
|
+
elsif other.kind_of?(Float)
|
229
|
+
Float(self) ** other
|
230
|
+
else
|
231
|
+
x , y = other.coerce(self)
|
232
|
+
x ** y
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
module Math
|
238
|
+
def sqrt(a)
|
239
|
+
if a.kind_of?(Complex)
|
240
|
+
abs = sqrt(a.real*a.real + a.image*a.image)
|
241
|
+
# if not abs.kind_of?(Rational)
|
242
|
+
# return a**Rational(1,2)
|
243
|
+
# end
|
244
|
+
x = sqrt((a.real + abs)/Rational(2))
|
245
|
+
y = sqrt((-a.real + abs)/Rational(2))
|
246
|
+
# if !(x.kind_of?(Rational) and y.kind_of?(Rational))
|
247
|
+
# return a**Rational(1,2)
|
248
|
+
# end
|
249
|
+
if a.image >= 0
|
250
|
+
Complex(x, y)
|
251
|
+
else
|
252
|
+
Complex(x, -y)
|
253
|
+
end
|
254
|
+
elsif a >= 0
|
255
|
+
rsqrt(a)
|
256
|
+
else
|
257
|
+
Complex(0,rsqrt(-a))
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def rsqrt(a)
|
262
|
+
if a.kind_of?(Float)
|
263
|
+
sqrt!(a)
|
264
|
+
elsif a.kind_of?(Rational)
|
265
|
+
rsqrt(a.numerator)/rsqrt(a.denominator)
|
266
|
+
else
|
267
|
+
src = a
|
268
|
+
max = 2 ** 32
|
269
|
+
byte_a = [src & 0xffffffff]
|
270
|
+
# ruby's bug
|
271
|
+
while (src >= max) and (src >>= 32)
|
272
|
+
byte_a.unshift src & 0xffffffff
|
273
|
+
end
|
274
|
+
|
275
|
+
answer = 0
|
276
|
+
main = 0
|
277
|
+
side = 0
|
278
|
+
for elm in byte_a
|
279
|
+
main = (main << 32) + elm
|
280
|
+
side <<= 16
|
281
|
+
if answer != 0
|
282
|
+
if main * 4 < side * side
|
283
|
+
applo = main.div(side)
|
284
|
+
else
|
285
|
+
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
|
286
|
+
end
|
287
|
+
else
|
288
|
+
applo = sqrt!(main).to_i + 1
|
289
|
+
end
|
290
|
+
|
291
|
+
while (x = (side + applo) * applo) > main
|
292
|
+
applo -= 1
|
293
|
+
end
|
294
|
+
main -= x
|
295
|
+
answer = (answer << 16) + applo
|
296
|
+
side += applo * 2
|
297
|
+
end
|
298
|
+
if main == 0
|
299
|
+
answer
|
300
|
+
else
|
301
|
+
sqrt!(a)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
module_function :sqrt
|
307
|
+
module_function :rsqrt
|
308
|
+
end
|
309
|
+
|
310
|
+
class Complex
|
311
|
+
Unify = true
|
312
|
+
end
|
313
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/mathn/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-mathn"
|
6
|
+
spec.version = RubySL::Mathn::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library mathn.}
|
10
|
+
spec.summary = %q{Ruby standard library mathn.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-mathn"
|
12
|
+
spec.license = "BSD"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "mspec", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
describe "Bignum#**" do
|
4
|
+
before(:each) do
|
5
|
+
@bignum = bignum_value(47)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns self raised to other (positive) power" do
|
9
|
+
(@bignum ** 4).should == 7237005577332262361485077344629993318496048279512298547155833600056910050625
|
10
|
+
(@bignum ** 1.2).should be_close(57262152889751597425762.57804, TOLERANCE)
|
11
|
+
end
|
12
|
+
|
13
|
+
ruby_version_is '1.9' do
|
14
|
+
it "returns a complex number when negative and raised to a fractional power" do
|
15
|
+
((-@bignum) ** (1/3)).should be_close(Complex(1048576,1816186.907597341), TOLERANCE)
|
16
|
+
((-@bignum) ** (1.0/3)).should be_close(Complex(1048576,1816186.907597341), TOLERANCE)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
ruby_version_is '1.9' do
|
4
|
+
describe "Kernel#Complex" do
|
5
|
+
it "returns an Integer if imaginary part is 0" do
|
6
|
+
Complex(42,0).should == 42
|
7
|
+
Complex(42,0).should be_kind_of(Fixnum)
|
8
|
+
Complex(bignum_value,0).should == bignum_value
|
9
|
+
Complex(bignum_value,0).should be_kind_of(Bignum)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
describe "Fixnum#**" do
|
4
|
+
it "returns self raised to other (positive) power" do
|
5
|
+
(2 ** 4).should == 16
|
6
|
+
(2 ** 1.2).should be_close(2.2973967, TOLERANCE)
|
7
|
+
end
|
8
|
+
|
9
|
+
ruby_version_is '1.9' do
|
10
|
+
it "returns a complex number when negative and raised to a fractional power" do
|
11
|
+
((-8) ** (1/3)).should be_close(Complex(1, 1.73205), TOLERANCE)
|
12
|
+
((-8) ** (1.0/3)).should be_close(Complex(1, 1.73205), TOLERANCE)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
describe "Float#**" do
|
4
|
+
it "returns self raised to other (positive) power" do
|
5
|
+
(2.0 ** 4).should == 16.0
|
6
|
+
(2.0 ** 1.2).should be_close(2.2973967, TOLERANCE)
|
7
|
+
end
|
8
|
+
|
9
|
+
ruby_version_is '1.9' do
|
10
|
+
it "returns a complex number when negative and raised to a fractional power" do
|
11
|
+
((-8.0) ** (1/3)).should be_close(Complex(1, 1.73205), TOLERANCE)
|
12
|
+
((-8.0) ** (1.0/3)).should be_close(Complex(1, 1.73205), TOLERANCE)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
ruby_version_is ''...'1.9' do
|
4
|
+
describe "Integer#gcd2" do
|
5
|
+
it "Returns the greatest common divisor of the two numbers" do
|
6
|
+
15.gcd2(5).should == 5
|
7
|
+
15.gcd2(-6).should == 3
|
8
|
+
-23.gcd2(19).should == 1
|
9
|
+
-10.gcd2(-2).should == 2
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises a ZeroDivisionError when is called on zero" do
|
13
|
+
lambda { 0.gcd2(2) }.should raise_error(ZeroDivisionError)
|
14
|
+
lambda { 2.gcd2(0) }.should raise_error(ZeroDivisionError)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
describe "Integer#prime_division" do
|
4
|
+
it "Performs a prime factorization of a positive integer" do
|
5
|
+
100.prime_division.should == [[2, 2], [5, 2]]
|
6
|
+
end
|
7
|
+
|
8
|
+
# Proper handling of negative integers has been added to MRI trunk
|
9
|
+
# in revision 24091. Prior to that, all versions of MRI returned nonsense.
|
10
|
+
ruby_bug "trunk@24091", "1.9.1" do
|
11
|
+
it "Performs a prime factorization of a negative integer" do
|
12
|
+
-26.prime_division.should == [[-1, 1], [2, 1], [13, 1]]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises a ZeroDivisionError when is called on zero" do
|
17
|
+
lambda { 0.prime_division }.should raise_error(ZeroDivisionError)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path('../shared/rsqrt', __FILE__)
|
2
|
+
|
3
|
+
describe "Math#rsqrt" do
|
4
|
+
it_behaves_like :mathn_math_rsqrt, :_, IncludesMath.new
|
5
|
+
|
6
|
+
it "is a private instance method" do
|
7
|
+
IncludesMath.should have_private_instance_method(:rsqrt)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "Math.rsqrt" do
|
12
|
+
it_behaves_like :mathn_math_rsqrt, :_, Math
|
13
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
describe :mathn_math_rsqrt, :shared => true do
|
5
|
+
it "returns the square root for Rational numbers" do
|
6
|
+
@object.send(:rsqrt, Rational(9, 25)).should == Rational(3, 5)
|
7
|
+
@object.send(:rsqrt, 16/64).should == Rational(1, 2)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns the square root for positive numbers" do
|
11
|
+
@object.send(:rsqrt, 1).should == 1
|
12
|
+
@object.send(:rsqrt, 4.0).should == 2.0
|
13
|
+
@object.send(:rsqrt, 12.34).should == Math.sqrt!(12.34)
|
14
|
+
end
|
15
|
+
|
16
|
+
ruby_version_is ""..."1.9" do
|
17
|
+
it "raises an Errno::EDOM if the argument is a negative number" do
|
18
|
+
lambda { @object.send(:rsqrt, -1) }.should raise_error(Errno::EDOM)
|
19
|
+
lambda { @object.send(:rsqrt, -4.0) }.should raise_error(Errno::EDOM)
|
20
|
+
lambda { @object.send(:rsqrt, -16/64) }.should raise_error(Errno::EDOM)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ruby_version_is "1.9" do
|
25
|
+
it "raises an Math::DomainError if the argument is a negative number" do
|
26
|
+
lambda { @object.send(:rsqrt, -1) }.should raise_error(Math::DomainError)
|
27
|
+
lambda { @object.send(:rsqrt, -4.0) }.should raise_error(Math::DomainError)
|
28
|
+
lambda { @object.send(:rsqrt, -16/64) }.should raise_error(Math::DomainError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
describe :mathn_math_sqrt, :shared => true do
|
5
|
+
it "returns the square root for Rational numbers" do
|
6
|
+
@object.send(:sqrt, Rational(9, 25)).should == Rational(3, 5)
|
7
|
+
@object.send(:sqrt, 16/64).should == Rational(1, 2)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns the square root for Complex numbers" do
|
11
|
+
@object.send(:sqrt, Complex(1, 0)).should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns the square root for positive numbers" do
|
15
|
+
@object.send(:sqrt, 1).should == 1
|
16
|
+
@object.send(:sqrt, 4.0).should == 2.0
|
17
|
+
@object.send(:sqrt, 12.34).should == Math.sqrt!(12.34)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns the square root for negative numbers" do
|
21
|
+
@object.send(:sqrt, -9).should == Complex(0, 3)
|
22
|
+
@object.send(:sqrt, -5.29).should == Complex(0, 2.3)
|
23
|
+
@object.send(:sqrt, -16/64).should == Complex(0, 1/2)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path('../shared/sqrt', __FILE__)
|
2
|
+
|
3
|
+
describe "Math#rsqrt" do
|
4
|
+
it_behaves_like :mathn_math_sqrt, :_, IncludesMath.new
|
5
|
+
|
6
|
+
it "is a private instance method" do
|
7
|
+
IncludesMath.should have_private_instance_method(:sqrt)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "Math.rsqrt" do
|
12
|
+
it_behaves_like :mathn_math_sqrt, :_, Math
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require'mathn'
|
2
|
+
|
3
|
+
describe "Prime#each" do
|
4
|
+
it "enumerates the elements" do
|
5
|
+
primes = Prime.new
|
6
|
+
result = []
|
7
|
+
|
8
|
+
primes.each { |p|
|
9
|
+
result << p
|
10
|
+
break if p > 10
|
11
|
+
}
|
12
|
+
|
13
|
+
result.should == [2, 3, 5, 7, 11]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "don't rewind the generator, each loop start at the current value" do
|
17
|
+
primes = Prime.new
|
18
|
+
primes.next
|
19
|
+
result = []
|
20
|
+
|
21
|
+
primes.each { |p|
|
22
|
+
result << p
|
23
|
+
break if p > 10
|
24
|
+
}
|
25
|
+
|
26
|
+
result.should == [3, 5, 7, 11]
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
describe "Prime.new" do
|
4
|
+
it "returns a new Prime number" do
|
5
|
+
Prime.new.should be_kind_of(Prime)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "raises a TypeError when is called with some arguments" do
|
9
|
+
lambda { Prime.new(1) }.should raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
ruby_version_is '1.9' do
|
4
|
+
describe "Kernel#Rational" do
|
5
|
+
it "returns an Integer if denominator divides numerator evenly" do
|
6
|
+
Rational(42,6).should == 7
|
7
|
+
Rational(42,6).should be_kind_of(Fixnum)
|
8
|
+
Rational(bignum_value,1).should == bignum_value
|
9
|
+
Rational(bignum_value,1).should be_kind_of(Bignum)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
ruby_version_is ''...'1.9' do
|
4
|
+
describe "Rational#** when passed [Rational]" do
|
5
|
+
it "returns Rational.new!(1, 1) when the passed argument is 0" do
|
6
|
+
(Rational.new!(3, 4) ** Rational.new!(0, 3)).should == Rational.new!(1,1)
|
7
|
+
(Rational.new!(-3, 4) ** Rational.new!(0, 3)).should == Rational.new!(1,1)
|
8
|
+
(Rational.new!(3, -4) ** Rational.new!(0, 3)).should == Rational.new!(1,1)
|
9
|
+
(Rational.new!(3, 4) ** Rational.new!(0, -3)).should == Rational.new!(1,1)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns Rational.new!(1, 1) when self is 1" do
|
13
|
+
(Rational.new!(1,1) ** Rational.new!(2, 3)).should == Rational.new!(1,1)
|
14
|
+
(Rational.new!(1,1) ** Rational.new!(-2, 3)).should == Rational.new!(1,1)
|
15
|
+
(Rational.new!(1,1) ** Rational.new!(2, -3)).should == Rational.new!(1,1)
|
16
|
+
(Rational.new!(1,1) ** Rational.new!(-2, -3)).should == Rational.new!(1,1)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns Rational.new!(0, 1) when self is 0" do
|
20
|
+
(Rational.new!(0,1) ** Rational.new!(2, 3)).should == Rational.new!(0,1)
|
21
|
+
(Rational.new!(0,1) ** Rational.new!(-2, 3)).should == Rational.new!(0,1)
|
22
|
+
(Rational.new!(0,1) ** Rational.new!(2, -3)).should == Rational.new!(0,1)
|
23
|
+
(Rational.new!(0,1) ** Rational.new!(-2, -3)).should == Rational.new!(0,1)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns a Complex number when self is negative" do
|
27
|
+
(Rational.new!(-1,2) ** Rational.new!(2, 3)).should be_close(Complex(-0.314980262473718, 0.545561817985861), TOLERANCE)
|
28
|
+
(Rational.new!(-1,2) ** Rational.new!(-2, 3)).should be_close(Complex(-0.793700525984099, -1.3747296369986), TOLERANCE)
|
29
|
+
(Rational.new!(-1,2) ** Rational.new!(2, -3)).should be_close(Complex(-0.793700525984099, -1.3747296369986), TOLERANCE)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Rational#** when passed [Integer]" do
|
34
|
+
it "returns the Rational value of self raised to the passed argument" do
|
35
|
+
(Rational.new!(3, 4) ** 4).should == Rational.new!(81, 256)
|
36
|
+
(Rational.new!(3, 4) ** -4).should == Rational.new!(256, 81)
|
37
|
+
(Rational.new!(-3, 4) ** -4).should == Rational.new!(256, 81)
|
38
|
+
(Rational.new!(3, -4) ** -4).should == Rational.new!(256, 81)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns Rational.new!(1, 1) when the passed argument is 0" do
|
42
|
+
(Rational.new!(3, 4) ** 0).should == Rational.new!(1, 1)
|
43
|
+
(Rational.new!(-3, 4) ** 0).should == Rational.new!(1, 1)
|
44
|
+
(Rational.new!(3, -4) ** 0).should == Rational.new!(1, 1)
|
45
|
+
|
46
|
+
(Rational.new!(bignum_value, 4) ** 0).should == Rational.new!(1, 1)
|
47
|
+
(Rational.new!(3, -bignum_value) ** 0).should == Rational.new!(1, 1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "Rational#** when passed [Float]" do
|
52
|
+
it "returns self converted to Float and raised to the passed argument" do
|
53
|
+
(Rational.new!(3, 1) ** 3.0).should == 27.0
|
54
|
+
(Rational.new!(3, 1) ** 1.5).should be_close(5.19615242270663, TOLERANCE)
|
55
|
+
(Rational.new!(3, 1) ** -1.5).should be_close(0.192450089729875, TOLERANCE)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns 1.0 when the passed argument is 0" do
|
59
|
+
(Rational.new!(3, 4) ** 0.0).should == 1.0
|
60
|
+
(Rational.new!(-3, 4) ** 0.0).should == 1.0
|
61
|
+
(Rational.new!(-3, 4) ** 0.0).should == 1.0
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns NaN if self is negative and the passed argument is not 0" do
|
65
|
+
(Rational.new!(-3, 2) ** 1.5).nan?.should be_true
|
66
|
+
(Rational.new!(3, -2) ** 1.5).nan?.should be_true
|
67
|
+
(Rational.new!(3, -2) ** -1.5).nan?.should be_true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
3
|
+
describe "Rational#inspect" do
|
4
|
+
ruby_version_is ''...'1.9' do
|
5
|
+
it "returns a string representation of self" do
|
6
|
+
Rational.new!(3, 4).inspect.should == "3/4"
|
7
|
+
Rational.new!(-5, 8).inspect.should == "-5/8"
|
8
|
+
Rational.new!(-1, -2).inspect.should == "1/2"
|
9
|
+
Rational.new!(0, 2).inspect.should == "0/2"
|
10
|
+
Rational.new!(bignum_value, 1).inspect.should == "#{bignum_value}/1"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ruby_version_is '1.9' do
|
15
|
+
it "returns a string representation of self" do
|
16
|
+
Rational(3, 4).inspect.should == "(3/4)"
|
17
|
+
Rational(-5, 8).inspect.should == "(-5/8)"
|
18
|
+
Rational(-1, -2).inspect.should == "(1/2)"
|
19
|
+
Rational(0, 2).inspect.should == "0"
|
20
|
+
Rational(bignum_value, 1).inspect.should == "#{bignum_value}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubysl-mathn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Shirai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubysl-prettyprint
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
description: Ruby standard library mathn.
|
70
|
+
email:
|
71
|
+
- brixen@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .travis.yml
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/mathn.rb
|
83
|
+
- lib/rubysl/mathn.rb
|
84
|
+
- lib/rubysl/mathn/mathn.rb
|
85
|
+
- lib/rubysl/mathn/version.rb
|
86
|
+
- rubysl-mathn.gemspec
|
87
|
+
- spec/bignum/exponent_spec.rb
|
88
|
+
- spec/complex/Complex_spec.rb
|
89
|
+
- spec/fixnum/exponent_spec.rb
|
90
|
+
- spec/float/exponent_spec.rb
|
91
|
+
- spec/integer/from_prime_division_spec.rb
|
92
|
+
- spec/integer/gcd2_spec.rb
|
93
|
+
- spec/integer/prime_division_spec.rb
|
94
|
+
- spec/math/fixtures/classes.rb
|
95
|
+
- spec/math/rsqrt_spec.rb
|
96
|
+
- spec/math/shared/rsqrt.rb
|
97
|
+
- spec/math/shared/sqrt.rb
|
98
|
+
- spec/math/sqrt_spec.rb
|
99
|
+
- spec/prime/each_spec.rb
|
100
|
+
- spec/prime/new_spec.rb
|
101
|
+
- spec/prime/next_spec.rb
|
102
|
+
- spec/rational/Rational_spec.rb
|
103
|
+
- spec/rational/exponent_spec.rb
|
104
|
+
- spec/rational/inspect_spec.rb
|
105
|
+
homepage: https://github.com/rubysl/rubysl-mathn
|
106
|
+
licenses:
|
107
|
+
- BSD
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.0.3
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Ruby standard library mathn.
|
129
|
+
test_files:
|
130
|
+
- spec/bignum/exponent_spec.rb
|
131
|
+
- spec/complex/Complex_spec.rb
|
132
|
+
- spec/fixnum/exponent_spec.rb
|
133
|
+
- spec/float/exponent_spec.rb
|
134
|
+
- spec/integer/from_prime_division_spec.rb
|
135
|
+
- spec/integer/gcd2_spec.rb
|
136
|
+
- spec/integer/prime_division_spec.rb
|
137
|
+
- spec/math/fixtures/classes.rb
|
138
|
+
- spec/math/rsqrt_spec.rb
|
139
|
+
- spec/math/shared/rsqrt.rb
|
140
|
+
- spec/math/shared/sqrt.rb
|
141
|
+
- spec/math/sqrt_spec.rb
|
142
|
+
- spec/prime/each_spec.rb
|
143
|
+
- spec/prime/new_spec.rb
|
144
|
+
- spec/prime/next_spec.rb
|
145
|
+
- spec/rational/Rational_spec.rb
|
146
|
+
- spec/rational/exponent_spec.rb
|
147
|
+
- spec/rational/inspect_spec.rb
|
148
|
+
has_rdoc:
|