definite_integral of max function

asked 2022-09-13 17:05:54 +0200

Max Alekseyev gravatar image

The following code returns 1/2, however it can be easily verified that $$\int_0^1 \max(x,\ 1-x)\,{\rm d}x = \frac34.$$ What's wrong?

from sage.symbolic.integration.integral import definite_integral
var('x')
definite_integral(max(x,1-x),x,0,1)
edit retag flag offensive close merge delete

Comments

1

use max_symbolic

FrédéricC gravatar imageFrédéricC ( 2022-09-13 19:11:47 +0200 )edit

Thanks! max_symbolic does lead to the correct answer, but I worry that using max silently produces an incorrect answer - should Sage give an error or a warning about the issue at least?

Max Alekseyev gravatar imageMax Alekseyev ( 2022-09-13 19:57:59 +0200 )edit

This is a common pitfall. You could have tried to see what max(x,1-x) answers.

FrédéricC gravatar imageFrédéricC ( 2022-09-13 20:37:20 +0200 )edit

This is weird - both max(x,1-x) and min(x,1-x) return x. At very least this breaks the identity $\max(x,1-x) + \min(x,1-x) = 1$.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-09-13 20:56:34 +0200 )edit

With symbolic arguments, max and min return the first argument. Hence:

sage: max(x,1-x) + min(x,1-x)                                                   
2*x

However,

sage: max(x,1-x) + min(1-x,x)                                                   
1
Juanjo gravatar imageJuanjo ( 2022-09-14 03:17:22 +0200 )edit

It's good to understand the behavior of max, but still the issue is that it silently produces an incorrect result. If it encounters a problem (such as inability to compute the correct result), it should complain to the user one way or another. The worst thing it can do is to ignore it and return something arbitrary.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-09-14 17:10:29 +0200 )edit
1

max and minare Python functions, and the Sage developers have chosen so far to not redefine them. The issue is documented in https://doc.sagemath.org/html/en/refe..., at least.

John Palmieri gravatar imageJohn Palmieri ( 2022-09-15 00:48:55 +0200 )edit

@FrédéricC : Would you mind making a proper answer for the benefit of future ask.sagemath.org perusers ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-09-17 08:54:01 +0200 )edit