max and min evaluating symbolic expressions too soon

130 views
Skip to first unread message

Matt Rissler

unread,
Sep 14, 2009, 8:02:54 AM9/14/09
to sage-support
So I'm trying to get around the fact that plotting half of an
ellipsoid with plot3d is nigh impossible,

x,y=var('x,y')
plot3d(sqrt(1-(x^2/9+y^2/4)),(x,-3,3),(y,-2,2))

but I stumbled across this problem in the process. Since the problem
is that we can't evaluate at some points lets replace them. So trying
this

sqrt(max(1-(x^2/9+y^2/4),0))

returns this

sqrt(-1/9*x^2 - 1/4*y^2 + 1)

So passing this into plot3d doesn't fix anything.

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.

Thanks,
Matt

Burcin Erocal

unread,
Sep 16, 2009, 10:34:39 AM9/16/09
to sage-s...@googlegroups.com
Hi Matt,

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

kcrisman

unread,
Sep 16, 2009, 11:16:56 AM9/16/09
to sage-support
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.

- kcrisman


On Sep 16, 10:34 am, Burcin Erocal <bur...@erocal.org> wrote:
> Hi Matt,
>
> On Mon, 14 Sep 2009 05:02:54 -0700 (PDT)
>

Burcin Erocal

unread,
Sep 17, 2009, 10:10:14 AM9/17/09
to sage-s...@googlegroups.com
On Wed, 16 Sep 2009 08:16:56 -0700 (PDT)
kcrisman <kcri...@gmail.com> wrote:

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

Matt Rissler

unread,
Sep 24, 2009, 8:08:12 PM9/24/09
to sage-support

I finally got back to looking at this thread. That does work, though
it breaks calling plot3d with the following format:

max_symbolic=MaxSymbolic()
f(x,y)=sqrt(max_symbolic(9-x^2-y^2,0))
plot3d(f(x,y),(x,-3,3),(y,-3,3))

However

plot3d(f,(-3,3),(-3,3))

works fine. So it's a fine work around.

Thanks,

Matt

On Sep 16, 9:34 am, Burcin Erocal <bur...@erocal.org> wrote:
> Hi Matt,
>
> On Mon, 14 Sep 2009 05:02:54 -0700 (PDT)
>

Iwan Lappo-Danilewski

unread,
Sep 25, 2009, 9:50:52 AM9/25/09
to sage-support
This is a really nasty bug.. Symbolic maxima and minima are
mathematical standard tools, they should at least be _callable_ in
standard sage. One can see the problem this way:

p = var("p")
show( plot(max(p,3),(p,0,10)) )
show( plot(min(p,3),(p,0,10)) )

Could someone please post a quickhack to define min_symbolic() other
than

max_symbolic(-a,-b)*(-1)


Reply all
Reply to author
Forward
0 new messages