ChebyRuby 0.0.1.pre

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c0f9d3747a36682aa90b2b738c563d5e744f7b6
4
+ data.tar.gz: 66f91daacd639572b3ee61bc5cf90664c5d45702
5
+ SHA512:
6
+ metadata.gz: 35b08a1ba0dee7629ee11c83ca1d461d93d32b31761ee92e4a9b83e9490fa2a6282f873d25a9deb866aed7e3ee5a030aa24a14cad79ed4b66014d46a1e6d782e
7
+ data.tar.gz: c2cb87ab8481d85bb2f8c085f1d9c6182954188e58eb82d7aa5a2f5572c598a2d9ed943276ed85fa24b682947d53dfcd21e9c84c693e173df6e0a0131c139896
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc 'Run tests'
8
+ task default: :test
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'chebyruby'
4
+ puts 'Welcome!'
@@ -0,0 +1,8 @@
1
+ class ChebyRuby
2
+
3
+ end
4
+
5
+ require 'chebyruby/romberg_integration'
6
+ require 'chebyruby/simpsons_integration'
7
+ require 'chebyruby/integration'
8
+ require 'chebyruby/univariate_function'
@@ -0,0 +1,5 @@
1
+ module ChebyRuby::Differentiation
2
+ def slope(func, a, b)
3
+ (func.value(b) - func.value(a))/(b-a)
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'differentiation'
2
+ class ChebyRuby::FiniteDifferencing
3
+ include Differentiation
4
+
5
+ attr_accessor :func
6
+
7
+ def initialize(func)
8
+ @func = func
9
+ end
10
+
11
+ def forward(x, h = 0.1, order = 1)
12
+ (0..order).map do |i|
13
+ (-1)**i * FiniteDifferencing::binomial(order, i) *
14
+ func.value(x + (order - i) * h)
15
+ end.inject(:+) / (h**order).to_f
16
+ end
17
+
18
+ def backward(x, h = 0.1, order = 1)
19
+ (0..order).map do |i|
20
+ (-1)**i * FiniteDifferencing::binomial(order, i) *
21
+ func.value(x - i * h)
22
+ end.inject(:+) / (h**order).to_f
23
+ end
24
+
25
+ def central(x, h = 0.1, order = 1)
26
+ (0..order).map do |i|
27
+ (-1)**i * FiniteDifferencing::binomial(order, i) *
28
+ func.value(x + (order/2.0 - i) * h)
29
+ end.inject(:+) / (h**order).to_f
30
+ end
31
+
32
+ private
33
+ def FiniteDifferencing.binomial(n, k)
34
+ (1..k).map do |i|
35
+ (n + 1 - i)/i.to_f
36
+ end.inject(:*) || 1
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ module ChebyRuby::Integration
2
+ def trap_integrate(func, a, b, iterations = 32)
3
+ h = (b - a)/iterations.to_f
4
+ mesh = (a..b).step(h).to_a[1...-1]
5
+ summand = func.value(a) +
6
+ 2 * (mesh.map(&func.func).inject(:+) || 0) + func.value(b)
7
+ (b - a)/(2.0 * iterations) * summand
8
+ end
9
+
10
+ def accuracy_comparison(a, b, method = 'Trapezoid', cm = 'Trapezoid')
11
+ case method
12
+ when 'Trapezoid' then (integrate(a, b) - trap_integrate(func, a, b)).abs
13
+ when 'Romberg' then (integrate(a, b) - Romberg.sintegrate(func, a, b)).abs
14
+ when 'Simpsons' then (integrate(a, b) - Simpsons.sintegrate(func, a, b)).abs
15
+ end
16
+ end
17
+
18
+ class << self
19
+ def sintegrate(func, a, b)
20
+ self.new(func).integrate(a, b)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'integration'
2
+
3
+ class ChebyRuby::RombergIntegration
4
+ include ChebyRuby::Integration
5
+
6
+ attr_accessor :func
7
+
8
+ def initialize(func)
9
+ @func = func
10
+ end
11
+
12
+ def integrate(a, b, iterations = 8)
13
+ t = Array.new(iterations)
14
+ iterations.times do |i|
15
+ t[i] = []
16
+ t[i][0] = trap_integrate(func, a, b, 2 ** i).to_f
17
+ end
18
+ (1...t.length).each do |i|
19
+ (1..i).each do |k|
20
+ t[i][k] = ((4 ** k) * t[i][k-1] - t[i-1][k-1])/(4**k - 1).to_f
21
+ end
22
+ end
23
+ t.last.last
24
+ end
25
+
26
+ def accuracy_comparison(a, b, method = 'Trapezoid')
27
+ super.accuracy_comparison(a, b, method, 'RombergIntegration')
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'integration'
2
+
3
+ class ChebyRuby::SimpsonsIntegration
4
+ include ChebyRuby::Integration
5
+
6
+ attr_accessor :func
7
+
8
+ def initialize(func)
9
+ @func = func
10
+ end
11
+
12
+ def integrate(a, b, iterations = 32)
13
+ h = (b - a) / iterations.to_f
14
+ mesh = (a..b).step(h).to_a[1...-1]
15
+ even = mesh.each_with_index.select{|i, j| j.odd?}.map(&:first)
16
+ odd = mesh.each_with_index.select{|i, j| j.even?}.map(&:first)
17
+ summand = func.value(a) + func.value(b) +
18
+ 2 * even.map(&func.func).inject(:+) + 4 * odd.map(&func.func).inject(:+)
19
+ (h / 3.0) * summand
20
+ end
21
+
22
+ def accuracy_comparison(a, b, method = 'Trapezoid')
23
+ super.accuracy_comparison(a, b, method, 'RombergIntegration')
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ # This is the class for a univariate function.
2
+ #
3
+ # @attr func [Lambda] the univariate function.
4
+ class ChebyRuby::UnivariateFunction
5
+ attr_accessor :func
6
+
7
+ # The constructor for the class UnivariateFunction.
8
+ #
9
+ # @param [Lambda] func which represents the function.
10
+ def initialize(&func)
11
+ @func = func
12
+ end
13
+
14
+ # This gets the value of the function at a specified value.
15
+ #
16
+ # @param [Numeric] x the value at which the function is evaluated.
17
+ # @return the value of the function at x.
18
+ def value(x)
19
+ func.call(x.to_f)
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ require 'minitest/autorun'
2
+ require 'chebyruby'
3
+
4
+ class TestFunction < Minitest::Test
5
+ def setup
6
+ @sin = ChebyRuby::UnivariateFunction.new {|x| Math.sin(x)}
7
+ @exp = ChebyRuby::UnivariateFunction.new {|x| Math.exp(x)}
8
+ @pol = ChebyRuby::UnivariateFunction.new {|x| x**2 + 2*x + 1}
9
+ end
10
+
11
+ def test_functions_return_floats
12
+ assert_kind_of Float, @sin.value(rand)
13
+ assert_kind_of Float, @exp.value(rand)
14
+ assert_kind_of Float, @pol.value(rand)
15
+ end
16
+
17
+ def test_functions_return_floats_with_integer_parameters
18
+ assert_kind_of Float, @sin.value(1)
19
+ assert_kind_of Float, @exp.value(0)
20
+ assert_kind_of Float, @pol.value(0)
21
+ end
22
+
23
+ def test_functions_return_expected_values
24
+ assert_in_delta 0, @sin.value(0), 1e-8
25
+ assert_in_delta 0, @sin.value(Math::PI), 1e-8
26
+ assert_in_delta 1, @exp.value(0), 1e-8
27
+ assert_in_delta Math::E, @exp.value(1), 1e-8
28
+ z = rand
29
+ assert_in_delta (Math::E ** z), @exp.value(z), 1e-8
30
+ assert_in_delta 1, @pol.value(0), 1e-8
31
+ assert_in_delta 4, @pol.value(1), 1e-8
32
+ f = -> (x) {x**2 + 2*x + 1}
33
+ assert_in_delta f.call(z), @pol.value(z), 1e-8
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ChebyRuby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ platform: ruby
6
+ authors:
7
+ - Eli Sadoff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for numerical analysis and scientific computing.
14
+ email: snood1205@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Rakefile
20
+ - bin/chebyruby
21
+ - lib/chebyruby.rb
22
+ - lib/chebyruby/differentiation.rb
23
+ - lib/chebyruby/finite_differencing.rb
24
+ - lib/chebyruby/integration.rb
25
+ - lib/chebyruby/romberg_integration.rb
26
+ - lib/chebyruby/simpsons_integration.rb
27
+ - lib/chebyruby/univariate_function.rb
28
+ - test/test_function.rb
29
+ homepage: https://github.com/snood1205/chebyruby
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.5.1
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: A gem for numerical analysis and scientific computing.This is a pre-release
53
+ so beware.
54
+ test_files:
55
+ - test/test_function.rb