Hi Emmanuel,

On 2012-12-22, Emmanuel Charpentier <emanuel.charpent...@gmail.com> wrote:
> So, if I follow you, Sage's add was designed from the start to be=20
> overloaded by class methods,

You mean: *python's* add was designed in that way.

> As for the performance : class matching and method dispatching would occur=
> every time a hypothetical max method would be used, and this is considered=
> bad performance-wise. May I note that fundamentally identical class=20
> matching and method dispatching occurs every time one uses '+'

No, you misunderstood. Python's max and min functions certainly dispatch
to comparison methods of the classes, in order to find out what instance
is biggest/smallest. That's not the problem.

The problem is that you won't (and can't) get a straight answer out of a
pair of symbolic expressions if you ask "which of you two is bigger":

 sage: x>1  # Note: The result is a symbolic expression!
 x > 1
 sage: type(x>1)
 <type 'sage.symbolic.expression.Expression'>

 sage: bool(x>1)  # try to evaluate - but hang on, we don't know x!
 False
 sage: bool(1>x)
 False

In other words:
 * Comparison of symbolic expressions by <, > or cmp really does not
   make much sense before inserting values to the variables.
 * Python's max or min functions makes no sense if cmp can't give a
   straight answer.
 * In your integral example, what you really want is that the comparison
   in the max function is delayed until values are inserted for all
   variables. Python's max/min does not delay. Hence, one would need to
   replace it by a function that does delayed evaluation *for symbolic
   expressions only*.
 * The price to pay for replacing Python's min/max is a serious regression
   for everything that is not a symbolic expression. I, for one, would
   not be willing to pay that price. Hence, better have a special
   function (namely max_symbolic) for that special purpose.

But if you use "max" only in integrals, then max's speed on integers
won't matter for you (even though it would matter for others!). Hence,
if you like, you can easily customise it by defining
 max = max_symbolic

Note that max_symbolic appears to work on non-symbolic input:
 sage: max(range(5))
 4

Nevertheless, such customisation is inappropriate for general
applications, because one gets the following regression:

 sage: L = range(100)
 sage: %timeit a=max_symbolic(L)
 625 loops, best of 3: 352 µs per loop
 sage: %timeit a=max(L)
 625 loops, best of 3: 4.03 µs per loop

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To post to this group, send email to sage-support@googlegroups.com.
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support?hl=en.


Reply via email to