mathpack 0.2.0 → 0.3.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/approximation.rb +73 -0
- data/lib/mathpack/version.rb +1 -1
- data/lib/mathpack.rb +1 -0
- data/mathpack.gemspec +2 -2
- data/spec/approximation_spec.rb +38 -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: e0f0d47957524843b49cef7c52ac64230e6f7d68
|
4
|
+
data.tar.gz: cb662a79d6977fb3f2d891f2b2eb2881b2c00cf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d134a1000e9bf0068a36923484f373cf546492e8a6795539ee8616f06d09ec6e31886e096929f6c463a23a8d48dbcbf973b035309d3ccac92dceec6973f88e0
|
7
|
+
data.tar.gz: 0a8fc14ec5126df7b9afe59ca2141b52539cea8a710875c6ca6bb10e5aae7fc4238cca77a0f6b359be6bc6d0b215f85d220439f0f5b99475592616d8ed264e30
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'mathpack'
|
2
|
+
|
3
|
+
module Mathpack
|
4
|
+
module Approximation
|
5
|
+
def self.basic_fi(x, power)
|
6
|
+
x**power
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.approximate_by_polynom(params = {}, &function)
|
10
|
+
f = block_given? ? fill_f(params[:x], &function) : params[:f]
|
11
|
+
mnk_matrix, mnk_vector = count_mnk_values(params[:x], f, params[:polynom_power])
|
12
|
+
Mathpack::SLE.solve(matrix: mnk_matrix, f: mnk_vector).reverse
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.scalar_function_composition(first_vect, second_vect, first_power, second_power)
|
16
|
+
result = 0.0
|
17
|
+
first_vect.each_index do |i|
|
18
|
+
result += basic_fi(first_vect[i], first_power) * basic_fi(second_vect[i], second_power)
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.count_mnk_values(x, f, polynom_power)
|
24
|
+
mnk_matrix = Array.new(polynom_power + 1) { Array.new(polynom_power + 1) }
|
25
|
+
mnk_vector = Array.new(polynom_power + 1)
|
26
|
+
0.upto(polynom_power) do |i|
|
27
|
+
0.upto(polynom_power) do |j|
|
28
|
+
mnk_matrix[i][j] = scalar_function_composition(x, x, i, j)
|
29
|
+
end
|
30
|
+
mnk_vector[i] = scalar_function_composition(x, f, i, 1)
|
31
|
+
end
|
32
|
+
[mnk_matrix, mnk_vector]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.print_polynom(coefficients)
|
36
|
+
polynom = ''
|
37
|
+
coefficients.each_index do |i|
|
38
|
+
next if coefficients[i] == 0
|
39
|
+
if i == 0
|
40
|
+
polynom += '-' if coefficients[i] < 0.0
|
41
|
+
else
|
42
|
+
polynom += coefficients[i] < 0.0 ? ' - ' : ' + '
|
43
|
+
end
|
44
|
+
polynom += coefficients[i].abs.to_s if coefficients[i].abs != 1 || i == coefficients.length - 1
|
45
|
+
polynom += '*' if i != coefficients.length - 1 && coefficients[i].abs != 1
|
46
|
+
case i
|
47
|
+
when coefficients.length - 1
|
48
|
+
|
49
|
+
when coefficients.length - 2
|
50
|
+
polynom += 'x'
|
51
|
+
else
|
52
|
+
polynom += coefficients.length - i >= 11 ? "x^(#{coefficients.length - 1 - i})" : "x^#{coefficients.length - 1 - i}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
polynom
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.generate_nodes(params = {})
|
59
|
+
nodes = []
|
60
|
+
x = params[:start]
|
61
|
+
loop do
|
62
|
+
nodes << x
|
63
|
+
x += params[:step]
|
64
|
+
break if x > params[:end]
|
65
|
+
end
|
66
|
+
nodes
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.fill_f(x, &function)
|
70
|
+
x.map { |val| function.call(val) }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/mathpack/version.rb
CHANGED
data/lib/mathpack.rb
CHANGED
data/mathpack.gemspec
CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/$})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
23
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
describe 'Approximation' do
|
2
|
+
require 'mathpack/approximation'
|
3
|
+
|
4
|
+
context '#mnk' do
|
5
|
+
let(:x_start) { 0.0 }
|
6
|
+
let(:x_end) { 10.0 }
|
7
|
+
let(:step) { 0.25 }
|
8
|
+
let(:nodes) { Mathpack::Approximation.generate_nodes(start: x_start, end: x_end, step: step) }
|
9
|
+
let(:values) { Mathpack::Approximation.fill_f(nodes){ |val| val**2 } }
|
10
|
+
|
11
|
+
it 'should generate nodes' do
|
12
|
+
expect(nodes.length).to be_eql(41)
|
13
|
+
expect(nodes.first).to be_eql(x_start)
|
14
|
+
expect(nodes.last).to be_eql(x_end)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should fill function table' do
|
18
|
+
expect(values.length).to be_eql(nodes.length)
|
19
|
+
expect(values.last).to be_eql(nodes.last**2)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should print polynoms' do
|
23
|
+
expect(Mathpack::Approximation.print_polynom([5])).to be_eql('5')
|
24
|
+
expect(Mathpack::Approximation.print_polynom([2, 3])).to be_eql('2*x + 3')
|
25
|
+
expect(Mathpack::Approximation.print_polynom([1, -1, 1])).to be_eql('x^2 - x + 1')
|
26
|
+
expect(Mathpack::Approximation.print_polynom([1, 3, 5])).to be_eql('x^2 + 3*x + 5')
|
27
|
+
expect(Mathpack::Approximation.print_polynom([1, -2, 3, -4])).to be_eql('x^3 - 2*x^2 + 3*x - 4')
|
28
|
+
expect(Mathpack::Approximation.print_polynom([1, 0, 1])).to be_eql('x^2 + 1')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should approximate by polynom' do
|
32
|
+
coefficients = Mathpack::Approximation.approximate_by_polynom(x: nodes, f: values, polynom_power: 2)
|
33
|
+
expect(coefficients).to be_eql([1.0, 0.0, 0.0])
|
34
|
+
coefficients = Mathpack::Approximation.approximate_by_polynom(x: nodes, f: values, polynom_power: 0)
|
35
|
+
expect(coefficients).to be_eql([33.75])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxmilan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,11 +52,13 @@ files:
|
|
52
52
|
- README.md
|
53
53
|
- Rakefile
|
54
54
|
- lib/mathpack.rb
|
55
|
+
- lib/mathpack/approximation.rb
|
55
56
|
- lib/mathpack/equation.rb
|
56
57
|
- lib/mathpack/sle.rb
|
57
58
|
- lib/mathpack/statistics.rb
|
58
59
|
- lib/mathpack/version.rb
|
59
60
|
- mathpack.gemspec
|
61
|
+
- spec/approximation_spec.rb
|
60
62
|
- spec/equation_spec.rb
|
61
63
|
- spec/sle_spec.rb
|
62
64
|
- spec/statistics_spec.rb
|