Skip to content
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

Find_root bug #3980

Closed
kcrisman opened this issue Aug 28, 2008 · 8 comments
Closed

Find_root bug #3980

kcrisman opened this issue Aug 28, 2008 · 8 comments

Comments

@kcrisman
Copy link
Member

The reciprocal of tangent is not a constant function, but Sage says otherwise.

sage: z=tan
sage: z
tan
sage: 1/z
1/tan
sage: find_root(1/z,1,2)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)

<snip>

/Applications/sage/local/lib/python2.5/site-packages/sage/numerical/optimize.py in find_root(f, a, b, xtol, rtol, maxiter, full_output)
     52     """
     53     try:
---> 54         return f.find_root(a=a,b=b,xtol=xtol,rtol=rtol,maxiter=maxiter,full_output=full_output)
     55     except AttributeError:
     56         pass

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.py in find_root(self, a, b, var, xtol, rtol, maxiter, full_output)
   3088                     return a
   3089                 else:
-> 3090                     raise RuntimeError, "no zero in the interval, since constant expression is not 0."
   3091             var = repr(w[0])
   3092 

RuntimeError: no zero in the interval, since constant expression is not 0.

Component: calculus

Issue created by migration from https://trac.sagemath.org/ticket/3980

@kcrisman kcrisman added this to the sage-3.2 milestone Aug 28, 2008
@jicama jicama mannequin assigned burcin and unassigned sagetrac-jkantor Aug 31, 2008
@jicama jicama mannequin added c: calculus and removed c: numerical labels Aug 31, 2008
@sagetrac-anakha
Copy link
Mannequin

sagetrac-anakha mannequin commented Sep 8, 2008

comment:2

I have a less obscure example of this.

sage: find_root(sin, -1, 1)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)

/Volumes/Place/anakha/sage-3.1.2.alpha4/<ipython console> in <module>()

/Volumes/Place/anakha/sage-3.1.2.alpha4/local/lib/python2.5/site-packages/sage/numerical/optimize.py in find_root(f, a, b, xtol, rtol, maxiter, full_output)
     52     """
     53     try:
---> 54         return f.find_root(a=a,b=b,xtol=xtol,rtol=rtol,maxiter=maxiter,full_output=full_output)
     55     except AttributeError:
     56         pass

/Volumes/Place/anakha/sage-3.1.2.alpha4/local/lib/python2.5/site-packages/sage/calculus/calculus.py in find_root(self, a, b, var, xtol, rtol, maxiter, full_output)
   3120                     return a
   3121                 else:
-> 3122                     raise RuntimeError, "no zero in the interval, since constant expression is not 0."
   3123             var = repr(w[0])
   3124 

RuntimeError: no zero in the interval, since constant expression is not 0.

And I have traced down the bug to this:

sage: sin.variables()
()
sage: (1/tan).variables()
()

Since these expressions contain no variables they are deemed constant and since they are not equal to 0 they have no roots. You can fix your example by trying:

sage: find_root(1/tan(x), 1, 2)
1.5707963267948968

Also note that this works:

sage: (1/tan)(1)
1/tan(1)

Although, technically no variables are there to be replaced.

I don't know if this is really a bug or not, or how to fix it.

@jicama
Copy link
Mannequin

jicama mannequin commented Sep 8, 2008

comment:3

The problem is that find_root is asking the wrong question of its first argument. It asks "what are your variables?" Instead, it should ask "do you return a number when called with one argument?" I will try to code this up when I get a chance.

@jicama
Copy link
Mannequin

jicama mannequin commented Sep 9, 2008

comment:4

Here's a patch that avoids asking the function what it's variables are.

@jicama jicama mannequin added the s: needs review label Sep 9, 2008
@jicama jicama mannequin assigned jicama and unassigned burcin Sep 9, 2008
@sagetrac-mhampton
Copy link
Mannequin

sagetrac-mhampton mannequin commented Sep 21, 2008

comment:5

Somehow this is now too permissive, since the following should return an error:

sage: var('t')
sage: find_root(1/t-x,0,2)
     1.0000000000005

@jicama
Copy link
Mannequin

jicama mannequin commented Sep 29, 2008

Attachment: 3980.patch.gz

@jicama
Copy link
Mannequin

jicama mannequin commented Sep 29, 2008

comment:6

Good catch mhampton. I modified the patch to throw a NotImplementedError if self.number_of_variables() is greater than 1. This is closer to the old behavior. I added a new test to explicitly check the case you gave above.

@jicama jicama mannequin added s: needs review and removed s: needs work labels Sep 29, 2008
@sagetrac-mhampton
Copy link
Mannequin

sagetrac-mhampton mannequin commented Oct 1, 2008

comment:7

OK, this passes tests and it seems OK. One thing I noticed, which I think can be considered beyond the scope of this patch, is that

var('t')
plot(sin(t*x),-1,1)

quickly fails with "ValueError: free variable: x", which is good, but:

plot(lambda t: sin(t*x),-1,1)

tries to plot 400 values and finally gives

verbose 0 (3585: plot.py, _plot) WARNING: When plotting, failed to evaluate function at 400 points.
verbose 0 (3585: plot.py, _plot) Last error message: 'float() argument must be a string or a number'

and an empty plot. It would be nice to speed up that failure if you know an easy way.

@sagetrac-mabshoff
Copy link
Mannequin

sagetrac-mabshoff mannequin commented Oct 1, 2008

comment:8

Merged in Sage 3.1.3.alpha3.

@sagetrac-mabshoff sagetrac-mabshoff mannequin closed this as completed Oct 1, 2008
@sagetrac-mabshoff sagetrac-mabshoff mannequin modified the milestones: sage-3.2, sage-3.1.3 Oct 1, 2008
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants