mathpack 0.0.7 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8697da38b49527c038d846912f468f313c8a6b4f
4
- data.tar.gz: d6f4f88f6ea49ba317cdcab989cee5d8f838b28c
3
+ metadata.gz: f2feda03bce968d1a492dd5c6cfa99bfe1d8796c
4
+ data.tar.gz: 6b4908827c465f97ca042c266a393e8fb572ee94
5
5
  SHA512:
6
- metadata.gz: 7cfa0c7f1baaaa08af5a3b9858ce04b563ab10c664c5f07813c9f17a2cd2933d35a73254c9d30ef4801854e4bedbe02397888a866f282009d9f5e186b8018418
7
- data.tar.gz: 0f3733b6b9019d68a52220fbcfbcdd583e9aad45a3a70e2e05e394c6543dcf3d89dc71ce3c6c6b6ddf0512f9b4f9b07586ca97b9df0d623f496a1514488889e2
6
+ metadata.gz: e013b0cb1cb1339ca1b2572593b8d77dea7d4b11f678675a199125595b682f559a651d3d1ab807bb346e42084997b234bb1f969f503434be0ec5f69073adf6aa
7
+ data.tar.gz: 56d13a5f3c6fea715fa823e21197ef368d4591f5d3b83c250d56174d122d762a1cf81e1446788a9d2a7e054f1e5cc21d300555844d54c265ea506de758e10055
data/README.md CHANGED
@@ -18,7 +18,8 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install mathpack
20
20
  ## Information
21
- Gem `mathpack` allows to count statistical functions throught `Statistics` class
21
+ Gem `mathpack` allows to count statistical functions through `Statistics` class, solving nonlinear equations through `Equation` module
22
+ `solve` method
22
23
  ## Statistics
23
24
  `Statistics` class have following methods
24
25
  - **number** - returns a number of elements in series
@@ -32,20 +33,20 @@ Gem `mathpack` allows to count statistical functions throught `Statistics` class
32
33
  - **central_moment** - returns the *nth* central moment of series
33
34
  - **empirical_cdf** - returns *empirical distribution function* value in some point
34
35
  - **empirical_pdf** - returns *empirical probability density function* value in some point
35
- - **print_empirical_cdf_to_csv** - allows to print empirical_cdf grafic values to `.csv` file with name *filename*
36
- - **print_empirical_cdf_to_csv** - allows to print empirical_cdf grafic values to `.csv` file with name *filename*
36
+ - **print_empirical_cdf_to_csv** - allows to print empirical_cdf line chart values to `.csv` file with name *filename*
37
+ - **print_empirical_cdf_to_csv** - allows to print empirical_cdf line chart values to `.csv` file with name *filename*
37
38
 
38
- ## Usage
39
+ ### Usage
39
40
  ```ruby
40
41
  stat = Mathpack::Statistics.new([1, 2, 5, 6])
41
42
  stat.number() #=> 4
42
43
  stat.mean() #=> 3.5
43
- stat.variance() #=> 4.25
44
+ stat.variance() #=> 4.25
44
45
  stat.kurtosis() #=> 1.221453287197232
45
46
  stat.skewness() #=> 0.0
46
47
  stat.min() #=> 1
47
48
  stat.max() #=> 6
48
- stat.raw_moment(3) #=> 87.5
49
+ stat.raw_moment(3) #=> 87.5
49
50
  stat.central_moment(4) #=> 22.0625
50
51
  stat.empirical_cdf(5.5) #=> 0.75
51
52
  stat.empirical_pdf(3) #=> 0.07639393483317147
@@ -53,6 +54,32 @@ stat.print_empirical_cdf_to_csv('cdf.csv') #=> nil
53
54
  stat.print_empirical_pdf_to_csv('pdf.csv') #=> nil
54
55
  ```
55
56
 
57
+ ##Equation
58
+ `Equation` module has only one method
59
+ - **solve** - method, which allows to solve *nonlinear equations*. Neccessary params are `eps` representing calculations accuraccy and `start` representing point to start root search
60
+
61
+ ###Usage
62
+ 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
+
64
+ You need to complete the following steps:
65
+ 1. Equation should look like
66
+ ![equation](http://latex.codecogs.com/gif.latex?%5Ctiny%20f%28x%29%20%3D%200)
67
+ 2. For our equation ![equation](http://latex.codecogs.com/gif.latex?%5Csmall%20f%28x%29%20%3D%20x%5E%7B2%7D%20-%20%5Csin%28x+1%29)
68
+ 3. Choose the calculations accurracy. For example ![equation](http://latex.codecogs.com/gif.latex?%5Csmall%200.00001)
69
+ 4. Choose some point near the expected root of equation. For example ![equation](http://latex.codecogs.com/gif.latex?%5Csmall%200)
70
+
71
+ Then to solve equation you should call
72
+ ```ruby
73
+ Mathpack::Equation.solve(start: 0, eps: 0.00001){|x| x**2 - Math.sin(x+1)})
74
+ ```
75
+ Here is some examples of **solve** function usage
76
+ ```ruby
77
+ Mathpack::Equation.solve(start: 0, eps: 0.00001){|x| x**2 - Math.sin(x+1)})
78
+ Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| 1/x - Math.log(x)})
79
+ Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| x**2 - 2*x + 1})
80
+ Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| Math.exp(x-2) - Math.sin(x)})
81
+ ```
82
+
56
83
  ## Contributing
57
84
 
58
85
  1. Fork it ( https://github.com/[my-github-username]/mathpack/fork )
@@ -0,0 +1,14 @@
1
+ module Mathpack
2
+ module Equation
3
+ def self.solve(param = {}, &function)
4
+ derivate_eps = 1e-5
5
+ xk1 = param[:start]
6
+ loop do
7
+ xk = xk1
8
+ xk1 = xk - function.call(xk)/((function.call(xk + derivate_eps) - function.call(xk - derivate_eps))/(2*derivate_eps))
9
+ break if (xk1 - xk).abs < param[:eps]
10
+ end
11
+ return xk1
12
+ end
13
+ end
14
+ end
File without changes
@@ -1,3 +1,3 @@
1
1
  module Mathpack
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/mathpack.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "mathpack/version"
2
- require 'statistics'
2
+ require 'mathpack/statistics'
3
+ require 'mathpack/equation'
@@ -0,0 +1,14 @@
1
+ describe 'Equations' do
2
+ require 'mathpack/equation'
3
+
4
+ context 'calculate nonlinear equations' do
5
+
6
+ it 'should solve equation' do
7
+ eps = 0.00001
8
+ expect(Mathpack::Equation.solve(start: 0, eps: eps){|x| x**2 - Math.sin(x+1)}).to be_between(-0.613763 - eps, -0.613763 + eps)
9
+ expect(Mathpack::Equation.solve(start: 0.01, eps: eps){|x| 1/x - Math.log(x)}).to be_between(1.76322 - eps, 1.76322 + eps)
10
+ expect(Mathpack::Equation.solve(start: 0.01, eps: eps){|x| x**2 - 2*x + 1}).to be_between(1.0 - eps, 1.0 + eps)
11
+ expect(Mathpack::Equation.solve(start: 0.01, eps: eps){|x| Math.exp(x-2) - Math.sin(x)}).to be_between(0.159396 - eps, 0.159396 + eps)
12
+ end
13
+ end
14
+ end
@@ -1,7 +1,7 @@
1
1
  describe 'Statistics' do
2
- require 'statistics'
2
+ require 'mathpack/statistics'
3
3
 
4
- context "calculated using class methods" do
4
+ describe "calculated statistics functions" do
5
5
 
6
6
  let(:data) { [1,5,4,2,3,4,5,7,2,7] }
7
7
  let(:stat) { Mathpack::Statistics.new(data) }
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.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxmilan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-27 00:00:00.000000000 Z
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,9 +52,11 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - lib/mathpack.rb
55
+ - lib/mathpack/equation.rb
56
+ - lib/mathpack/statistics.rb
55
57
  - lib/mathpack/version.rb
56
- - lib/statistics.rb
57
58
  - mathpack.gemspec
59
+ - spec/equation_spec.rb
58
60
  - spec/statistics_spec.rb
59
61
  homepage: ''
60
62
  licenses:
@@ -81,4 +83,5 @@ signing_key:
81
83
  specification_version: 4
82
84
  summary: Summary
83
85
  test_files:
86
+ - spec/equation_spec.rb
84
87
  - spec/statistics_spec.rb