mathpack 0.4.7 → 0.4.8
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.rb +1 -0
- data/lib/mathpack/differential_equations.rb +66 -0
- data/lib/mathpack/version.rb +1 -1
- data/spec/differential_equations_spec.rb +14 -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: 10ff066c9adc6d22256567faf50bb86bfa41b919
|
4
|
+
data.tar.gz: dd387c24f4f687771eed4119b9d4e843be181e2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a4147c4f2be3f383dd001c9092e61ee127bb5cfbfd7300ec58ad4fa430b91dc358f561440fa70c4f1e86e1749519a93e332d2a1c04e27e112cc0547d5049fcb
|
7
|
+
data.tar.gz: fb1dfcefe66bd0dc9e8496353d57eb10288b394551cdcf07d2f83a5ff32e85dfac65b752f2ffa128346cfafbfa31b6a57dd13b15f42b1b87a287f5def36ea707
|
data/lib/mathpack.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'mathpack'
|
2
|
+
|
3
|
+
module Mathpack
|
4
|
+
module DifferentialEquations
|
5
|
+
def self.runge_kutta_4(params = {})
|
6
|
+
nodes = Mathpack::Approximation.generate_nodes(from: params[:from], to: params[:to], step: params[:step])
|
7
|
+
y = Array.new(params[:system].length) { Array.new(nodes.length) }
|
8
|
+
y.each_index do |i|
|
9
|
+
y[i][0] = params[:y0][i]
|
10
|
+
end
|
11
|
+
fi0 = Array.new(y.length)
|
12
|
+
fi1 = Array.new(y.length)
|
13
|
+
fi2 = Array.new(y.length)
|
14
|
+
fi3 = Array.new(y.length)
|
15
|
+
for i in 0...nodes.length-1 do
|
16
|
+
column = y.transpose[i]
|
17
|
+
y_volna = Array.new(y.length)
|
18
|
+
y.each_index do |j|
|
19
|
+
fi0[j] = params[:system][j].call(nodes[i], *column)
|
20
|
+
end
|
21
|
+
y.each_index do |j|
|
22
|
+
y_volna.each_index { |k| y_volna[k] = column[k] + 0.5 * params[:step] * fi0[k] }
|
23
|
+
fi1[j] = params[:system][j].call(nodes[i] + 0.5 * params[:step], *y_volna)
|
24
|
+
end
|
25
|
+
y.each_index do |j|
|
26
|
+
y_volna.each_index { |k| y_volna[k] = column[k] + 0.5 * params[:step] * fi1[k] }
|
27
|
+
fi2[j] = params[:system][j].call(nodes[i] + 0.5 * params[:step], *y_volna)
|
28
|
+
end
|
29
|
+
y.each_index do |j|
|
30
|
+
y_volna.each_index { |k| y_volna[k] = column[k] + params[:step] * fi2[k] }
|
31
|
+
fi3[j] = params[:system][j].call(nodes[i + 1], *y_volna)
|
32
|
+
end
|
33
|
+
y.each_index do |j|
|
34
|
+
y[j][i + 1] = y[j][i] + params[:step]/6.0 * (fi0[j] + 2.0 * fi1[j] + 2.0 * fi2[j] + fi3[j])
|
35
|
+
y[j][i + 1] = y[j][i] + params[:step]/6.0 * (fi0[j] + 2.0 * fi1[j] + 2.0 * fi2[j] + fi3[j])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
y
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.solve_cauchie_system(params = {})
|
42
|
+
params[:method] ||= :runge_kutta_4
|
43
|
+
params[:m] = 2
|
44
|
+
step = (params[:to] - params[:from]) / 2.0
|
45
|
+
result = Mathpack::DifferentialEquations.send(params[:method], from: params[:from], to: params[:to], step: step, y0: params[:y0], system: params[:system], eps: params[:eps])
|
46
|
+
u_next = Array.new(params[:system].length) { |i| result[i] }
|
47
|
+
loop do
|
48
|
+
u_prev = Array.new(u_next.length) { |i| u_next[i].dup }
|
49
|
+
step /= 2.0
|
50
|
+
result = Mathpack::DifferentialEquations.send(params[:method], from: params[:from], to: params[:to], step: step, y0: params[:y0], system: params[:system], eps: params[:eps])
|
51
|
+
u_next.each_index do |i|
|
52
|
+
u_next[i] = result[i]
|
53
|
+
end
|
54
|
+
is_finished = true
|
55
|
+
u_next.each_index do |i|
|
56
|
+
if Mathpack::IO.count_diff(u_next[i], u_prev[i]) / (2**params[:m] - 1) > params[:eps]
|
57
|
+
is_finished = false
|
58
|
+
break
|
59
|
+
end
|
60
|
+
end
|
61
|
+
break if is_finished
|
62
|
+
end
|
63
|
+
{ x: Mathpack::Approximation.generate_nodes(from: params[:from], to: params[:to], step: step), y: u_next.first }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/mathpack/version.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
describe 'DifferentialEquation' do
|
2
|
+
require 'mathpack/differential_equations'
|
3
|
+
|
4
|
+
describe '#solve_cauchie_system' do
|
5
|
+
let(:eps) { 1e-6 }
|
6
|
+
|
7
|
+
it 'solves cauchie system correctly' do
|
8
|
+
cauchie_problem = [ ->(x, u1, u2) { u2 }, -> (x, u1, u2) { - 1.0 / x * u2 + 1.0 / x**2 * u1 + 3.0 }]
|
9
|
+
result = Mathpack::DifferentialEquations.solve_cauchie_system(from: 1.0, to: 2.0, eps: eps, system: cauchie_problem, y0: [1.0, 1.0])
|
10
|
+
correct_result = Mathpack::Approximation.fill_f(result[:x]){ |t| t**2 - 0.5 * t + 1.0/(2.0 * t) }
|
11
|
+
expect(Mathpack::IO.count_diff(result[:y], correct_result) <= eps).to eq(true)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
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.
|
4
|
+
version: 0.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxmilan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- lib/mathpack.rb
|
55
55
|
- lib/mathpack/approximation.rb
|
56
|
+
- lib/mathpack/differential_equations.rb
|
56
57
|
- lib/mathpack/functional.rb
|
57
58
|
- lib/mathpack/functions.rb
|
58
59
|
- lib/mathpack/integral_equations.rb
|
@@ -64,6 +65,7 @@ files:
|
|
64
65
|
- lib/mathpack/version.rb
|
65
66
|
- mathpack.gemspec
|
66
67
|
- spec/approximation_spec.rb
|
68
|
+
- spec/differential_equations_spec.rb
|
67
69
|
- spec/functional_spec.rb
|
68
70
|
- spec/functions_spec.rb
|
69
71
|
- spec/integral_equations_spec.rb
|