root_solver 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/root_solver.rb +105 -0
- data/lib/root_solver/version.rb +3 -0
- data/root_solver.gemspec +34 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 227787f9dc1cff91f3f087ecc9796f2f67bc830e
|
4
|
+
data.tar.gz: 8035c9da849472d0c254c43227f3b3463b8f2c75
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70379f6266ee4851b5d7d772802c04a71ffec46612be048d1ef2720e95ae89ddd4719471d78aefcad6d805c0e873a5db995866fb28a3dc8e6352e768326437e3
|
7
|
+
data.tar.gz: ab199994448de0733f164a000fba9fde331a9693d002d16a876f5585f5ec36b6a98bd6e332cd9c29fba103bac5a35ec314c925f2bf5fdd6c3ef806dcdf4d794f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# RootSolver
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/root_solver`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'root_solver'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install root_solver
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/root_solver.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "root_solver"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/root_solver.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require "root_solver/version"
|
2
|
+
|
3
|
+
module RootSolver
|
4
|
+
class Newton
|
5
|
+
def initialize(f, x0 = 0, tol = 0.01, n = 10, eps = 0.001)
|
6
|
+
@f = f
|
7
|
+
@x0 = x0
|
8
|
+
@tol = tol
|
9
|
+
@n = n
|
10
|
+
@eps = eps
|
11
|
+
end
|
12
|
+
|
13
|
+
def solve(f = @f, x0 = @x0, tol = @tol, n = @n, eps = @eps)
|
14
|
+
y0 = f.call(x0)
|
15
|
+
y_prime = (f.call(x0 + eps) - y0) / eps
|
16
|
+
|
17
|
+
raise NonconvergenceError.new if y_prime.abs < tol
|
18
|
+
|
19
|
+
x = x0 - y0 / y_prime
|
20
|
+
y = f.call(x)
|
21
|
+
if y.abs < tol #successfully found root
|
22
|
+
x
|
23
|
+
elsif n <= 0 #solver not within threshold after n iterations.
|
24
|
+
x
|
25
|
+
else
|
26
|
+
solve(f, x, tol, n - 1, eps)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class Bisection
|
33
|
+
def initialize(f, low, high, tol = 0.01, n = 10)
|
34
|
+
@f = f
|
35
|
+
@tol = tol
|
36
|
+
@n = n
|
37
|
+
@high = high
|
38
|
+
@low = low
|
39
|
+
end
|
40
|
+
|
41
|
+
def solve(f = @f, low = @low, high = @high, tol = @tol, n = @n)
|
42
|
+
x = (high + low) / 2
|
43
|
+
y = f.call(x)
|
44
|
+
|
45
|
+
if y.abs < tol #successfully found root
|
46
|
+
x
|
47
|
+
elsif n <= 0 #break out of solver after so many iterations
|
48
|
+
x
|
49
|
+
elsif x_converge?(low, high, tol) && !crossing?(f, low, high)
|
50
|
+
raise NoRootError.new
|
51
|
+
else #narrow window of searching by half
|
52
|
+
if y > 0
|
53
|
+
high = x
|
54
|
+
else
|
55
|
+
low = x
|
56
|
+
end
|
57
|
+
solve(f, low, high, tol, n - 1)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
#root is between the high and low
|
64
|
+
def crossing?(f, low, high)
|
65
|
+
f.call(low) * f.call(high) <= 0
|
66
|
+
end
|
67
|
+
|
68
|
+
#high and low are approximately equivalent
|
69
|
+
def x_converge?(low, high, tol)
|
70
|
+
high - low < tol
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
class BisectionNewton
|
76
|
+
def initialize(f, low, high, tol = 0.1, n = 5)
|
77
|
+
@f = f
|
78
|
+
@tol = tol
|
79
|
+
@n = n
|
80
|
+
@high = high
|
81
|
+
@low = low
|
82
|
+
end
|
83
|
+
|
84
|
+
def solve
|
85
|
+
x1 = RootSolver::Bisection.new(@f, @low, @high, @tol, @n).solve
|
86
|
+
RootSolver::Newton.new(@f, x1, @tol).solve
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class Error < RuntimeError
|
91
|
+
end
|
92
|
+
|
93
|
+
class NoRootError < Error
|
94
|
+
def initialize(msg = "Root not found.")
|
95
|
+
super
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class NonconvergenceError < Error
|
100
|
+
def initialize(msg = "Solver not converging.")
|
101
|
+
super
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/root_solver.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'root_solver/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "root_solver"
|
8
|
+
spec.version = RootSolver::VERSION
|
9
|
+
spec.authors = ["Lindsay Hannon"]
|
10
|
+
spec.email = ["lindsay@teamairship.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ A root-solver that uses bisection then newton's method to approximate a function's root }
|
13
|
+
spec.description = %q{ Newton's method works best when you have a reasonable first guess, while bisection is a
|
14
|
+
better choice when your first guess may be far away (or near a zero slope). }
|
15
|
+
spec.homepage = "https://github.com/teamairship/root_solver"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: root_solver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lindsay Hannon
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: " Newton's method works best when you have a reasonable first guess,
|
56
|
+
while bisection is a\n better choice when your first guess
|
57
|
+
may be far away (or near a zero slope). "
|
58
|
+
email:
|
59
|
+
- lindsay@teamairship.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/root_solver.rb
|
72
|
+
- lib/root_solver/version.rb
|
73
|
+
- root_solver.gemspec
|
74
|
+
homepage: https://github.com/teamairship/root_solver
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata:
|
78
|
+
allowed_push_host: https://rubygems.org
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.6.6
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: A root-solver that uses bisection then newton's method to approximate a function's
|
99
|
+
root
|
100
|
+
test_files: []
|