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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebeda1905dee9fe22d4a6469200537d76d21c432
4
- data.tar.gz: b3b71ff00ef7a9ced9dd6855f8ef912329167f0e
3
+ metadata.gz: 10ff066c9adc6d22256567faf50bb86bfa41b919
4
+ data.tar.gz: dd387c24f4f687771eed4119b9d4e843be181e2b
5
5
  SHA512:
6
- metadata.gz: 89c4e920ab56d6dc9ba263d0f04dc6682efba2f2374d01c3afc8f56b809e0a40ec96b9987e64c8ec8e5881b9e29bdd73162b32a15fa18c0bab6b43c0bbb14572
7
- data.tar.gz: e19cd9fe1cf1bdc670e5b0ad2f1a2b47f9e6254cf6e4ba9cad9742a7f66ec43bfecf722f7f99919775196ca22a1f9144226762b3c5547e72495c688b9065f8a0
6
+ metadata.gz: 6a4147c4f2be3f383dd001c9092e61ee127bb5cfbfd7300ec58ad4fa430b91dc358f561440fa70c4f1e86e1749519a93e332d2a1c04e27e112cc0547d5049fcb
7
+ data.tar.gz: fb1dfcefe66bd0dc9e8496353d57eb10288b394551cdcf07d2f83a5ff32e85dfac65b752f2ffa128346cfafbfa31b6a57dd13b15f42b1b87a287f5def36ea707
data/lib/mathpack.rb CHANGED
@@ -8,3 +8,4 @@ require 'mathpack/io'
8
8
  require 'mathpack/functions'
9
9
  require 'mathpack/functional'
10
10
  require 'mathpack/integral_equations'
11
+ require 'mathpack/differential_equations'
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Mathpack
2
- VERSION = '0.4.7'
2
+ VERSION = '0.4.8'
3
3
  end
@@ -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.7
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-06-05 00:00:00.000000000 Z
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