A problem here is that we cannot know the a priori relationship between x and y, which means
D[Min[x, x], x]
should return 1
instead of Indeterminate
.
And
D[Min[x, 1], x]
will give
Piecewise[{{1, x < 1}, {0, x > 1}}, Indeterminate]
correctly.
One solution is to define your own derivative.
Clear[min]
Unprotect[Derivative];
Derivative[0, 1][min][x_, y_] =
Piecewise[{{1, x > y}, {0, x < y}}, Indeterminate];
Derivative[1, 0][min][y_, x_] =
Piecewise[{{1, x > y}, {0, x < y}}, Indeterminate];
Protect[Derivative];
D[min[x, y], y]
(*Piecewise[{{1, x > y}, {0, x < y}}, Indeterminate]*)
However, you cannot set min
to Min
.
Another solution is to restrict the parameters to real numbers.
Clear[min]
min[x_Reals, y_Reals] := Min[x, y]
D[min[x, y], x]
(*Piecewise[{{1, y > x}, {0, y < x}}, Indeterminate]*)
But its disadvantage is that min
cannot be calculated symbolically normally.
Or you can refer to the answer in Derivatives of piecewise functions of functions. For example
Clear[min]
min[x_, y_] :=
Module[{h, g}, Limit[Min[x + h, y], h -> 0, Direction -> Reals]]
D[min[x, y], x] //
Simplify[#, Assumptions -> {x \[Element] Reals, y \[Element] Reals}] &
(*ConditionalExpression[Piecewise[{{1, x < y}}, 0], x != y]*)
diff(min(x, y), x)
correctly answers $$\begin{array}{cc} 1 & x<y \\ undefined & x=y \\ 0 & y<x \end{array} .$$ $\endgroup$Piecewise[]
(see @Goofy's answer), andD[]
differentiates the piecewise form ofMin[]
. I don't consider documented behavior a bug. Consider submitting feedback to WRI on how to improve their product. $\endgroup$