On Mon, 14 Sep 2009 05:02:54 -0700 (PDT)
Matt Rissler <disc...@gmail.com> wrote:
> Is it possible to have max behave as you would expect with a symbolic
> expression, i.e. wait until you evaluate it or restrict the domain to
> check what is the maximum of the two or more values.
Below is a quick implementation of a symbolic max function. It seems
to work here:
sage: max_symbolic = MaxSymbolic()
sage: max_symbolic(5,0)
5
sage: max_symbolic(x,0)
max(x, 0)
sage: max_symbolic(x,0).subs(x=5)
5
Is this at all useful? Note that trying to evaluate this many times
might be very very slow.
Cheers,
Burcin
----
from sage.symbolic.function import SFunction
class MaxSymbolic(SFunction):
def __init__(self):
SFunction.__init__(self, 'max', eval_func=self._eval_)
def _eval_(*args):
largs = len(args)
if largs == 0:
raise TypeError, "expected one or more arguments"
if largs == 1:
return args[0]
res = 0
for x in args:
try:
if hasattr(x, 'pyobject'):
pyobj = x.pyobject()
else:
pyobj = x
except TypeError:
return None
res = max(pyobj, res)
return res
>
> Might there be a way to do something that doesn't conflict with the
> builtin max function in the same way as the (nearly reviewed) #3587
> seems to avoid conflict with the builtin sum function? This would be
> pretty useful, as currently:
>
> sage: var('x,y')
> (x, y)
> sage: max(x,y)
> x
> sage: f(x)=1+x;g(x)=2-x
> sage: max(f,g)
> x |--> x + 1
>
> which last result is... debatable.
It's not hard to provide symbolic max and min functions, and we should
definitely do this.
http://trac.sagemath.org/sage_trac/ticket/6949
If we can make them as fast as the builtin max and min, we can consider
replacing the builtin ones with the symbolic alternatives.
Cheers,
Burcin