NMath 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/NMath.gemspec +26 -0
- data/README.md +36 -0
- data/Rakefile +24 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/ext/NMath/NMath.c +9 -0
- data/ext/NMath/NMath.h +6 -0
- data/ext/NMath/extconf.rb +3 -0
- data/lib/NMath.rb +191 -0
- data/lib/NMath/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e44b023243014137f160b6d52005b15845bde685
|
4
|
+
data.tar.gz: 38422cf7534f2688bf8747a5cc3a14bfb7857a95
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f2faad447cce7e7817f00e17cb77e04373c12b51db00a297f06f99b26af707daa3fac5363c52af8ad0836e606528fb16d671983cff08cb59e592f90da929875
|
7
|
+
data.tar.gz: 2224430fd2a7f4787d25396775ffbc97a1f596a0837d4a98c1d348a58da00228f83dbc166becbb86a775ca67abf88f154749223962e57fec2fec1acc28624a62
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/NMath.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'NMath/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "NMath"
|
8
|
+
spec.version = NMath::VERSION
|
9
|
+
spec.authors = ["gogotanaka"]
|
10
|
+
spec.email = ["mail@tanakakazuki.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ NMath serves to make mathematical operations more precise in Ruby. }
|
13
|
+
spec.description = %q{ NMath serves to make mathematical operations more precise in Ruby. }
|
14
|
+
spec.homepage = "http://gogotanaka.me/"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.extensions = ["ext/NMath/extconf.rb"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rake-compiler"
|
25
|
+
spec.add_development_dependency "minitest"
|
26
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# NMath
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/NMath`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'NMath'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install NMath
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/NMath.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :test
|
11
|
+
require "rake/extensiontask"
|
12
|
+
|
13
|
+
task :build => :compile
|
14
|
+
|
15
|
+
Rake::ExtensionTask.new("NMath") do |ext|
|
16
|
+
ext.lib_dir = "lib/NMath"
|
17
|
+
end
|
18
|
+
|
19
|
+
task :compile_and_test do
|
20
|
+
Rake::Task['compile'].invoke
|
21
|
+
Rake::Task['test'].invoke
|
22
|
+
end
|
23
|
+
|
24
|
+
task default: :compile_and_test
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "NMath"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/ext/NMath/NMath.c
ADDED
data/ext/NMath/NMath.h
ADDED
data/lib/NMath.rb
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
require "NMath/version"
|
2
|
+
# Adapted from Mathn: https://github.com/ruby/ruby/blob/trunk/lib/mathn.rb
|
3
|
+
#--
|
4
|
+
# $Release Version: 0.5 $
|
5
|
+
# $Revision: 1.1.1.1.4.1 $
|
6
|
+
|
7
|
+
##
|
8
|
+
# = mathn
|
9
|
+
#
|
10
|
+
# mathn serves to make mathematical operations more precise in Ruby
|
11
|
+
# and to integrate other mathematical standard libraries.
|
12
|
+
#
|
13
|
+
# Without mathn:
|
14
|
+
#
|
15
|
+
# 3 / 2 => 1 # Integer
|
16
|
+
#
|
17
|
+
# With mathn:
|
18
|
+
#
|
19
|
+
# 3 / 2 => 3/2 # Rational
|
20
|
+
#
|
21
|
+
# mathn keeps value in exact terms.
|
22
|
+
#
|
23
|
+
# Without mathn:
|
24
|
+
#
|
25
|
+
# 20 / 9 * 3 * 14 / 7 * 3 / 2 # => 18
|
26
|
+
#
|
27
|
+
# With mathn:
|
28
|
+
#
|
29
|
+
# 20 / 9 * 3 * 14 / 7 * 3 / 2 # => 20
|
30
|
+
#
|
31
|
+
#
|
32
|
+
# When you require 'mathn', the libraries for Prime, CMath, Matrix and Vector
|
33
|
+
# are also loaded.
|
34
|
+
#
|
35
|
+
# == Copyright
|
36
|
+
#
|
37
|
+
# Author: Keiju ISHITSUKA (SHL Japan Inc.)
|
38
|
+
#--
|
39
|
+
# class Numeric follows to make this documentation findable in a reasonable
|
40
|
+
# location
|
41
|
+
|
42
|
+
class Numeric; end
|
43
|
+
|
44
|
+
require "cmath.rb"
|
45
|
+
require "matrix.rb"
|
46
|
+
require "prime.rb"
|
47
|
+
|
48
|
+
require "mathn/rational"
|
49
|
+
require "mathn/complex"
|
50
|
+
|
51
|
+
unless defined?(Math.exp!)
|
52
|
+
Object.instance_eval{remove_const :Math}
|
53
|
+
Math = CMath # :nodoc:
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# When mathn is required, Fixnum's division is enhanced to
|
58
|
+
# return more precise values from mathematical expressions.
|
59
|
+
#
|
60
|
+
# 2/3*3 # => 0
|
61
|
+
# require 'NMath'
|
62
|
+
# 2/3*3 # => 2
|
63
|
+
|
64
|
+
class Fixnum
|
65
|
+
remove_method :/
|
66
|
+
|
67
|
+
##
|
68
|
+
# +/+ defines the Rational division for Fixnum.
|
69
|
+
#
|
70
|
+
# 1/3 # => (1/3)
|
71
|
+
|
72
|
+
alias / quo
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# When mathn is required Bignum's division is enhanced to
|
77
|
+
# return more precise values from mathematical expressions.
|
78
|
+
#
|
79
|
+
# (2**72) / ((2**70) * 3) # => 4/3
|
80
|
+
|
81
|
+
class Bignum
|
82
|
+
remove_method :/
|
83
|
+
|
84
|
+
##
|
85
|
+
# +/+ defines the Rational division for Bignum.
|
86
|
+
#
|
87
|
+
# (2**72) / ((2**70) * 3) # => 4/3
|
88
|
+
|
89
|
+
alias / quo
|
90
|
+
end
|
91
|
+
|
92
|
+
##
|
93
|
+
# When mathn is required, the Math module changes as follows:
|
94
|
+
#
|
95
|
+
# Standard Math module behaviour:
|
96
|
+
# Math.sqrt(4/9) # => 0.0
|
97
|
+
# Math.sqrt(4.0/9.0) # => 0.666666666666667
|
98
|
+
# Math.sqrt(- 4/9) # => Errno::EDOM: Numerical argument out of domain - sqrt
|
99
|
+
#
|
100
|
+
# After require 'mathn', this is changed to:
|
101
|
+
#
|
102
|
+
# require 'mathn'
|
103
|
+
# Math.sqrt(4/9) # => 2/3
|
104
|
+
# Math.sqrt(4.0/9.0) # => 0.666666666666667
|
105
|
+
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
|
106
|
+
|
107
|
+
module Math
|
108
|
+
remove_method(:sqrt)
|
109
|
+
|
110
|
+
##
|
111
|
+
# Computes the square root of +a+. It makes use of Complex and
|
112
|
+
# Rational to have no rounding errors if possible.
|
113
|
+
#
|
114
|
+
# Math.sqrt(4/9) # => 2/3
|
115
|
+
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
|
116
|
+
# Math.sqrt(4.0/9.0) # => 0.666666666666667
|
117
|
+
|
118
|
+
def sqrt(a)
|
119
|
+
if a.kind_of?(Complex)
|
120
|
+
abs = sqrt(a.real*a.real + a.imag*a.imag)
|
121
|
+
x = sqrt((a.real + abs)/Rational(2))
|
122
|
+
y = sqrt((-a.real + abs)/Rational(2))
|
123
|
+
if a.imag >= 0
|
124
|
+
Complex(x, y)
|
125
|
+
else
|
126
|
+
Complex(x, -y)
|
127
|
+
end
|
128
|
+
elsif a.respond_to?(:nan?) and a.nan?
|
129
|
+
a
|
130
|
+
elsif a >= 0
|
131
|
+
rsqrt(a)
|
132
|
+
else
|
133
|
+
Complex(0,rsqrt(-a))
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# Compute square root of a non negative number. This method is
|
139
|
+
# internally used by +Math.sqrt+.
|
140
|
+
|
141
|
+
def rsqrt(a)
|
142
|
+
if a.kind_of?(Float)
|
143
|
+
sqrt!(a)
|
144
|
+
elsif a.kind_of?(Rational)
|
145
|
+
rsqrt(a.numerator)/rsqrt(a.denominator)
|
146
|
+
else
|
147
|
+
src = a
|
148
|
+
max = 2 ** 32
|
149
|
+
byte_a = [src & 0xffffffff]
|
150
|
+
# ruby's bug
|
151
|
+
while (src >= max) and (src >>= 32)
|
152
|
+
byte_a.unshift src & 0xffffffff
|
153
|
+
end
|
154
|
+
|
155
|
+
answer = 0
|
156
|
+
main = 0
|
157
|
+
side = 0
|
158
|
+
for elm in byte_a
|
159
|
+
main = (main << 32) + elm
|
160
|
+
side <<= 16
|
161
|
+
if answer != 0
|
162
|
+
if main * 4 < side * side
|
163
|
+
applo = main.div(side)
|
164
|
+
else
|
165
|
+
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
|
166
|
+
end
|
167
|
+
else
|
168
|
+
applo = sqrt!(main).to_i + 1
|
169
|
+
end
|
170
|
+
|
171
|
+
while (x = (side + applo) * applo) > main
|
172
|
+
applo -= 1
|
173
|
+
end
|
174
|
+
main -= x
|
175
|
+
answer = (answer << 16) + applo
|
176
|
+
side += applo * 2
|
177
|
+
end
|
178
|
+
if main == 0
|
179
|
+
answer
|
180
|
+
else
|
181
|
+
sqrt!(a)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
class << self
|
187
|
+
remove_method(:sqrt)
|
188
|
+
end
|
189
|
+
module_function :sqrt
|
190
|
+
module_function :rsqrt
|
191
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: NMath
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gogotanaka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-25 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: " NMath serves to make mathematical operations more precise in Ruby. "
|
70
|
+
email:
|
71
|
+
- mail@tanakakazuki.com
|
72
|
+
executables: []
|
73
|
+
extensions:
|
74
|
+
- ext/NMath/extconf.rb
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- NMath.gemspec
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- ext/NMath/NMath.c
|
86
|
+
- ext/NMath/NMath.h
|
87
|
+
- ext/NMath/extconf.rb
|
88
|
+
- lib/NMath.rb
|
89
|
+
- lib/NMath/version.rb
|
90
|
+
homepage: http://gogotanaka.me/
|
91
|
+
licenses: []
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.5
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: NMath serves to make mathematical operations more precise in Ruby.
|
113
|
+
test_files: []
|