mathpack 0.3.0 → 0.3.1

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: e0f0d47957524843b49cef7c52ac64230e6f7d68
4
- data.tar.gz: cb662a79d6977fb3f2d891f2b2eb2881b2c00cf9
3
+ metadata.gz: 990d138b20708752c754e294a22627435a3a5ce9
4
+ data.tar.gz: dbb63f6b4cea510030e05c819cd2bac9a5c7376a
5
5
  SHA512:
6
- metadata.gz: 1d134a1000e9bf0068a36923484f373cf546492e8a6795539ee8616f06d09ec6e31886e096929f6c463a23a8d48dbcbf973b035309d3ccac92dceec6973f88e0
7
- data.tar.gz: 0a8fc14ec5126df7b9afe59ca2141b52539cea8a710875c6ca6bb10e5aae7fc4238cca77a0f6b359be6bc6d0b215f85d220439f0f5b99475592616d8ed264e30
6
+ metadata.gz: 36318534efa67765b74962c5d6b102b7e0e4c4b2e2bbda35a795c443be5d6e7b09f6ec9be97f014ae7e8db88a6831ef710afb42714164ead92322b35f3c9433a
7
+ data.tar.gz: 38db3fd63908159911ef2ba99d8b6a0b899ae26010482104db5aa384f42f61f1176dcec6c59672fe664fb342f4f6817a5540a3d3289950cfaf4d382212194338
data/.gitignore CHANGED
@@ -21,3 +21,4 @@ tmp
21
21
  *.a
22
22
  mkmf.log
23
23
  .rubocop.yml
24
+ *.idea
data/README.md CHANGED
@@ -19,7 +19,7 @@ Or install it yourself as:
19
19
  $ gem install mathpack
20
20
  ## Information
21
21
  Gem `mathpack` allows to count statistical functions through `Statistics` class, solve nonlinear equations through `Equation` module
22
- `solve` method, solve systems of linear equations through `SLE` module `solve` method.
22
+ `solve` method, solve systems of linear equations through `SLE` module `solve` method, approximate functions by polynom through `approximate_by_polynom` method of `Approximation` module.
23
23
  ## Statistics
24
24
  `Statistics` class have following methods
25
25
  - **number** - returns a number of elements in series
@@ -35,6 +35,7 @@ Gem `mathpack` allows to count statistical functions through `Statistics` class,
35
35
  - **empirical_pdf** - returns *empirical probability density function* value in some point
36
36
  - **print_empirical_cdf_to_csv** - allows to print empirical_cdf line chart values to `.csv` file with name *filename*
37
37
  - **print_empirical_cdf_to_csv** - allows to print empirical_cdf line chart values to `.csv` file with name *filename*
38
+ - **trend** - returns trend polynom
38
39
 
39
40
  ### Usage
40
41
  ```ruby
@@ -52,13 +53,14 @@ stat.empirical_cdf(5.5) #=> 0.75
52
53
  stat.empirical_pdf(3) #=> 0.07639393483317147
53
54
  stat.print_empirical_cdf_to_csv('cdf.csv') #=> nil
54
55
  stat.print_empirical_pdf_to_csv('pdf.csv') #=> nil
56
+ stat.trend(polynom_power: 1) #=> 1.7999999999999996*x - 0.9999999999999987
55
57
  ```
56
58
 
57
- ##Equation
58
- `Equation` module has only one method
59
+ ## Equation
60
+ `Equation` module has only one method:
59
61
  - **solve** - method, which allows to solve *nonlinear equations*. Neccessary params are `eps` representing calculations accuraccy and `start` representing point to start root search
60
62
 
61
- ###Usage
63
+ ### Usage
62
64
  Now you have no problems solving **nonlinear equations**. If you want, for example, to solve equation ![equation](http://latex.codecogs.com/gif.latex?x%5E%7B2%7D%20%3D%20%5Csin%28%7Bx+1%7D%29)
63
65
 
64
66
  You need to complete the following steps:
@@ -80,10 +82,10 @@ Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| Math.exp(x-2) - Math.sin
80
82
  ```
81
83
 
82
84
  ## SLE
83
- `SLE` module has only one method
85
+ `SLE` module has only one method:
84
86
  - **solve** - method, which allows to solve *system of linear equations*. Neccessary params are `matrix` representing system matrix and `f`. Params can be **Array** or **Matrix** class. If the system can't be solved, method raise exception
85
87
 
86
- ## Usage
88
+ ### Usage
87
89
  Let's solve some system of linear equations. It can be written as
88
90
 
89
91
  ![equation](http://latex.codecogs.com/gif.latex?%5Csmall%20AX%20%3D%20B)
@@ -104,6 +106,36 @@ b = Matrix.row_vector [15, 30, 15]
104
106
  Mathpack::SLE.solve(matrix: a, f: b) #=> Matrix[[-1.0, 2.0, 4.0]]
105
107
  ```
106
108
 
109
+ ## Approximation
110
+ May be sometimes you need to approximate some function, which is analytic or represented by table of values, by polynom. You can
111
+ manage with this problem using `Approximation` module. This module uses least squares approximation method.
112
+ `Approximation` module has the following methods:
113
+ - **approximate_by_polynom** - main method of the module, that allows to approximate function. It returns array of coefficients of polynom.
114
+ - **generate_nodes** - method, that allows to generate nodes for approximation with some step.
115
+ - **print_polynom** - method, that returns a string representing polynom with given coefficients.
116
+
117
+ ### Usage
118
+ ```ruby
119
+ # Function to print polynom having it's coefficients
120
+ Mathpack::Approximation::print_polynom([1, -2, 1]) #=> x^2 - 2*x + 1
121
+
122
+ # Function to generate nodes for approximation. Only choose start value, end value and step
123
+ x = Mathpack::Approximation::generate_nodes(start: 0, end: 10, step: 0.25)
124
+ #=> [0, 0.25, ..., 10]
125
+
126
+ # Function that approximate given function by polynom with power polynom_power in approximation nodes x and returns coefficients of approximating polynom
127
+ result = Mathpack::Approximation::approximate_by_polynom(x: x, polynom_power: 5){ |x| Math.sin(x) }
128
+ #=> [0.0008009744982571882, -0.030619986086758588, 0.3805927651948083, -1.8481035413353888, 2.985465287759307, -0.3873066069630569]
129
+
130
+ # May be you want to print this polynom, let's call print_polynom function
131
+ Mathpack::Approximation::print_polynom(result)
132
+ #=> 0.0008009744982571882*x^5 - 0.030619986086758588*x^4 + 0.3805927651948083*x^3 - 1.8481035413353888*x^2 + 2.985465287759307*x - 0.3873066069630569
133
+
134
+ # If you have a table of values, where x - array of arguement values and f - array of function values, call approximate by polynom function with parameter f instead of block
135
+ result = Mathpack::Approximation::approximate_by_polynom(x: [1, 2, 3], f: [1, 4, 9], polynom_power: 2) #=> [1, 0, 0]
136
+ Mathpack::Approximation::print_polynom(result) #=> x^2
137
+ ```
138
+
107
139
  ## Contributing
108
140
 
109
141
  1. Fork it ( https://github.com/[my-github-username]/mathpack/fork )
@@ -82,6 +82,12 @@ module Mathpack
82
82
  end
83
83
  end
84
84
 
85
+ def trend(params = {})
86
+ numbers = Array.new(number){ |i| i + 1 }
87
+ polynom = Mathpack::Approximation::approximate_by_polynom(x: numbers, f: @series, polynom_power: params[:polynom_power])
88
+ Mathpack::Approximation.print_polynom(polynom)
89
+ end
90
+
85
91
  private
86
92
 
87
93
  def heaviside(x)
@@ -1,3 +1,3 @@
1
1
  module Mathpack
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
data/mathpack.gemspec CHANGED
@@ -19,5 +19,5 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.6'
22
- spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rake', '~>0'
23
23
  end
@@ -9,30 +9,30 @@ describe 'Approximation' do
9
9
  let(:values) { Mathpack::Approximation.fill_f(nodes){ |val| val**2 } }
10
10
 
11
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)
12
+ expect(nodes.length).to eql(41)
13
+ expect(nodes.first).to eql(x_start)
14
+ expect(nodes.last).to eql(x_end)
15
15
  end
16
16
 
17
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)
18
+ expect(values.length).to eql(nodes.length)
19
+ expect(values.last).to eql(nodes.last**2)
20
20
  end
21
21
 
22
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')
23
+ expect(Mathpack::Approximation.print_polynom([5])).to eql('5')
24
+ expect(Mathpack::Approximation.print_polynom([2, 3])).to eql('2*x + 3')
25
+ expect(Mathpack::Approximation.print_polynom([1, -1, 1])).to eql('x^2 - x + 1')
26
+ expect(Mathpack::Approximation.print_polynom([1, 3, 5])).to eql('x^2 + 3*x + 5')
27
+ expect(Mathpack::Approximation.print_polynom([1, -2, 3, -4])).to eql('x^3 - 2*x^2 + 3*x - 4')
28
+ expect(Mathpack::Approximation.print_polynom([1, 0, 1])).to eql('x^2 + 1')
29
29
  end
30
30
 
31
31
  it 'should approximate by polynom' do
32
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])
33
+ expect(coefficients).to eql([1.0, 0.0, 0.0])
34
34
  coefficients = Mathpack::Approximation.approximate_by_polynom(x: nodes, f: values, polynom_power: 0)
35
- expect(coefficients).to be_eql([33.75])
35
+ expect(coefficients).to eql([33.75])
36
36
  end
37
37
  end
38
38
  end
@@ -59,5 +59,9 @@ describe 'Statistics' do
59
59
  it 'should calculate empirical pdf values in points' do
60
60
  expect(stat.empirical_pdf(stat.mean)).to eql(0.1882412842233359)
61
61
  end
62
+
63
+ it 'should find trend' do
64
+ expect(stat.trend(polynom_power: 2)).to eql('0.0075757575757576055*x^2 + 0.2681818181818179*x + 2.233333333333334')
65
+ end
62
66
  end
63
67
  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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxmilan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-25 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Includes collection of mathematical methods