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 +4 -4
- data/README.md +96 -35
- data/lib/mathpack/functions.rb +23 -0
- data/lib/mathpack/integration.rb +2 -3
- data/lib/mathpack/io.rb +1 -1
- data/lib/mathpack/version.rb +1 -1
- data/lib/mathpack.rb +1 -0
- data/spec/functions_spec.rb +40 -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: 9b544fc6133006b26c674fd82657520f9569d95d
|
4
|
+
data.tar.gz: b7a523bf46386d4046a978d3591abc267ce54516
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
22
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
55
|
-
stat.print_empirical_pdf_to_csv('pdf
|
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
|
+

|
84
|
+
#### beta(x, y)
|
85
|
+

|
86
|
+
#### erf(x) (Laplace function)
|
87
|
+

|
88
|
+
#### dawson_plus(x)
|
89
|
+

|
90
|
+
#### dawson_minus(x)
|
91
|
+

|
92
|
+
|
59
93
|
## Equation
|
60
|
-
|
61
|
-
|
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
|
101
|
+
Now you have no problems solving nonlinear equations. If you want, for example, to solve equation 
|
65
102
|
|
66
103
|
You need to complete the following steps:
|
67
104
|
- Equation should look like 
|
@@ -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**
|
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
|
-
|
86
|
-
|
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
|

|
92
132
|
|
93
|
-
where A is n
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
-
|
114
|
-
-
|
115
|
-
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/mathpack/integration.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
module Mathpack
|
2
2
|
module Integration
|
3
|
-
STEP = 0.
|
4
|
-
INTEGRATION_LIMIT =
|
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")
|
data/lib/mathpack/version.rb
CHANGED
data/lib/mathpack.rb
CHANGED
@@ -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.
|
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-
|
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
|