step one
This commit is contained in:
parent
7a8d5d13fa
commit
9e663db9dc
68 changed files with 5647 additions and 2958 deletions
19
internal/numerics/search.go
Normal file
19
internal/numerics/search.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package numerics
|
||||
|
||||
// Bisect returns the largest index i in [imin, imax] such that f(i) < target,
|
||||
// assuming f is monotonically nondecreasing on that range.
|
||||
//
|
||||
// If target <= f(imin), returns imin. If target > f(imax), returns imax.
|
||||
// Performs O(log(imax-imin)) evaluations of f.
|
||||
func Bisect(imin, imax int, target float64, f func(i int) float64) int {
|
||||
lo, hi := imin, imax
|
||||
for lo < hi {
|
||||
mid := (lo + hi + 1) / 2
|
||||
if target <= f(mid) {
|
||||
hi = mid - 1
|
||||
} else {
|
||||
lo = mid
|
||||
}
|
||||
}
|
||||
return lo
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue