Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fixed bug in find_root when full_output=True (size check assumed it w…
Browse files Browse the repository at this point in the history
…as false)

Also fixed the doctesting in expression.pyx for 1/tan(x)
  • Loading branch information
assaferan committed Sep 7, 2018
1 parent 91f14d3 commit 8feb5a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/sage/numerical/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,20 @@ def find_root(f, a, b, xtol=10e-13, rtol=2.0**-50, maxiter=100, full_output=Fals
a = s

import scipy.optimize
root = scipy.optimize.brentq(f, a, b,
brentqRes = scipy.optimize.brentq(f, a, b,
full_output=full_output, xtol=xtol, rtol=rtol, maxiter=maxiter)
# A check following ticket 4942, to ensure we actually found a root
# Maybe should use a different tolerance here?
# The idea is to take roughly the derivative and multiply by estimated
# value of the root
root = 0
if (full_output):
root = brentqRes[0]
else:
root = brentqRes
if (abs(f(root)) > max(abs(root * rtol * (right - left) / (b - a)), 1e-6)):
raise RuntimeError("Brent's method failed to find a zero for f on the interval")
return root
return brentqRes

def find_local_maximum(f, a, b, tol=1.48e-08, maxfun=500):
"""
Expand Down
12 changes: 11 additions & 1 deletion src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11733,8 +11733,18 @@ cdef class Expression(CommutativeRingElement):
-0.588532743981862...
sage: sin(x).find_root(-1,1)
0.0
sage: (1/tan(x)).find_root(3,3.5)
This example was ixed along with ticket 4942 -
there was an error in the example
pi is a root for tan(x), but an asymptote to 1/tan(x)
added an example to show handling of both cases::
sage: (tan(x)).find_root(3,3.5)
3.1415926535...
sage: (1/tan(x)).find_root(3, 3.5)
Traceback (most recent call last):
...
RuntimeError: Brent's method failed to find a zero for f on the interval
An example with a square root::
Expand Down

0 comments on commit 8feb5a9

Please sign in to comment.