mathpack 0.3.2 → 0.4.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 +4 -4
- data/lib/mathpack/integration.rb +60 -0
- data/lib/mathpack/version.rb +1 -1
- data/lib/mathpack.rb +1 -0
- data/spec/integration_spec.rb +42 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78c7a6ef1eb501cbcc71788e0afe54a97c18b466
|
4
|
+
data.tar.gz: 52b242cd86918bbf029762f6191b86674af05b2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c25535090593e3f413123b78a63c40140eaa0ef58094059e4b2c321d65d143d320e5d7bedce7e75b3c55a86b4c62f0dc6d9a215e3e75993fa41a15fb12781ee
|
7
|
+
data.tar.gz: 85627190173a1ef07beec6be3c7ceb559c19867e2fb3edbd293c9e7a3bdd47ffc5dd0cfa87f42ab01e0547f24846c694c9fa3ac166f8a6af8246041d5faddbfa
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Mathpack
|
2
|
+
module Integration
|
3
|
+
STEP = 0.0625
|
4
|
+
INTEGRATION_LIMIT = 1e3
|
5
|
+
|
6
|
+
def self.integrate(params = {}, &f)
|
7
|
+
if params[:from] == -Float::INFINITY && params[:to] == Float::INFINITY
|
8
|
+
result = solve_ni_2(&f)
|
9
|
+
elsif params[:to] == Float::INFINITY || params[:from] == -Float::INFINITY
|
10
|
+
if params[:from] == -Float::INFINITY
|
11
|
+
params[:from] = -params[:to]
|
12
|
+
g = ->(x) { f.call(-x) }
|
13
|
+
else
|
14
|
+
g = f
|
15
|
+
end
|
16
|
+
result = solve_ni_1(params[:from], &g)
|
17
|
+
else
|
18
|
+
result = solve_oi(params[:from], params[:to], &f)
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.basic_integrate(a, b, &f)
|
24
|
+
result = 5.0/9.0 * f.call((a + b)/2.0 + (b - a)/2.0 * (-(3.0/5.0)**0.5))
|
25
|
+
result += 8.0/9.0 * f.call((a + b)/2.0)
|
26
|
+
result += 5.0/9.0 * f.call((a + b)/2.0 + (b - a)/2.0 * (3.0/5.0)**0.5)
|
27
|
+
result * (b - a)/2.0
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.solve_oi(from, to, &f)
|
31
|
+
result = 0.0
|
32
|
+
nodes = Mathpack::Approximation.generate_nodes(start: from, end: to, step: STEP)
|
33
|
+
for i in 0...nodes.length - 1 do
|
34
|
+
result += basic_integrate(nodes[i], nodes[i + 1], &f)
|
35
|
+
end
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.solve_ni_1(from, &f)
|
40
|
+
to = [from, INTEGRATION_LIMIT].max
|
41
|
+
result = solve_oi(from, to, &f)
|
42
|
+
coefficients_array = [0.4589646740, 0.4170008308, 0.1133733821, 0.0103991974, 0.0002610172, 0.0000008985]
|
43
|
+
nodes_array = [0.2228466042, 1.8889321017, 2.9927363261, 5.7751435691, 9.8374674184, 15.9828739806]
|
44
|
+
nodes_array.each_index do |i|
|
45
|
+
result += coefficients_array[i] * Math.exp(nodes_array[i]) * f.call(to + nodes_array[i])
|
46
|
+
end
|
47
|
+
result
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.solve_ni_2(&f)
|
51
|
+
result = 0.0
|
52
|
+
coefficients_array = [0.004530009906, 0.1570673203, 0.7246295952, 0.7246295952, 0.1570673203, 0.004530009906]
|
53
|
+
nodes_array = [-2.3506049737, -1.3358490740, -0.4360774119, 0.4360774119, 1.3358490740, 2.3506049737]
|
54
|
+
nodes_array.each_index do |i|
|
55
|
+
result += coefficients_array[i] * Math.exp(nodes_array[i]**2) * f.call(nodes_array[i])
|
56
|
+
end
|
57
|
+
result
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/mathpack/version.rb
CHANGED
data/lib/mathpack.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
describe 'Integration' do
|
2
|
+
require 'mathpack/integration'
|
3
|
+
|
4
|
+
context '#a-b integrals' do
|
5
|
+
it 'should integrate e^(-x^2)' do
|
6
|
+
expect(Mathpack::Integration.integrate(from: 0, to: 10){ |x| Math.exp(-x**2) }).to be_between(0.8862269254525, 0.8862269254534)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should integrate sin(x^2)' do
|
10
|
+
expect(Mathpack::Integration.integrate(from: 1, to: 7.3){ |x| Math.sin(x**2) }).to be_between(0.384315, 0.384324)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should integrate sin(x)/x' do
|
14
|
+
expect(Mathpack::Integration.integrate(from: 0, to: 3.6){ |x| Math.sin(x) / x }).to be_between(1.821945, 1.821954)
|
15
|
+
expect(Mathpack::Integration.integrate(from: -1, to: 1){ |x| Math.sin(x) / x }).to be_between(1.89216614073435, 1.89216614073444)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should integrate 1/lnx' do
|
19
|
+
expect(Mathpack::Integration.integrate(from: 1.1, to: 4.7){ |x| 1 / Math.log(x) }).to be_between(5.120315, 5.120324)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context '#ni-1 integrals' do
|
24
|
+
it 'should integrate e^(-x)/(x+1)' do
|
25
|
+
expect(Mathpack::Integration.integrate(from: 0, to: Float::INFINITY){ |x| Math.exp(-x) / (x + 1) }).to be_between(0.596346, 0.596348)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should count gamma function' do
|
29
|
+
expect(Mathpack::Integration.integrate(from: 0, to: Float::INFINITY){ |x| Math.exp(-x) * x**1.5 }).to be_between(1.329339, 1.329341)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should count Laplace function' do
|
33
|
+
expect(Mathpack::Integration.integrate(from: -Float::INFINITY, to: 1.65){ |x| 1.0 / (2 * Math::PI)**0.5 * Math.exp(-x**2/2) }).to be_between(0.950450, 0.950549)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context '#ni-2 integrals' do
|
38
|
+
it 'should integrate e^(-x^2)*cos(x)' do
|
39
|
+
expect(Mathpack::Integration.integrate(from: -Float::INFINITY, to: Float::INFINITY){ |x| Math.exp(-x**2) * Math.cos(x) }).to be_between(1.3803, 1.3804)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mathpack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxmilan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -54,12 +54,14 @@ files:
|
|
54
54
|
- lib/mathpack.rb
|
55
55
|
- lib/mathpack/approximation.rb
|
56
56
|
- lib/mathpack/equation.rb
|
57
|
+
- lib/mathpack/integration.rb
|
57
58
|
- lib/mathpack/sle.rb
|
58
59
|
- lib/mathpack/statistics.rb
|
59
60
|
- lib/mathpack/version.rb
|
60
61
|
- mathpack.gemspec
|
61
62
|
- spec/approximation_spec.rb
|
62
63
|
- spec/equation_spec.rb
|
64
|
+
- spec/integration_spec.rb
|
63
65
|
- spec/sle_spec.rb
|
64
66
|
- spec/statistics_spec.rb
|
65
67
|
homepage: ''
|