mathpack 0.4.2 → 0.4.3

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: 111c0ff1891e4a3c36c9b93e8f76c6e95e6d3daa
4
- data.tar.gz: 3356428babcbf1b7031dc7dfbd4c03a397dc565c
3
+ metadata.gz: 9b544fc6133006b26c674fd82657520f9569d95d
4
+ data.tar.gz: b7a523bf46386d4046a978d3591abc267ce54516
5
5
  SHA512:
6
- metadata.gz: 4f04c1e83d260b0376c79b7899092d878b8f5f3b9dedcc40963a931ca0a5b200a8770598164429678fb2d02d1af14d4d99569a4000c633ced33882f617e2fa6d
7
- data.tar.gz: 39e0707d6eabb22509488221178fe14322d391934a248e8092ddef5050d0a1902755cba9af1698a01d1146046ad489c457b2b82ac2e82f75ae508df6444865d8
6
+ metadata.gz: 8e2d7cf42e3d4528c1e156d636ff393fe4c52d2da6a2197c14f6fe6d16ea9b615514f3aa3f544c6248884bd59d59d66306e28f8aa3ba26a7b448a79fa519d4f8
7
+ data.tar.gz: dab1058342ca55b4ce594a9cc8398d154a4f1774d9c7e73dccb6d6aeb5f725f7f67f48698690e0b3679e854598fb5aef0eb40d9750adbb6e475927261410acf4
data/README.md CHANGED
@@ -18,24 +18,45 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install mathpack
20
20
  ## Information
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, approximate functions by polynom through `approximate_by_polynom` method of `Approximation` module.
21
+ `Mathpack` includes following modules:
22
+ - **SLE**. Solves system of linear equations
23
+ - **Statistics**. Provides methods to analyze data samples
24
+ - **Functions**. Collects mathematical functions
25
+ - **Approximation**. Allows to approximate table and analytical functions by polynom
26
+ - **Equation**. Solves unlinear mathematical equations
27
+ - **Integration**. Integrates functions
28
+ - **IO**. Prints data
29
+
23
30
  ## Statistics
24
31
  `Statistics` class have following methods
25
- - **number** - returns a number of elements in series
26
- - **mean** - returns a mean of series
27
- - **variance** - returns a variance of series
28
- - **skewness** - returns a skewness of series
29
- - **kurtosis** - returns a kurtosis
30
- - **min** - returns the minimal element of series
31
- - **max** - returns the maxinal element of series
32
- - **raw_moment** - returns the *nth* raw moment of series
33
- - **central_moment** - returns the *nth* central moment of series
34
- - **empirical_cdf** - returns *empirical distribution function* value in some point
35
- - **empirical_pdf** - returns *empirical probability density function* value in some point
36
- - **print_empirical_cdf_to_csv** - allows to print empirical_cdf line chart values to `.csv` file with name *filename*
37
- - **print_empirical_pdf_to_csv** - allows to print empirical_pdf line chart values to `.csv` file with name *filename*
38
- - **trend** - returns trend polynom
32
+ #### number
33
+ returns a number of elements in series
34
+ #### mean
35
+ returns a mean of series
36
+ #### variance
37
+ returns a variance of series
38
+ #### skewness
39
+ returns a skewness
40
+ #### kurtosis
41
+ returns a kurtosis
42
+ #### min
43
+ returns the minimal element of series
44
+ #### max
45
+ returns the maxinal element of series
46
+ #### raw_moment(n)
47
+ returns the **nth** raw moment of series
48
+ #### central_moment(n)
49
+ returns the **nth** central moment of series
50
+ #### empirical_cdf(x)
51
+ empirical distribution function value in **x**
52
+ #### empirical_pdf(x)
53
+ returns empirical probability density function value in **x**
54
+ #### print_empirical_cdf_to_csv(filename)
55
+ allows to print empirical_cdf table function to **filename.csv**
56
+ #### print_empirical_pdf_to_csv(filename)
57
+ allows to print empirical_pdf table function to **filename.csv**
58
+ #### trend
59
+ returns trend polynom coefficients
39
60
 
40
61
  ### Usage
41
62
  ```ruby
@@ -51,17 +72,33 @@ stat.raw_moment(3) #=> 87.5
51
72
  stat.central_moment(4) #=> 22.0625
52
73
  stat.empirical_cdf(5.5) #=> 0.75
53
74
  stat.empirical_pdf(3) #=> 0.07639393483317147
54
- stat.print_empirical_cdf_to_csv('cdf.csv') #=> nil
55
- stat.print_empirical_pdf_to_csv('pdf.csv') #=> nil
75
+ stat.print_empirical_cdf_to_csv('cdf') #=> nil
76
+ stat.print_empirical_pdf_to_csv('pdf') #=> nil
56
77
  stat.trend(polynom_power: 1) #=> 1.7999999999999996*x - 0.9999999999999987
57
78
  ```
58
79
 
80
+ ## Functions
81
+ `Functions` module includes a collection of popular functions.
82
+ #### gamma(x)
83
+ ![equation](http://latex.codecogs.com/gif.latex?%5CGamma%28x%29%20%3D%20%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%20x%5E%7Bt-1%7De%5E%7B-x%7Ddx)
84
+ #### beta(x, y)
85
+ ![equation](http://latex.codecogs.com/gif.latex?B%28x%2C%20y%29%20%3D%20%5Cint_%7B0%7D%5E%7B1%7D%20t%5E%7Bx-1%7D%281-t%29%5E%7By-1%7Ddx)
86
+ #### erf(x) (Laplace function)
87
+ ![equation](http://latex.codecogs.com/gif.latex?erf%28x%29%20%3D%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%7D%7D%20%5Cint_%7B-%5Cinfty%7D%5E%7Bx%7De%5E%7B-t%5E%7B2%7D%7Ddt)
88
+ #### dawson_plus(x)
89
+ ![equation](http://latex.codecogs.com/gif.latex?D_%7B+%7D%28x%29%20%3D%20e%5E%7B-x%5E%7B2%7D%7D%5Cint_%7B0%7D%5E%7Bx%7De%5E%7Bt%5E%7B2%7D%7Ddt)
90
+ #### dawson_minus(x)
91
+ ![equation](http://latex.codecogs.com/gif.latex?D_%7B-%7D%28x%29%20%3D%20e%5E%7Bx%5E%7B2%7D%7D%5Cint_%7B0%7D%5E%7Bx%7De%5E%7B-t%5E%7B2%7D%7Ddt)
92
+
59
93
  ## Equation
60
- `Equation` module has only one method:
61
- - **solve** - method, which allows to solve *nonlinear equations*. Neccessary params are `eps` representing calculations accuraccy and `start` representing point to start root search
94
+ #### solve(params = {})
95
+ returns solution of nonlinear equation.
96
+ ##### Parameters
97
+ - *start* - point to start root search
98
+ - *eps* - calculations accuraccy
62
99
 
63
100
  ### Usage
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)
101
+ 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)
65
102
 
66
103
  You need to complete the following steps:
67
104
  - Equation should look like ![equation](http://latex.codecogs.com/gif.latex?%5Ctiny%20f%28x%29%20%3D%200)
@@ -73,7 +110,7 @@ Then to solve equation you should call
73
110
  ```ruby
74
111
  Mathpack::Equation.solve(start: 0, eps: 0.00001){|x| x**2 - Math.sin(x+1)})
75
112
  ```
76
- Here is some examples of **solve** function usage
113
+ Here is some other examples of **solve** usage
77
114
  ```ruby
78
115
  Mathpack::Equation.solve(start: 0, eps: 0.00001){|x| x**2 - Math.sin(x+1)})
79
116
  Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| 1/x - Math.log(x)})
@@ -82,15 +119,18 @@ Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| Math.exp(x-2) - Math.sin
82
119
  ```
83
120
 
84
121
  ## SLE
85
- `SLE` module has only one method:
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
122
+ #### solve(params = {})
123
+ returns solution of system of linear equations.
124
+ ##### Parameters
125
+ - *matrix* - system matrix
126
+ - *f* - right part vector
87
127
 
88
128
  ### Usage
89
129
  Let's solve some system of linear equations. It can be written as
90
130
 
91
131
  ![equation](http://latex.codecogs.com/gif.latex?%5Csmall%20AX%20%3D%20B)
92
132
 
93
- where A is n*n matrix, X - vector of unknown, B - vector
133
+ where *A* is n-n matrix, *X* - vector of unknown, *B* - vector
94
134
 
95
135
  If you want to solve this system you should call
96
136
  ```ruby
@@ -107,12 +147,22 @@ Mathpack::SLE.solve(matrix: a, f: b) #=> Matrix[[-1.0, 2.0, 4.0]]
107
147
  ```
108
148
 
109
149
  ## 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.
150
+ #### approximate_by_polynom(params = {})
151
+ returns array of coefficients of polynom, approximating given function on [from, to] segment.
152
+ ##### Parameters
153
+ - *x* - array of approximation nodes
154
+ - *polynom_power* - power of approximation polynom
155
+ - *f* - functions values in *x* if you have table function
156
+
157
+ #### generate_nodes(params = {})
158
+ returns nodes for approximation with some step.
159
+ ##### Parameters
160
+ - *from* - first node
161
+ - *to* - last node
162
+ - *step* - step between nodes
163
+
164
+ #### print_polynom(coefficients)
165
+ returns a string representing polynom with given coefficients.
116
166
 
117
167
  ### Usage
118
168
  ```ruby
@@ -137,7 +187,12 @@ Mathpack::Approximation.print_polynom(result) #=> x^2
137
187
  ```
138
188
 
139
189
  ## Integration
140
- `Integration` module has method `integrate`, which is used to integrate numericality various functions.
190
+
191
+ #### integrate(params = {})
192
+ returns integral value.
193
+ ##### Parameters
194
+ - *from* - start of integration
195
+ - *to* - end of integration
141
196
 
142
197
  ### Usage
143
198
  Let you have the following integral:
@@ -157,14 +212,20 @@ Mathpack::Integration.integrate(from: -Float::INFINITY, to: Float::INFINITY){ |x
157
212
 
158
213
  ## IO
159
214
 
160
- `IO` module allows to output complex data
215
+ #### print_table_function(params = {})
216
+ writes table function values to *.csv* file
217
+ ##### Parameters
218
+ - *filename* - name of output file
219
+ - *x* - arguements array
220
+ - *y* - function values array
221
+ - *labels* - hash of labels names for *x* and *y* column
161
222
 
162
223
  ### Usage
163
224
 
164
225
  If you have table function, represented by argument array and function values array, you should use
165
- `print_table_function`, that prints your data to `.csv` file.
226
+ **print_table_function**, that prints your data to **filename.csv** file.
166
227
  ```ruby
167
- Mathpack::IO.print_table_function(filename: 'table.csv', x: [1, 2, 3], y: [2, 4, 6], labels: { x: 'x', y: 'f(x)'}
228
+ Mathpack::IO.print_table_function(filename: 'table.csv', x: [1, 2, 3], y: [2, 4, 6], labels: { x: 'x', y: 'f(x)'})
168
229
  ```
169
230
 
170
231
  ## Contributing
@@ -0,0 +1,23 @@
1
+ module Mathpack
2
+ module Functions
3
+ def self.gamma(t)
4
+ Mathpack::Integration.integrate(from: 0.0, to: Float::INFINITY) { |x| x**(t-1) * Math.exp(-x) }
5
+ end
6
+
7
+ def self.erf(x)
8
+ 1.0 / (2.0 * Math::PI)**0.5 * Mathpack::Integration.integrate(from: -Float::INFINITY, to: x) { |u| Math.exp(-u**2 / 2.0) }
9
+ end
10
+
11
+ def self.beta(a, b)
12
+ gamma(a) * gamma(b) / gamma(a + b)
13
+ end
14
+
15
+ def self.dawson_plus(x)
16
+ Math.exp(-x**2) * Mathpack::Integration.integrate(from: 0.0, to: x) { |x| Math.exp(x**2) }
17
+ end
18
+
19
+ def self.dawson_minus(x)
20
+ Math.exp(x**2) * Mathpack::Integration.integrate(from: 0.0, to: x) { |x| Math.exp(-x**2) }
21
+ end
22
+ end
23
+ end
@@ -1,8 +1,7 @@
1
1
  module Mathpack
2
2
  module Integration
3
- STEP = 0.0625
4
- INTEGRATION_LIMIT = 1e3
5
-
3
+ STEP = 0.0078125
4
+ INTEGRATION_LIMIT = 1e2
6
5
  def self.integrate(params = {}, &f)
7
6
  if params[:from] == -Float::INFINITY && params[:to] == Float::INFINITY
8
7
  result = solve_oi(-INTEGRATION_LIMIT, INTEGRATION_LIMIT, &f)
data/lib/mathpack/io.rb CHANGED
@@ -2,7 +2,7 @@ module Mathpack
2
2
  module IO
3
3
  def self.print_table_function(params = {})
4
4
  fail 'Arrays length dismatch' if params[:x].length != params[:y].length
5
- File.open(params[:filename] || 'table_function.csv', 'w+') do |file|
5
+ File.open(params[:filename] + '.csv'|| 'table_function.csv', 'w+') do |file|
6
6
  file.write("#{params[:labels][:x]};#{params[:labels][:y]}\n") if params[:labels] && params[:labels][:x] && params[:labels][:y]
7
7
  params[:x].each_index do |i|
8
8
  file.write("#{params[:x][i]};#{params[:y][i]}\n")
@@ -1,3 +1,3 @@
1
1
  module Mathpack
2
- VERSION = '0.4.2'
2
+ VERSION = '0.4.3'
3
3
  end
data/lib/mathpack.rb CHANGED
@@ -5,3 +5,4 @@ require 'mathpack/sle'
5
5
  require 'mathpack/approximation'
6
6
  require 'mathpack/integration'
7
7
  require 'mathpack/io'
8
+ require 'mathpack/functions'
@@ -0,0 +1,40 @@
1
+ describe 'Functions' do
2
+ require 'mathpack/functions'
3
+
4
+ describe '#gamma' do
5
+ it 'should calculate gamma function' do
6
+ expect(Mathpack::Functions.gamma(1.5)).to be_between(0.886225, 0.886229)
7
+ expect(Mathpack::Functions.gamma(1.8)).to be_between(0.9313835, 0.9313844)
8
+ expect(Mathpack::Functions.gamma(5.8)).to be_between(85.62165, 85.62174)
9
+ expect(Mathpack::Functions.gamma(3.75)).to be_between(4.422985, 4.422994)
10
+ expect(Mathpack::Functions.gamma(7.68)).to be_between(2662.63, 2662.635)
11
+ end
12
+ end
13
+
14
+ describe '#erf' do
15
+ it 'should calculate erf function' do
16
+ expect((Mathpack::Functions.erf(0.0) - 0.5).abs < 1e-7).to eq(true)
17
+ expect((Mathpack::Functions.erf(1.73) - 0.9582).abs < 1e-4).to eq(true)
18
+ expect((Mathpack::Functions.erf(3.49) - 0.9998).abs < 1e-4).to eq(true)
19
+ end
20
+ end
21
+
22
+ describe '#beta' do
23
+ it 'should calculate beta function' do
24
+ expect(Mathpack::Functions.beta(3.5, 2.2)).to be_between(0.0504866, 0.05048665)
25
+ end
26
+ end
27
+
28
+ describe '#dawson_plus' do
29
+ it 'should calculate dawson_plus integral' do
30
+ expect(Mathpack::Functions.dawson_plus(0.924138873)).to be_between(0.5410442, 0.54104425)
31
+ expect(Mathpack::Functions.dawson_plus(1.5019752683)).to be_between(0.427686616, 0.4276866164)
32
+ end
33
+ end
34
+
35
+ describe '#dawson_minus' do
36
+ it 'should calculate dawson_minus integral' do
37
+ expect(Mathpack::Functions.dawson_minus(0.924138873) + Mathpack::Functions.dawson_minus(-0.924138873) < 1e-5).to eq(true)
38
+ end
39
+ end
40
+ 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.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxmilan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-07 00:00:00.000000000 Z
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,6 +54,7 @@ files:
54
54
  - lib/mathpack.rb
55
55
  - lib/mathpack/approximation.rb
56
56
  - lib/mathpack/equation.rb
57
+ - lib/mathpack/functions.rb
57
58
  - lib/mathpack/integration.rb
58
59
  - lib/mathpack/io.rb
59
60
  - lib/mathpack/sle.rb
@@ -62,6 +63,7 @@ files:
62
63
  - mathpack.gemspec
63
64
  - spec/approximation_spec.rb
64
65
  - spec/equation_spec.rb
66
+ - spec/functions_spec.rb
65
67
  - spec/integration_spec.rb
66
68
  - spec/sle_spec.rb
67
69
  - spec/statistics_spec.rb