Hi Matt,

On Mon, 14 Sep 2009 05:02:54 -0700 (PDT)
Matt Rissler <[email protected]> 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

--~--~---------~--~----~------------~-------~--~----~
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