-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bisection root-finder in arbitrary dimension #7
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0b7a5c1
Add 1D bisection root-finder
dpsanders 844d135
Add bisection.jl file
dpsanders c080136
Rewrite bisection so that applies directly to IntervalBox
dpsanders eb287e7
Modifications to make bisection work with higher-dimensional functions
dpsanders 11c36fd
Restrict signature of bisection
dpsanders c903baa
Restrict type for Root object
dpsanders e9d6e37
Add tests for bisection on infinite 1D and 2D intervals
dpsanders d5f58ae
Fix failing test
dpsanders 46c1c2d
Add bisection test file
dpsanders a2aae73
Modify type of empty vector of Roots
dpsanders eec1811
Move bisection files to correct place
dpsanders 884c6ca
Add complex roots example
dpsanders 49dbd29
Complex bisection returns complex intervals
dpsanders 888825f
Add method for complex_bisection with two intervals instead of Interv…
dpsanders b3ef09f
Improve complex_bisection using Complex constructor
dpsanders 8734c27
Add bisection for vector of IntervalBox
dpsanders File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Example of finding roots of a complex function | ||
# f(z) = z^4 + z - 2 (book of Hansen-Walster, chap. 10) | ||
|
||
using IntervalRootFinding, IntervalArithmetic | ||
|
||
doc""" | ||
complex_bisection(f, X) | ||
|
||
Find complex roots of $f: \mathbb{C} \to \mathbb{C}$. | ||
|
||
Inputs: | ||
|
||
- `f`: function $f: \mathbb{C} \to \mathbb{C}$, i.e. a function that accepts a complex number and returns another complex number. | ||
|
||
- `X`: An `IntervalBox` specifying the bounds on the real and imaginary parts | ||
of `z`. | ||
|
||
""" | ||
function complex_bisection(f, X::IntervalBox) | ||
|
||
""" | ||
Make a 2D real version of the complex function `f` suitable for `bisection`, | ||
i.e. that accepts an `IntervalBox` and returns an `IntervalBox` | ||
""" | ||
function g(X::IntervalBox) | ||
z = Complex(X...) | ||
zz = f(z) | ||
|
||
return IntervalBox(reim(zz)) | ||
end | ||
|
||
roots = bisection(g, X) | ||
|
||
return g, [Complex(root.interval...) for root in roots] | ||
end | ||
|
||
""" | ||
complex_bisection(f, x, y) | ||
|
||
Version in which the bounds are specified as two separate `Interval`s. | ||
""" | ||
complex_bisection(f, x::Interval, y::Interval) = complex_bisection(f, x × y) | ||
|
||
f(z) = z^4 + z - 2 | ||
|
||
L = 10 | ||
g, roots = complex_bisection(f, -L..L, -L..L) | ||
|
||
println(roots) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
doc""" | ||
bisection(f, X; tolerance=1e-3) | ||
|
||
Find possible roots of the function `f` inside the `Interval` or `IntervalBox` `X`. | ||
""" | ||
function bisection{T<:Union{Interval,IntervalBox}}(f, X::T; tolerance=1e-3, debug=false) | ||
|
||
image = f(X) | ||
|
||
debug && @show X, image | ||
|
||
if !(zero(X) ⊆ image) | ||
return Root{typeof(X)}[] | ||
end | ||
|
||
if diam(X) < tolerance | ||
return [Root(X, :unknown)] | ||
end | ||
|
||
X1, X2 = bisect(X) | ||
|
||
if debug | ||
@show X1, X2 | ||
end | ||
|
||
return [bisection(f, X1, tolerance=tolerance); | ||
bisection(f, X2, tolerance=tolerance)] | ||
|
||
end | ||
|
||
|
||
function bisection{T<:Union{Interval,IntervalBox}}(f, V::Vector{T}; tolerance=1e-3, debug=false) | ||
|
||
return vcat([bisection(f, X, tolerance=tolerance, debug=debug) for X in V]) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using ValidatedNumerics, ValidatedNumerics.RootFinding | ||
using Base.Test | ||
|
||
@testset "Bisection tests" begin | ||
@testset "Single variable" begin | ||
f(x) = sin(x) + cos(x/2) + x/5 | ||
|
||
roots = bisection(f, -∞..∞, tolerance=1e-3) | ||
roots2 = [root.interval for root in roots] | ||
|
||
@test roots2 == [ Interval(-0.8408203124999999, -0.8398437499999999), | ||
Interval(3.6425781249999996, 3.6435546874999996), | ||
Interval(6.062499999999999, 6.063476562499999) | ||
] | ||
|
||
end | ||
|
||
@testset "Two variables" begin | ||
@intervalbox g(x, y) = (x^2 + y^2 - 1, y - x) | ||
|
||
X = (-∞..∞) × (-∞..∞) | ||
roots = bisection(g, X, tolerance=1e-3) | ||
roots2 = [root.interval for root in roots] | ||
|
||
@test roots2 == [IntervalBox(Interval(-0.7080078124999999, -0.7070312499999999), Interval(-0.7080078124999999, -0.7070312499999999)), | ||
IntervalBox(Interval(-0.7080078124999999, -0.7070312499999999), Interval(-0.7070312499999999, -0.7060546874999999)), | ||
IntervalBox(Interval(-0.7070312499999999, -0.7060546874999999), Interval(-0.7080078124999999, -0.7070312499999999)), | ||
IntervalBox(Interval(0.7060546874999999, 0.7070312499999999), Interval(0.7070312499999999, 0.7080078124999999)), | ||
IntervalBox(Interval(0.7070312499999999, 0.7080078124999999), Interval(0.7060546874999999, 0.7070312499999999)), | ||
IntervalBox(Interval(0.7070312499999999, 0.7080078124999999), Interval(0.7070312499999999, 0.7080078124999999))] | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not in test/REQUIRE
and this file isn't getting run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks.