integration 0.1.1 → 0.1.2
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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +6 -5
- data/README.md +67 -53
- data/Rakefile +5 -25
- data/integration.gemspec +4 -5
- data/lib/integration.rb +233 -58
- data/lib/integration/version.rb +3 -0
- data/lib/opencl/Makefile +29 -0
- data/lib/opencl/integration_host.c +151 -0
- data/lib/opencl/opencl_minimization.rb +251 -0
- data/lib/opencl/unidimensional_kernel.cl +47 -0
- metadata +7 -7
- data/.autotest +0 -23
- data/.rspec +0 -2
- data/Gemfile.lock +0 -38
- data/Manifest.txt +0 -9
- data/lib/symbolic.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 437b8e5c01a17b8168d503e3053429e5df10238c
|
4
|
+
data.tar.gz: 4b8fde1abeece2e42932cda9fa06779ee08ea274
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10f8aa27944b66c5e0817f3e6f9b18fc948be1539aef46931ac1ea803c869f243bb91b097a0edd6433cdafdce5728dc6dca9a32109ed6208cba1b65c6b66b47d
|
7
|
+
data.tar.gz: 92b5bcc05f2438e3d4c301119d984e82074353ca6e03975eaf972bc39669584e07a038479f9f38fc8a1370708b82d23cec228bb686333343a3c5726c750df871
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
-
|
4
|
-
-
|
5
|
-
-
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0
|
5
|
+
- 2.1
|
6
|
+
- 2.2
|
6
7
|
# - jruby-19mode # JRuby in 1.9 mode -> Problems with bigint
|
7
8
|
# - rbx-19mode
|
8
9
|
# script: bundle exec rspec spec
|
9
10
|
|
10
11
|
script:
|
11
|
-
bundle exec
|
12
|
-
|
12
|
+
bundle exec rake spec
|
13
|
+
|
13
14
|
before_install:
|
14
15
|
- sudo apt-get update -qq
|
15
16
|
- sudo apt-get install -y libgsl0-dev
|
data/README.md
CHANGED
@@ -1,60 +1,74 @@
|
|
1
|
-
#
|
1
|
+
# Integration
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/SciRuby/integration)
|
4
|
+
[](https://codeclimate.com/github/SciRuby/integration)
|
4
5
|
|
5
|
-
|
6
|
+
Numerical integration for Ruby, with a simple interface. Several different integration methods are implemented.
|
6
7
|
|
7
|
-
|
8
|
+
## Installation
|
8
9
|
|
9
|
-
|
10
|
+
```bash
|
11
|
+
$ gem install integration
|
12
|
+
```
|
13
|
+
|
14
|
+
You can install GSL (please use your package manager) for better performance and support for infinite bounds. After successfully installing the library:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
$ gem install rb-gsl
|
18
|
+
```
|
19
|
+
|
20
|
+
## Documentation
|
21
|
+
|
22
|
+
You can see the latest documentation for Integration at Rubydoc: [rubydoc.info/github/sciruby/integration](http://www.rubydoc.info/github/sciruby/integration).
|
23
|
+
|
24
|
+
## Features
|
10
25
|
|
11
26
|
* Use only one method: Integration.integrate
|
12
|
-
* Rectangular, Trapezoidal, Simpson, Adaptive quadrature, Monte Carlo and Romberg integration methods available on pure Ruby.
|
13
|
-
* If available, uses Ruby/GSL QNG non-adaptive Gauss-Kronrod integration and QAG adaptive integration, with support for Infinity+ and Infinity-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
##
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
Except as contained in this notice, the name of the Beng shall not
|
58
|
-
be used in advertising or otherwise to promote the sale, use or other dealings
|
59
|
-
in this Software without prior written authorization from Beng.
|
27
|
+
* Rectangular, Trapezoidal, Simpson, Adaptive quadrature, Monte Carlo and Romberg integration methods available on pure Ruby.
|
28
|
+
* If available, uses Ruby/GSL QNG non-adaptive Gauss-Kronrod integration and QAG adaptive integration, with support for Infinity+ and Infinity-
|
29
|
+
|
30
|
+
## Examples
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# Integrate x^2 in [1, 2] using Simpson's rule.
|
34
|
+
Integration.integrate(1, 2, tolerance: 1e-10, method: :simpson) do |x|
|
35
|
+
x**2
|
36
|
+
end
|
37
|
+
# => 2.333333
|
38
|
+
|
39
|
+
# Support for infinity bounds with GSL QAG adaptative integration.
|
40
|
+
|
41
|
+
normal_pdf = lambda do |x|
|
42
|
+
(1 / Math.sqrt(2 * Math::PI)) * Math.exp(-0.5 * (x**2))
|
43
|
+
end
|
44
|
+
|
45
|
+
Integration.integrate(Integration::MInfinity, 0, tolerance: 1e-10, &normal_pdf) # => 0.5
|
46
|
+
Integration.integrate(0, Integration::Infinity , tolerance: 1e-10, &normal_pdf) # => 0.5
|
47
|
+
```
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
Copyright (c) 2005 Beng
|
52
|
+
2011-2015 clbustos
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
55
|
+
copy of this software and associated documentation files (the "Software"),
|
56
|
+
to deal in the Software without restriction, including without limitation
|
57
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
58
|
+
and/or sell copies of the Software, and to permit persons to whom the
|
59
|
+
Software is furnished to do so, subject to the following conditions:
|
60
|
+
|
61
|
+
The above copyright notice and this permission notice shall be included in
|
62
|
+
all copies or substantial portions of the Software.
|
63
|
+
|
64
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
65
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
66
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
67
|
+
THE DEVELOPERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
68
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
69
|
+
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
70
|
+
SOFTWARE.
|
60
71
|
|
72
|
+
Except as contained in this notice, the name of the Beng shall not
|
73
|
+
be used in advertising or otherwise to promote the sale, use or other dealings
|
74
|
+
in this Software without prior written authorization from Beng.
|
data/Rakefile
CHANGED
@@ -1,14 +1,9 @@
|
|
1
|
-
|
2
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/lib/"))
|
3
|
-
require 'rubygems'
|
4
|
-
require 'integration'
|
5
|
-
require 'bundler'
|
6
|
-
|
7
|
-
Bundler::GemHelper.install_tasks
|
8
|
-
|
9
|
-
|
10
|
-
gemspec = eval(IO.read("integration.gemspec"))
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
11
2
|
|
3
|
+
require 'integration/version'
|
4
|
+
require 'bundler'
|
5
|
+
require 'bundler/gem_tasks'
|
6
|
+
require 'rspec/core/rake_task'
|
12
7
|
|
13
8
|
begin
|
14
9
|
Bundler.setup(:default, :development)
|
@@ -18,28 +13,13 @@ rescue Bundler::BundlerError => e
|
|
18
13
|
exit e.status_code
|
19
14
|
end
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
require "rubygems/package_task"
|
24
|
-
Gem::PackageTask.new(gemspec).define
|
25
|
-
|
26
|
-
desc "install the gem locally"
|
27
|
-
task :install => [:package] do
|
28
|
-
sh %{gem install pkg/integration-#{Integration::VERSION}.gem}
|
29
|
-
end
|
30
|
-
|
31
|
-
require 'rspec/core/rake_task'
|
32
|
-
require 'rspec/core'
|
33
|
-
require 'rspec/core/rake_task'
|
34
16
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
35
17
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
36
18
|
end
|
37
19
|
|
38
|
-
|
39
20
|
desc "Open an irb session preloaded with integration"
|
40
21
|
task :console do
|
41
22
|
sh "irb -rubygems -I lib -r integration.rb"
|
42
23
|
end
|
43
24
|
|
44
25
|
task :default => :spec
|
45
|
-
# vim: syntax=ruby
|
data/integration.gemspec
CHANGED
@@ -1,16 +1,15 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib/', __FILE__)
|
3
|
-
$:.unshift lib unless $:.include?(lib)
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
4
2
|
|
3
|
+
require 'integration/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
6
|
s.name = "integration"
|
8
|
-
s.version =
|
7
|
+
s.version = Integration::VERSION
|
9
8
|
s.authors = ["Claudio Bustos","Ben Gimpert"]
|
10
9
|
s.description = "Numerical integration for Ruby, with a simple interface"
|
11
10
|
s.email = ["clbustos@gmail.com", "No Email"]
|
12
11
|
s.files = `git ls-files`.split("\n")
|
13
|
-
s.test_files = `git ls-files -- {test,spec,features,benchmark}/*`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test,spec,features,benchmark}/*`.split("\n")
|
14
13
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
14
|
s.require_paths = ["lib"]
|
16
15
|
s.summary = "A suite for integration in Ruby"
|
data/lib/integration.rb
CHANGED
@@ -7,10 +7,10 @@
|
|
7
7
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
8
8
|
# and/or sell copies of the Software, and to permit persons to whom the
|
9
9
|
# Software is furnished to do so, subject to the following conditions:
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# The above copyright notice and this permission notice shall be included in
|
12
12
|
# all copies or substantial portions of the Software.
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
15
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
16
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
@@ -18,33 +18,32 @@
|
|
18
18
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
19
19
|
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
20
|
# SOFTWARE.
|
21
|
-
#
|
21
|
+
#
|
22
22
|
# Except as contained in this notice, the name of the Beng shall not
|
23
23
|
# be used in advertising or otherwise to promote the sale, use or other dealings
|
24
24
|
# in this Software without prior written authorization from Beng.
|
25
25
|
|
26
26
|
# Diverse integration methods
|
27
27
|
# Use Integration.integrate as wrapper to direct access to methods
|
28
|
-
#
|
29
|
-
# Method API
|
28
|
+
#
|
29
|
+
# Method API
|
30
30
|
#
|
31
31
|
|
32
32
|
class Integration
|
33
|
-
VERSION = '0.2.0'
|
34
33
|
# Minus Infinity
|
35
34
|
MInfinity=:minfinity
|
36
35
|
# Infinity
|
37
36
|
Infinity=:infinity
|
38
37
|
class << self
|
39
|
-
|
38
|
+
|
40
39
|
# Create a method 'has_<library>' on Module
|
41
40
|
# which require a library and return true or false
|
42
|
-
# according to success of failure
|
41
|
+
# according to success of failure
|
43
42
|
def create_has_library(library) #:nodoc:
|
44
43
|
define_singleton_method("has_#{library}?") do
|
45
44
|
cv="@@#{library}"
|
46
45
|
if !class_variable_defined? cv
|
47
|
-
begin
|
46
|
+
begin
|
48
47
|
require library.to_s
|
49
48
|
class_variable_set(cv, true)
|
50
49
|
rescue LoadError
|
@@ -60,7 +59,7 @@ class Integration
|
|
60
59
|
# * Ayres : Outline of calculus
|
61
60
|
def rectangle(t1, t2, n, &f)
|
62
61
|
d=(t2-t1) / n.to_f
|
63
|
-
n.times.inject(0) {|ac,i|
|
62
|
+
n.times.inject(0) {|ac,i|
|
64
63
|
ac+f[t1+d*(i+0.5)]
|
65
64
|
}*d
|
66
65
|
end
|
@@ -72,35 +71,44 @@ class Integration
|
|
72
71
|
def trapezoid(t1, t2, n, &f)
|
73
72
|
d=(t2-t1) / n.to_f
|
74
73
|
(d/2.0)*(f[t1]+
|
75
|
-
2*(1..(n-1)).inject(0){|ac,i|
|
74
|
+
2*(1..(n-1)).inject(0){|ac,i|
|
76
75
|
ac+f[t1+d*i]
|
77
76
|
}+f[t2])
|
78
77
|
end
|
78
|
+
|
79
79
|
# Simpson's rule
|
80
80
|
# +n+ implies number of subdivisions
|
81
81
|
# Source:
|
82
82
|
# * Ayres : Outline of calculus
|
83
83
|
def simpson(t1, t2, n, &f)
|
84
84
|
n += 1 unless n % 2 == 0
|
85
|
-
d=(t2-t1) / n.to_f
|
85
|
+
d=(t2-t1) / n.to_f
|
86
86
|
out= (d / 3.0)*(f[t1.to_f].to_f+
|
87
87
|
((1..(n-1)).inject(0) {|ac,i|
|
88
|
-
ac+((i%2==0) ? 2 : 4)*f[t1+d*i]
|
88
|
+
ac+((i%2==0) ? 2 : 4)*f[t1+d*i]
|
89
89
|
})+f[t2.to_f].to_f)
|
90
90
|
out
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
|
+
# Simpson's 3/8 Rule
|
94
|
+
# +n+ implies number of subdivisions
|
95
|
+
# Source:
|
96
|
+
# * Burden, Richard L. and Faires, J. Douglas (2000): Numerical Analysis (7th ed.). Brooks/Cole
|
93
97
|
def simpson3by8(t1, t2, n, &f)
|
94
|
-
d = (t2-t1) / n.to_f
|
98
|
+
d = (t2-t1) / n.to_f
|
95
99
|
ac = 0
|
96
100
|
(0..n-1).each do |i|
|
97
101
|
ac+=(d/8.0)*(f[t1+i*d]+3*f[t1+i*d+d/3]+3*f[t1+i*d+2*d/3]+f[t1+(i+1)*d])
|
98
102
|
end
|
99
103
|
ac
|
100
104
|
end
|
101
|
-
|
105
|
+
|
106
|
+
# Boole's Rule
|
107
|
+
# +n+ implies number of subdivisions
|
108
|
+
# Source:
|
109
|
+
# Weisstein, Eric W. "Boole's Rule." From MathWorld—A Wolfram Web Resource
|
102
110
|
def boole(t1, t2, n, &f)
|
103
|
-
d = (t2-t1) / n.to_f
|
111
|
+
d = (t2-t1) / n.to_f
|
104
112
|
ac = 0
|
105
113
|
(0..n-1).each do |i|
|
106
114
|
ac+=(d/90.0)*(7*f[t1+i*d]+32*f[t1+i*d+d/4]+12*f[t1+i*d+d/2]+32*f[t1+i*d+3*d/4]+7*f[t1+(i+1)*d])
|
@@ -108,26 +116,38 @@ class Integration
|
|
108
116
|
ac
|
109
117
|
end
|
110
118
|
|
111
|
-
#
|
119
|
+
# Open Trapezoid method
|
120
|
+
# +n+ implies number of subdivisions
|
121
|
+
# Values computed at mid point and end point instead of starting points
|
112
122
|
def open_trapezoid(t1, t2, n, &f)
|
113
|
-
d = (t2-t1) / n.to_f
|
123
|
+
d = (t2-t1) / n.to_f
|
114
124
|
ac = 0
|
115
125
|
(0..n-1).each do |i|
|
116
126
|
ac+=(d/2.0)*(f[t1+i*d+d/3]+f[t1+i*d+2*d/3])
|
117
127
|
end
|
118
128
|
ac
|
119
129
|
end
|
120
|
-
|
130
|
+
|
131
|
+
# Milne's Method
|
132
|
+
# +n+ implies number of subdivisions
|
133
|
+
# Source:
|
134
|
+
# Abramowitz, M. and Stegun, I. A. (Eds.).
|
135
|
+
# Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables,
|
136
|
+
# 9th printing. New York: Dover, pp. 896-897, 1972.
|
121
137
|
def milne(t1, t2, n, &f)
|
122
|
-
d = (t2-t1) / n.to_f
|
138
|
+
d = (t2-t1) / n.to_f
|
123
139
|
ac = 0
|
124
140
|
(0..n-1).each do |i|
|
125
141
|
ac+=(d/3.0)*(2*f[t1+i*d+d/4]-f[t1+i*d+d/2]+2*f[t1+i*d+3*d/4])
|
126
142
|
end
|
127
143
|
ac
|
128
144
|
end
|
129
|
-
|
130
|
-
|
145
|
+
|
146
|
+
# Adaptive Quadrature
|
147
|
+
# Calls the Simpson's rule recursively on subintervals
|
148
|
+
# in case the error exceeds the desired tolerance
|
149
|
+
# +tolerance+ is the desired tolerance of error
|
150
|
+
def adaptive_quadrature(a, b, tolerance)
|
131
151
|
h = (b.to_f - a) / 2
|
132
152
|
fa = yield(a)
|
133
153
|
fc = yield(a + h)
|
@@ -151,7 +171,9 @@ class Integration
|
|
151
171
|
}
|
152
172
|
return helper.call(a, b, fa, fb, fc, h, s, 1)
|
153
173
|
end
|
154
|
-
|
174
|
+
|
175
|
+
# Gaussian Quadrature
|
176
|
+
# n-point Gaussian quadrature rule gives an exact result for polynomials of degree 2n − 1 or less
|
155
177
|
def gauss(t1, t2, n)
|
156
178
|
case n
|
157
179
|
when 1
|
@@ -187,50 +209,195 @@ class Integration
|
|
187
209
|
else
|
188
210
|
raise "Invalid number of spaced abscissas #{n}, should be 1-10"
|
189
211
|
end
|
190
|
-
|
212
|
+
|
191
213
|
sum = 0
|
192
214
|
(0...n).each do |i|
|
193
|
-
t = ((t1.to_f + t2) / 2.0) + (((t2 - t1) / 2.0) * z[i])
|
215
|
+
t = ((t1.to_f + t2) / 2.0) + (((t2 - t1) / 2.0) * z[i])
|
194
216
|
sum += w[i] * yield(t)
|
195
217
|
end
|
196
218
|
return ((t2 - t1) / 2.0) * sum
|
197
219
|
end
|
198
|
-
|
220
|
+
|
221
|
+
# Gauss Kronrod Rule:
|
222
|
+
# Provides a 3n+1 order estimate while re-using the function values of a lower-order(n order) estimate
|
223
|
+
# Source:
|
224
|
+
# "Gauss–Kronrod quadrature formula", Encyclopedia of Mathematics, Springer, ISBN 978-1-55608-010-4
|
199
225
|
def gauss_kronrod(t1,t2,n,points)
|
200
226
|
#g7k15
|
201
227
|
case points
|
202
228
|
when 15
|
203
|
-
|
204
|
-
|
229
|
+
|
230
|
+
z = [-0.9914553711208126, -0.9491079123427585, -0.8648644233597691,
|
231
|
+
-0.7415311855993945, -0.5860872354676911, -0.4058451513773972,
|
232
|
+
-0.20778495500789848, 0.0, 0.20778495500789848,
|
233
|
+
0.4058451513773972, 0.5860872354676911, 0.7415311855993945,
|
234
|
+
0.8648644233597691, 0.9491079123427585, 0.9914553711208126]
|
235
|
+
|
236
|
+
w = [0.022935322010529224, 0.06309209262997856, 0.10479001032225019,
|
237
|
+
0.14065325971552592, 0.1690047266392679, 0.19035057806478542,
|
238
|
+
0.20443294007529889, 0.20948214108472782, 0.20443294007529889,
|
239
|
+
0.19035057806478542, 0.1690047266392679, 0.14065325971552592,
|
240
|
+
0.10479001032225019, 0.06309209262997856, 0.022935322010529224]
|
241
|
+
|
205
242
|
when 21
|
206
243
|
#g10k21
|
207
|
-
|
208
|
-
|
244
|
+
|
245
|
+
z = [-0.9956571630258081, -0.9739065285171717, -0.9301574913557082,
|
246
|
+
-0.8650633666889845, -0.7808177265864169, -0.6794095682990244,
|
247
|
+
-0.5627571346686047, -0.4333953941292472, -0.2943928627014602,
|
248
|
+
-0.14887433898163122, 0.0, 0.14887433898163122,
|
249
|
+
0.2943928627014602, 0.4333953941292472, 0.5627571346686047,
|
250
|
+
0.6794095682990244, 0.7808177265864169, 0.8650633666889845,
|
251
|
+
0.9301574913557082, 0.9739065285171717, 0.9956571630258081]
|
252
|
+
|
253
|
+
w = [0.011694638867371874, 0.032558162307964725,
|
254
|
+
0.054755896574351995, 0.07503967481091996, 0.0931254545836976,
|
255
|
+
0.10938715880229764, 0.12349197626206584, 0.13470921731147334,
|
256
|
+
0.14277593857706009, 0.14773910490133849, 0.1494455540029169,
|
257
|
+
0.14773910490133849, 0.14277593857706009, 0.13470921731147334,
|
258
|
+
0.12349197626206584, 0.10938715880229764, 0.0931254545836976,
|
259
|
+
0.07503967481091996, 0.054755896574351995, 0.032558162307964725,
|
260
|
+
0.011694638867371874]
|
261
|
+
|
209
262
|
when 31
|
210
263
|
#g15k31
|
211
|
-
|
212
|
-
|
264
|
+
|
265
|
+
z = [-0.9980022986933971, -0.9879925180204854, -0.9677390756791391,
|
266
|
+
-0.937273392400706, -0.8972645323440819, -0.8482065834104272,
|
267
|
+
-0.790418501442466, -0.7244177313601701, -0.650996741297417,
|
268
|
+
-0.5709721726085388, -0.4850818636402397, -0.3941513470775634,
|
269
|
+
-0.29918000715316884, -0.20119409399743451, -0.1011420669187175,
|
270
|
+
0.0, 0.1011420669187175, 0.20119409399743451,
|
271
|
+
0.29918000715316884, 0.3941513470775634, 0.4850818636402397,
|
272
|
+
0.5709721726085388, 0.650996741297417, 0.7244177313601701,
|
273
|
+
0.790418501442466, 0.8482065834104272, 0.8972645323440819,
|
274
|
+
0.937273392400706, 0.9677390756791391, 0.9879925180204854,
|
275
|
+
0.9980022986933971]
|
276
|
+
|
277
|
+
w = [0.005377479872923349, 0.015007947329316122, 0.02546084732671532,
|
278
|
+
0.03534636079137585, 0.04458975132476488, 0.05348152469092809,
|
279
|
+
0.06200956780067064, 0.06985412131872826, 0.07684968075772038,
|
280
|
+
0.08308050282313302, 0.08856444305621176, 0.09312659817082532,
|
281
|
+
0.09664272698362368, 0.09917359872179196, 0.10076984552387559,
|
282
|
+
0.10133000701479154, 0.10076984552387559, 0.09917359872179196,
|
283
|
+
0.09664272698362368, 0.09312659817082532, 0.08856444305621176,
|
284
|
+
0.08308050282313302, 0.07684968075772038, 0.06985412131872826,
|
285
|
+
0.06200956780067064, 0.05348152469092809, 0.04458975132476488,
|
286
|
+
0.03534636079137585, 0.02546084732671532, 0.015007947329316122,
|
287
|
+
0.005377479872923349]
|
288
|
+
|
213
289
|
when 41
|
214
290
|
#g20k41
|
215
|
-
|
216
|
-
|
291
|
+
|
292
|
+
z = [-0.9988590315882777, -0.9931285991850949, -0.9815078774502503,
|
293
|
+
-0.9639719272779138, -0.9408226338317548, -0.912234428251326,
|
294
|
+
-0.878276811252282, -0.8391169718222188, -0.7950414288375512,
|
295
|
+
-0.7463319064601508, -0.6932376563347514, -0.636053680726515,
|
296
|
+
-0.5751404468197103, -0.5108670019508271, -0.4435931752387251,
|
297
|
+
-0.37370608871541955, -0.301627868114913, -0.22778585114164507,
|
298
|
+
-0.15260546524092267, -0.07652652113349734, 0.0,
|
299
|
+
0.07652652113349734, 0.15260546524092267, 0.22778585114164507,
|
300
|
+
0.301627868114913, 0.37370608871541955, 0.4435931752387251,
|
301
|
+
0.5108670019508271, 0.5751404468197103, 0.636053680726515,
|
302
|
+
0.6932376563347514, 0.7463319064601508, 0.7950414288375512,
|
303
|
+
0.8391169718222188, 0.878276811252282, 0.912234428251326,
|
304
|
+
0.9408226338317548, 0.9639719272779138, 0.9815078774502503,
|
305
|
+
0.9931285991850949, 0.9988590315882777]
|
306
|
+
|
307
|
+
w = [0.0030735837185205317, 0.008600269855642943,
|
308
|
+
0.014626169256971253, 0.020388373461266523, 0.02588213360495116,
|
309
|
+
0.0312873067770328, 0.036600169758200796, 0.041668873327973685,
|
310
|
+
0.04643482186749767, 0.05094457392372869, 0.05519510534828599,
|
311
|
+
0.05911140088063957, 0.06265323755478117, 0.06583459713361842,
|
312
|
+
0.06864867292852161, 0.07105442355344407, 0.07303069033278667,
|
313
|
+
0.07458287540049918, 0.07570449768455667, 0.07637786767208074,
|
314
|
+
0.07660071191799965, 0.07637786767208074, 0.07570449768455667,
|
315
|
+
0.07458287540049918, 0.07303069033278667, 0.07105442355344407,
|
316
|
+
0.06864867292852161, 0.06583459713361842, 0.06265323755478117,
|
317
|
+
0.05911140088063957, 0.05519510534828599, 0.05094457392372869,
|
318
|
+
0.04643482186749767, 0.041668873327973685, 0.036600169758200796,
|
319
|
+
0.0312873067770328, 0.02588213360495116, 0.020388373461266523,
|
320
|
+
0.014626169256971253, 0.008600269855642943,
|
321
|
+
0.0030735837185205317]
|
322
|
+
|
217
323
|
when 61
|
218
324
|
#g30k61
|
219
|
-
|
220
|
-
|
325
|
+
|
326
|
+
z = [-0.9994844100504906, -0.9968934840746495, -0.9916309968704046,
|
327
|
+
-0.9836681232797472, -0.9731163225011262, -0.9600218649683075,
|
328
|
+
-0.94437444474856, -0.9262000474292743, -0.9055733076999078,
|
329
|
+
-0.8825605357920527, -0.8572052335460612, -0.8295657623827684,
|
330
|
+
-0.799727835821839, -0.7677774321048262, -0.7337900624532268,
|
331
|
+
-0.6978504947933158, -0.6600610641266269, -0.6205261829892429,
|
332
|
+
-0.5793452358263617, -0.5366241481420199, -0.49248046786177857,
|
333
|
+
-0.44703376953808915, -0.4004012548303944, -0.3527047255308781,
|
334
|
+
-0.30407320227362505, -0.25463692616788985,
|
335
|
+
-0.20452511668230988, -0.15386991360858354,
|
336
|
+
-0.10280693796673702, -0.0514718425553177, 0.0,
|
337
|
+
0.0514718425553177, 0.10280693796673702, 0.15386991360858354,
|
338
|
+
0.20452511668230988, 0.25463692616788985, 0.30407320227362505,
|
339
|
+
0.3527047255308781, 0.4004012548303944, 0.44703376953808915,
|
340
|
+
0.49248046786177857, 0.5366241481420199, 0.5793452358263617,
|
341
|
+
0.6205261829892429, 0.6600610641266269, 0.6978504947933158,
|
342
|
+
0.7337900624532268, 0.7677774321048262, 0.799727835821839,
|
343
|
+
0.8295657623827684, 0.8572052335460612, 0.8825605357920527,
|
344
|
+
0.9055733076999078, 0.9262000474292743, 0.94437444474856,
|
345
|
+
0.9600218649683075, 0.9731163225011262, 0.9836681232797472,
|
346
|
+
0.9916309968704046, 0.9968934840746495, 0.9994844100504906]
|
347
|
+
|
348
|
+
w = [0.0013890136986770077, 0.003890461127099884,
|
349
|
+
0.0066307039159312926, 0.009273279659517764,
|
350
|
+
0.011823015253496341, 0.014369729507045804, 0.01692088918905327,
|
351
|
+
0.019414141193942382, 0.021828035821609193, 0.0241911620780806,
|
352
|
+
0.0265099548823331, 0.02875404876504129, 0.030907257562387762,
|
353
|
+
0.03298144705748372, 0.034979338028060025, 0.03688236465182123,
|
354
|
+
0.038678945624727595, 0.040374538951535956,
|
355
|
+
0.041969810215164244, 0.04345253970135607, 0.04481480013316266,
|
356
|
+
0.04605923827100699, 0.04718554656929915, 0.04818586175708713,
|
357
|
+
0.04905543455502978, 0.04979568342707421, 0.05040592140278235,
|
358
|
+
0.05088179589874961, 0.051221547849258774, 0.05142612853745902,
|
359
|
+
0.05149472942945157, 0.05142612853745902, 0.051221547849258774,
|
360
|
+
0.05088179589874961, 0.05040592140278235, 0.04979568342707421,
|
361
|
+
0.04905543455502978, 0.04818586175708713, 0.04718554656929915,
|
362
|
+
0.04605923827100699, 0.04481480013316266, 0.04345253970135607,
|
363
|
+
0.041969810215164244, 0.040374538951535956,
|
364
|
+
0.038678945624727595, 0.03688236465182123, 0.034979338028060025,
|
365
|
+
0.03298144705748372, 0.030907257562387762, 0.02875404876504129,
|
366
|
+
0.0265099548823331, 0.0241911620780806, 0.021828035821609193,
|
367
|
+
0.019414141193942382, 0.01692088918905327, 0.014369729507045804,
|
368
|
+
0.011823015253496341, 0.009273279659517764,
|
369
|
+
0.0066307039159312926, 0.003890461127099884,
|
370
|
+
0.0013890136986770077]
|
371
|
+
|
221
372
|
else # using 15 point quadrature
|
373
|
+
|
222
374
|
n = 15
|
223
|
-
|
224
|
-
|
375
|
+
|
376
|
+
z = [-0.9914553711208126, -0.9491079123427585, -0.8648644233597691,
|
377
|
+
-0.7415311855993945, -0.5860872354676911, -0.4058451513773972,
|
378
|
+
-0.20778495500789848, 0.0, 0.20778495500789848,
|
379
|
+
0.4058451513773972, 0.5860872354676911, 0.7415311855993945,
|
380
|
+
0.8648644233597691, 0.9491079123427585, 0.9914553711208126]
|
381
|
+
|
382
|
+
w = [0.022935322010529224, 0.06309209262997856, 0.10479001032225019,
|
383
|
+
0.14065325971552592, 0.1690047266392679, 0.19035057806478542,
|
384
|
+
0.20443294007529889, 0.20948214108472782, 0.20443294007529889,
|
385
|
+
0.19035057806478542, 0.1690047266392679, 0.14065325971552592,
|
386
|
+
0.10479001032225019, 0.06309209262997856, 0.022935322010529224]
|
387
|
+
|
225
388
|
end
|
389
|
+
|
226
390
|
sum = 0
|
227
391
|
(0...n).each do |i|
|
228
392
|
t = ((t1.to_f + t2) / 2.0) + (((t2 - t1) / 2.0) * z[i])
|
229
393
|
sum += w[i] * yield(t)
|
230
394
|
end
|
231
|
-
|
395
|
+
|
396
|
+
((t2 - t1) / 2.0) * sum
|
232
397
|
end
|
233
|
-
|
398
|
+
|
399
|
+
# Romberg Method:
|
400
|
+
# It is obtained by applying extrapolation techniques repeatedly on the trapezoidal rule
|
234
401
|
def romberg(a, b, tolerance,max_iter=20)
|
235
402
|
# NOTE one-based arrays are used for convenience
|
236
403
|
h = b.to_f - a
|
@@ -251,8 +418,10 @@ class Integration
|
|
251
418
|
end
|
252
419
|
r[j][j]
|
253
420
|
end
|
254
|
-
|
255
|
-
#
|
421
|
+
|
422
|
+
# Monte Carlo:
|
423
|
+
# Uses a non deterministic(probabilistic) approach for calculation of definite integrals
|
424
|
+
# Estimates the integral by randomly choosing points in a set and then calculating the number of points that fall in the desired area
|
256
425
|
def monte_carlo(t1, t2, n)
|
257
426
|
width = (t2 - t1).to_f
|
258
427
|
height = nil
|
@@ -269,15 +438,19 @@ class Integration
|
|
269
438
|
end
|
270
439
|
return (width * height) * area_ratio
|
271
440
|
end
|
441
|
+
|
272
442
|
def is_infinite?(v)
|
273
|
-
v==Infinity
|
443
|
+
v == Infinity || v == MInfinity
|
274
444
|
end
|
445
|
+
|
275
446
|
# Methods available on pure ruby
|
276
447
|
RUBY_METHOD=[:rectangle,:trapezoid,:simpson, :adaptive_quadrature , :gauss, :romberg, :monte_carlo, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, :milne]
|
448
|
+
|
277
449
|
# Methods available with Ruby/GSL library
|
278
450
|
GSL_METHOD=[:qng, :qag]
|
451
|
+
|
279
452
|
# Get the integral for a function +f+, with bounds +t1+ and
|
280
|
-
# +t2+ given a hash of +options+.
|
453
|
+
# +t2+ given a hash of +options+.
|
281
454
|
# If Ruby/GSL is available, you could use +Integration::Minfinity+
|
282
455
|
# and +Integration::Infinity+ as bounds. Method
|
283
456
|
# Options are
|
@@ -285,7 +458,7 @@ class Integration
|
|
285
458
|
# Default: 1e-10
|
286
459
|
# [:initial_step] Initial number of subdivitions
|
287
460
|
# [:step] Subdivitions increment on each iteration
|
288
|
-
# [:method] Integration method.
|
461
|
+
# [:method] Integration method.
|
289
462
|
# Methods are
|
290
463
|
# [:rectangle] for [:initial_step+:step*iteration] quadrilateral subdivisions
|
291
464
|
# [:trapezoid] for [:initial_step+:step*iteration] trapezoid-al subdivisions
|
@@ -304,7 +477,7 @@ class Integration
|
|
304
477
|
lower_bound=t1
|
305
478
|
upper_bound=t2
|
306
479
|
options[:method]=:qag if options[:method].nil?
|
307
|
-
else
|
480
|
+
else
|
308
481
|
lower_bound = [t1, t2].min
|
309
482
|
upper_bound = [t1, t2].max
|
310
483
|
end
|
@@ -320,35 +493,37 @@ class Integration
|
|
320
493
|
raise "Unknown integration method \"#{options[:method]}\""
|
321
494
|
end
|
322
495
|
end
|
496
|
+
|
323
497
|
# TODO: Document method
|
324
|
-
def integrate_gsl(lower_bound,upper_bound,options,&f)
|
325
|
-
|
498
|
+
def integrate_gsl(lower_bound,upper_bound,options,&f)
|
499
|
+
|
326
500
|
f = GSL::Function.alloc(&f)
|
327
501
|
method=options[:method]
|
328
502
|
tolerance=options[:tolerance]
|
329
|
-
|
503
|
+
|
330
504
|
if(method==:qag)
|
331
505
|
w = GSL::Integration::Workspace.alloc()
|
332
|
-
if(is_infinite?(lower_bound) and is_infinite?(upper_bound))
|
506
|
+
if(is_infinite?(lower_bound) and is_infinite?(upper_bound))
|
333
507
|
#puts "ambos"
|
334
|
-
val=f.qagi([tolerance,0.0], 1000, w)
|
508
|
+
val=f.qagi([tolerance,0.0], 1000, w)
|
335
509
|
elsif is_infinite?(lower_bound)
|
336
510
|
#puts "inferior #{upper_bound}"
|
337
|
-
val=f.qagil(upper_bound, [tolerance, 0], w)
|
511
|
+
val=f.qagil(upper_bound, [tolerance, 0], w)
|
338
512
|
elsif is_infinite?(upper_bound)
|
339
513
|
#puts "superior"
|
340
514
|
val=f.qagiu(lower_bound, [tolerance, 0], w)
|
341
515
|
else
|
342
|
-
|
516
|
+
|
343
517
|
val=f.qag([lower_bound,upper_bound],[tolerance,0.0], GSL::Integration::GAUSS61, w)
|
344
518
|
end
|
345
519
|
elsif(method==:qng)
|
346
|
-
val=f.qng([lower_bound, upper_bound], [tolerance, 0.0])
|
520
|
+
val=f.qng([lower_bound, upper_bound], [tolerance, 0.0])
|
347
521
|
else
|
348
522
|
raise "Unknown integration method \"#{method}\""
|
349
523
|
end
|
350
524
|
val[0]
|
351
525
|
end
|
526
|
+
|
352
527
|
def integrate_ruby(lower_bound,upper_bound,options,&f)
|
353
528
|
method=options[:method]
|
354
529
|
tolerance=options[:tolerance]
|
@@ -388,14 +563,14 @@ class Integration
|
|
388
563
|
current_step+=step
|
389
564
|
previous=value
|
390
565
|
#puts "Llamando al metodo"
|
391
|
-
|
566
|
+
|
392
567
|
value=method_obj.call(lower_bound, upper_bound, current_step, &f)
|
393
568
|
end
|
394
|
-
|
395
|
-
|
569
|
+
|
396
570
|
value
|
397
571
|
end
|
398
572
|
end
|
399
573
|
end
|
574
|
+
|
400
575
|
create_has_library :gsl
|
401
576
|
end
|