Mathpack
This gem includes a collection of mathematical methods
Installation
Add this line to your application's Gemfile:
gem 'mathpack'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mathpack
Information
Gem mathpack allows to count statistical functions through Statistics class, solve nonlinear equations through Equation module
solve method, solve systems of linear equations through SLE module solve method.
Statistics
Statistics class have following methods
- number - returns a number of elements in series
- mean - returns a mean of series
- variance - returns a variance of series
- skewness - returns a skewness of series
- kurtosis - returns a kurtosis
- min - returns the minimal element of series
- max - returns the maxinal element of series
- raw_moment - returns the nth raw moment of series
- central_moment - returns the nth central moment of series
- empirical_cdf - returns empirical distribution function value in some point
- empirical_pdf - returns empirical probability density function value in some point
- print_empirical_cdf_to_csv - allows to print empirical_cdf line chart values to
.csvfile with name filename - print_empirical_cdf_to_csv - allows to print empirical_cdf line chart values to
.csvfile with name filename
Usage
stat = Mathpack::Statistics.new([1, 2, 5, 6])
stat.number() #=> 4
stat.mean() #=> 3.5
stat.variance() #=> 4.25
stat.kurtosis() #=> 1.221453287197232
stat.skewness() #=> 0.0
stat.min() #=> 1
stat.max() #=> 6
stat.raw_moment(3) #=> 87.5
stat.central_moment(4) #=> 22.0625
stat.empirical_cdf(5.5) #=> 0.75
stat.empirical_pdf(3) #=> 0.07639393483317147
stat.print_empirical_cdf_to_csv('cdf.csv') #=> nil
stat.print_empirical_pdf_to_csv('pdf.csv') #=> nil
Equation
Equation module has only one method
- solve - method, which allows to solve nonlinear equations. Neccessary params are
epsrepresenting calculations accuraccy andstartrepresenting point to start root search
Usage
Now you have no problems solving nonlinear equations. If you want, for example, to solve equation
You need to complete the following steps:
- Equation should look like
- For our equation
- Choose the calculations accurracy. For example
- Choose some point near the expected root of equation. For example
Then to solve equation you should call
Mathpack::Equation.solve(start: 0, eps: 0.00001){|x| x**2 - Math.sin(x+1)})
Here is some examples of solve function usage
Mathpack::Equation.solve(start: 0, eps: 0.00001){|x| x**2 - Math.sin(x+1)})
Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| 1/x - Math.log(x)})
Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| x**2 - 2*x + 1})
Mathpack::Equation.solve(start: 0.01, eps: 0.00001){|x| Math.exp(x-2) - Math.sin(x)})
SLE
SLE module has only one method
- solve - method, which allows to solve system of linear equations. Neccessary params are
matrixrepresenting system matrix andf. Params can be Array or Matrix class. If the system can't be solved, method raise exception
Usage
Let's solve some system of linear equations. It can be written as
where A is n*n matrix, X - vector of unknown, B - vector
If you want to solve this system you should call
Mathpack::SLE.solve(matrix: A, f: B)
Parameter A can be Array or Matrix class. If Array is given result will be Array class. If Matrix is given result will be Matrix class.
a = [[1, 2, 3], [4, 5, 6],[3, 5, 2]]
b = [15, 30, 15]
Mathpack::SLE.solve(matrix: a, f: b) #=> [-1.0, 2.0, 4.0]
a = Matrix[[1, 2, 3], [4, 5, 6],[3, 5, 2]]
b = Matrix.row_vector [15, 30, 15]
Mathpack::SLE.solve(matrix: a, f: b) #=> Matrix[[-1.0, 2.0, 4.0]]
Contributing
- Fork it ( https://github.com/[my-github-username]/mathpack/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request