-
Notifications
You must be signed in to change notification settings - Fork 9
root: add FindBracketMono for finding brackets that hug a function's root #72
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
Open
fumin
wants to merge
4
commits into
gonum:master
Choose a base branch
from
fumin:findbracket
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,59 @@ | ||
// Copyright ©2025 The Gonum Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package root | ||
|
||
import "math" | ||
|
||
// FindBracketMono finds a bracket interval [a, b] where f(a)f(b) < 0. | ||
// f must be a monotonically increasing function. | ||
fumin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// guess is the initial guess of the bracket search. | ||
func FindBracketMono(f func(float64) float64, guess float64) (a, b float64) { | ||
fumin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Make sure initial guess has the same sign as the root. | ||
f0 := f(0) | ||
if (guess < 0 && f0 < 0) || (guess > 0 && f0 > 0) { | ||
guess *= -1 | ||
} | ||
|
||
// r is the rate in which we adjust the interval. | ||
var r float64 | ||
a = guess | ||
fa := f(a) | ||
if (a > 0) == (fa < 0) { | ||
r = 2 | ||
} else { | ||
r = 0.5 | ||
} | ||
|
||
// Expand bracket until x-axis is crossed. | ||
// maxiter value is based on https://github.com/boostorg/math/blob/boost-1.88.0/include/boost/math/policies/policy.hpp#L130 | ||
const maxiter = 200 | ||
crossed := false | ||
b = a * r | ||
fb := f(b) | ||
for range maxiter { | ||
if math.Signbit(fa) != math.Signbit(fb) || fa == 0 || fb == 0 { | ||
crossed = true | ||
break | ||
} | ||
a, fa = b, fb | ||
b *= r | ||
fb = f(b) | ||
} | ||
// If unable to cross x-axis, return the largest possible bracket. | ||
if !crossed { | ||
if r > 1 { | ||
b = math.Inf(int(math.Copysign(1, b))) | ||
} else { | ||
b = 0 | ||
} | ||
} | ||
|
||
fumin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Ensure a <= b | ||
if a > b { | ||
a, b = b, a | ||
} | ||
|
||
return a, b | ||
} |
This file contains hidden or 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,73 @@ | ||
// Copyright ©2025 The Gonum Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package root_test | ||
fumin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"gonum.org/v1/exp/root" | ||
) | ||
|
||
var findBracketMonoTests = []struct { | ||
fumin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name string | ||
f func(float64) float64 | ||
guess float64 | ||
validBracket func(a, b float64) bool | ||
}{ | ||
// Based on https://github.com/boostorg/math/blob/boost-1.88.0/test/test_toms748_solve.cpp | ||
{name: "f4.4", f: func(x float64) float64 { return math.Pow(x, 4) - 0.2 }, guess: 2}, | ||
{name: "f4.6", f: func(x float64) float64 { return math.Pow(x, 6) - 0.2 }, guess: 2}, | ||
{name: "f4.8", f: func(x float64) float64 { return math.Pow(x, 8) - 0.2 }, guess: 2}, | ||
{name: "f4.10", f: func(x float64) float64 { return math.Pow(x, 10) - 0.2 }, guess: 2}, | ||
{name: "f4.12", f: func(x float64) float64 { return math.Pow(x, 12) - 0.2 }, guess: 2}, | ||
|
||
// Based on https://github.com/boostorg/math/blob/boost-1.88.0/test/test_root_finding_concepts.cpp | ||
{name: "f1", f: func(x float64) float64 { return x*x*x - 27 }, guess: 27}, | ||
|
||
// Special cases. | ||
{name: "+Inf", f: func(x float64) float64 { return math.Atan(x) - 2 }, guess: 3, validBracket: func(a, b float64) bool { return a > 0 && math.IsInf(b, 1) }}, | ||
{name: "-Inf", f: func(x float64) float64 { return math.Atan(x) + 2 }, guess: 3, validBracket: func(a, b float64) bool { return math.IsInf(a, -1) && b < 0 }}, | ||
{name: "tiny positive", f: func(x float64) float64 { | ||
rt := math.SmallestNonzeroFloat64 | ||
switch { | ||
case x > rt: | ||
return 1 | ||
case x < rt: | ||
return -1 | ||
default: | ||
return 0 | ||
} | ||
}, guess: 3, validBracket: func(a, b float64) bool { return a == 0 && b > 0 }}, | ||
{name: "tiny negative", f: func(x float64) float64 { | ||
rt := -math.SmallestNonzeroFloat64 | ||
switch { | ||
case x > rt: | ||
return 1 | ||
case x < rt: | ||
return -1 | ||
default: | ||
return 0 | ||
} | ||
}, guess: -3, validBracket: func(a, b float64) bool { return a < 0 && b == 0 }}, | ||
} | ||
|
||
func TestFindBracketMono(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, test := range findBracketMonoTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
validBracket := test.validBracket | ||
if validBracket == nil { | ||
validBracket = func(a, b float64) bool { return test.f(a)*test.f(b) < 0 && a <= b } | ||
} | ||
|
||
a, b := root.FindBracketMono(test.f, test.guess) | ||
if !validBracket(a, b) { | ||
t.Errorf("%s: invalid bracket (%f, %f)", test.name, a, b) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.