On Mon, Aug 25, 2008 at 5:57 AM, Stan Schymanski <[EMAIL PROTECTED]> wrote:
>
> It seems that the find_minimum_on_interval and
> find_maximum_on_interval functions do not work properly. I found some
> posts about this and a ticket (http://trac.sagemath.org/sage_trac/
> ticket/2607). Are there any alternatives built into sage that would
> allow the numerical search for extreme values in a given interval?
>
> Here is another example of something going horribly wrong (I actually
> get that 'cannot coerce type...' message a lot when I try the
> find_maximum... function):

If you call _fast_float_ as illustrated below on your functions, find_* will
work, and also be much much faster:

sage: find_maximum_on_interval((-x^2)._fast_float_(x),-1,1)
(-7.7037197775489434e-34, -2.77555756156e-17)
sage: find_minimum_on_interval((-x^2)._fast_float_(x),-1,1)
(-0.99999992595132459, -0.999999962976)

find_* doesn't do this already since (1) _fast_float_ was written
after find_*, and (2) nobody has had the time to change find_*
to use _fast_float_.

>
> -
> | SAGE Version 3.1.1, Release Date: 2008-08-17                       |
> | Type notebook() for the GUI, and license() for information.        |
> ----------------------------------------------------------------------
>
> sage: find_minimum_on_interval(-x^2,-1,1)
> (-0.99999992595132459, -0.999999962976)
> sage: find_maximum_on_interval(-x^2,-1,1)
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
>
> /Users/sschym/Downloads/Free/sage-3.1.1/<ipython console> in
> <module>()
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/numerical/optimize.py in find_maximum_on_interval(f, a,
> b, tol, maxfun)
>    116         """
>    117         return -f(z)
> --> 118     minval, x = find_minimum_on_interval(g, a=a, b=b, tol=tol,
> maxfun=maxfun)
>    119     return -minval, x
>    120
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/numerical/optimize.py in find_minimum_on_interval(f, a,
> b, tol, maxfun)
>    158     a = float(a); b = float(b)
>    159     import scipy.optimize
> --> 160     xmin, fval, iter, funcalls = scipy.optimize.fminbound(f,
> a, b, full_output=1, xtol=tol, maxfun=maxfun)
>    161     return fval, xmin
>    162
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/scipy/optimize/optimize.py in fminbound(func, x1, x2, args,
> xtol, maxfun, full_output, disp)
>   1307         si = numpy.sign(rat) + (rat == 0)
>   1308         x = xf + si*max([abs(rat), tol1])
> -> 1309         fu = func(x,*args)
>   1310         num += 1
>   1311         fmin_data = (num, x, fu)
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/numerical/optimize.py in g(z)
>    115
>    116         """
> --> 117         return -f(z)
>    118     minval, x = find_minimum_on_interval(g, a=a, b=b, tol=tol,
> maxfun=maxfun)
>    119     return -minval, x
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/calculus/calculus.py in __call__(self, *args, **kwargs)
>   4607                             new_ops.append( op )
>   4608                         else:
> -> 4609                             new_ops.append( op(*[args[i] for i
> in indices]) )
>   4610                 except ValueError:
>   4611                     new_ops.append( op )
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/calculus/calculus.py in __call__(self, *args, **kwargs)
>   4613         #Check to see if all of the new_ops are symbolic
> constants
>   4614         #If so, then we should return a symbolic constant.
> -> 4615         new_ops = map(SR, new_ops)
>   4616         is_constant = all(map(lambda x: isinstance(x,
> SymbolicConstant), new_ops))
>   4617         if is_constant:
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/calculus/calculus.py in __call__(self, x)
>    449                 msg, s, pos = err.args
>    450                 raise TypeError, "%s: %s !!! %s" % (msg,
> s[:pos], s[pos:])
> --> 451         return self._coerce_impl(x)
>    452
>    453     def _coerce_impl(self, x):
>
> /Users/sschym/Downloads/Free/sage-3.1.1/local/lib/python2.5/site-
> packages/sage/calculus/calculus.py in _coerce_impl(self, x)
>    501             return self(x._sage_())
>    502         else:
> --> 503             raise TypeError, "cannot coerce type '%s' into a
> SymbolicExpression."%type(x)
>    504
>    505     def _repr_(self):
>
> TypeError: cannot coerce type '<class
> 'sage.calculus.equations.SymbolicEquation'>' into a
> SymbolicExpression.
> sage:
> >
>



-- 
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to