0
$\begingroup$

In 14.0 on Windows 10 let us consider

D[Min[x, y], x]

Piecewise[{{1, x - y <= 0}}, 0]

As I understand it, the result is not true in view of

D[Min[x, y], x] /. y -> x

1

which contradicts

Limit[(Min[x + h, x] - Min[x, x])/h, h -> 0]

Indeterminate

Using PiecewiseExpand does not help here. The documentation to D (see Details and Options and Special Functions/Piecewise and Generalized Functions) doesn't warn that D may produce a generic expression.

Is it a bug or I don't understand something?

$\endgroup$
9
  • $\begingroup$ The downvoter: What is incorrect in my question? $\endgroup$
    – user64494
    Commented Jun 24 at 17:11
  • $\begingroup$ Just to compare. The command of Maple 2024 diff(min(x, y), x) correctly answers $$\begin{array}{cc} 1 & x<y \\ undefined & x=y \\ 0 & y<x \end{array} .$$ $\endgroup$
    – user64494
    Commented Jun 24 at 17:18
  • $\begingroup$ I'm not a downvoter. But my personal belief is: to ask "well-defined or not" is human, not computer. $\endgroup$
    – A. Kato
    Commented Jun 25 at 1:48
  • $\begingroup$ @A.Kato: Compare the results of Mathematica 14 and Maple 2024. $\endgroup$
    – user64494
    Commented Jun 25 at 4:21
  • 3
    $\begingroup$ "Is it a bug or I don't understand something?" -- It's clearly documented in the documentation for Piecewise[] (see @Goofy's answer), and D[] differentiates the piecewise form of Min[]. I don't consider documented behavior a bug. Consider submitting feedback to WRI on how to improve their product. $\endgroup$
    – Michael E2
    Commented Jun 25 at 15:35

5 Answers 5

5
$\begingroup$

It is not clear why sometimes you take a limit and sometimes you just substitute the value. Consider

d[x_, y_] = D[Min[x, y], x]
Limit[d[x, y], y -> x]
(* Indeterminate *)

Edit

@Goofy have already given a good answer, but I think some clarifications are needed, especially in view of the OP comment that words from documentation "...are empty words".

From what I know, not all MA functions are intended for symbolic manipulations. Consider this equivalent definition:

min[x_, y_] := (x - y) UnitStep[y - x] + y

By comparing

Min[x, y] // PiecewiseExpand
min[x, y] // PiecewiseExpand

we see that their piecewise expansions are completely identical. We can analogically define the derivatives:

d1[x_, y_] = D[Min[x, y], x]
d2[x_, y_] = D[min[x, y], x]

They behave differently. As OP noticed d1[x, x] returns 1, and d2 works as expected from mathematical point of view.

d2[x, y] // PiecewiseExpand

$$ d2(x, y)=\left\{\begin{array}{cc} \text{Indeterminate} & x-y=0 \\ 1 & x-y\leq 0 \\ 0 & \text{True} \end{array}\right. $$

d2[x, x]
(*Indeterminate*)

The difference between min and Min is in the definition. I defined min in terms of a UnitStep that permits proper differentiation. But Min is defined in a procedural way, i.e., as an algorithm how to compute a minimum of some values rather than as a mathematical formula (It even works for 0 number of arguments). To my opinion, it is good to have these 2 possibilities. The only problem I see is in the documentation of Min, where some examples of differentiation are given. This is clearly an overstretching of the capabilities of this function.

Finally, I would like to express my humble opinion on how to improve. I feel that MA is lacking a function similar to PiecwiseExpand that would recast an expression in a UnitStep, I would call it UnitStepExpand. This expansion should be used every time mathematical operations on Min and similar functions must be performed.

$\endgroup$
3
  • $\begingroup$ You wrote "It is not clear why sometimes you take a limit and sometimes you just substitute the value".Think of concrete values of x and y. For example, D[Min[x, y], x] /. {x -> 1, y -> 1} results in wrong 1. Don't hesitate to ask for further explanation in need. $\endgroup$
    – user64494
    Commented Jun 24 at 22:58
  • 1
    $\begingroup$ Many thanks from me to you fot the workaround. I accept it. $\endgroup$
    – user64494
    Commented Jun 25 at 11:16
  • 4
    $\begingroup$ "I feel that MA is lacking a function similar to PiecwiseExpand that would recast an expression in a UnitStep, I would call it UnitStepExpand. "There's a handy undocumented Simplify`PWToUnitStep, actually I've defined unitStepExpand using it in a few posts :) : mathematica.stackexchange.com/… $\endgroup$
    – xzczd
    Commented Jun 25 at 12:27
10
$\begingroup$

From the documentation for Piecewise, "Possible Issues":

Derivatives are computed piece-by-piece, unless the function is univariate in a real variable... [Added in V11]

Since this:

Min[x, y] // PiecewiseExpand
(* Piecewise[{{x, x - y <= 0}}, y] *)

hence, I suppose, D[Min[x, y], x] yields this

Piecewise[{{D[x, x], x - y <= 0}}, D[y, x]]
(* Piecewise[{{1, x - y <= 0}}, 0] *)

That is: piece-by-piece differentiation.

Clearly, piece-by-piece differentiation is not guaranteed to be correct on the boundaries. You'll have to ask someone who knows, maybe at WRI, why this choice was made. I'm sure speed has something to do with it.

$\endgroup$
2
  • $\begingroup$ Just to compare, D[RealAbs[x], x] /. x -> 0 results in Indeterminate. Also Maple 2024 produces the correct result of diff(min(x, y), x) (see my comment to my question). $\endgroup$
    – user64494
    Commented Jun 25 at 5:03
  • $\begingroup$ @user64494 Compare this, too: D[RealAbs[x^2], x] /. x -> 0 $\endgroup$
    – Goofy
    Commented Jun 26 at 21:52
4
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

D[Min[x, y], x]

(* Piecewise[{{1, x - y <= 0}}, 0] *)

Using PiecewiseExpand gives the same result

Min[x, y] // PiecewiseExpand

(* Piecewise[{{x, x - y <= 0}}, y] *)

D[%, x]

(* Piecewise[{{1, x - y <= 0}}, 0] *)

The limit definition excludes the case x == y

Assuming[{x, y} ∈ Reals, 
 Limit[(Min[x + h, y] - Min[x, y])/h, h -> 0]]

(* ConditionalExpression[
   Piecewise[{{1, x - y < 0}}, 0], 
   x - y != 0] *)

However, it can be handled separately

Limit[(Min[x + h, x + h] - Min[x, x])/h, h -> 0]

(* 1 *)

EDIT: As with some other functions (e.g., Solve) the canonical order of the variables affects the result. In this case, the potential ambiguity is resolved in favor of the first of the sorted variables.

PiecewiseExpand /@ {Min[x, y], Min[y, x], Min[a, b], Min[b, a]}

enter image description here

$\endgroup$
5
  • $\begingroup$ Thank you for your work. If I correctly understand it, Assuming[{x, y} ∈ Reals in the above is superfluous since Min[x,y] automatically assumes {x,y}\[Element] Reals. The limit Limit[(Min[x + h, y] - Min[x, y])/h, h -> 0] generates a strange result ConditionalExpression[ Piecewise[{{1, x - y < 0}}, 0], !(Element[x | y, Reals] && x - y == 0)]. $\endgroup$
    – user64494
    Commented Jun 24 at 16:57
  • $\begingroup$ If I correctly understand it, Limit[(Min[x + h, x + h] - Min[x, x])/h, h -> 0] does not deal with the partial derivative under consideration al all. $\endgroup$
    – user64494
    Commented Jun 24 at 17:02
  • 1
    $\begingroup$ Limit[(Min[x + h, x + h] - Min[x, x])/h, h -> 0] is only dealing with the specific case that y == x, i.e., the derivative of Min[x, x] $\endgroup$
    – Bob Hanlon
    Commented Jun 24 at 17:08
  • $\begingroup$ FullSimplify[Limit[(Min[x + h, y] - Min[x, y])/h, h -> 0]] results in ConditionalExpression[ Piecewise[{{1, x < y}}, 0], Implies[Element[x | y, Reals], x != y]] and then %/.y->x` performs ConditionalExpression[0, ! (x | x) \[Element] Reals]. This does not answers the situation if x==y being real. $\endgroup$
    – user64494
    Commented Jun 24 at 17:09
  • $\begingroup$ In short words: no code of your answer produces Indeterminate in the case x==y. $\endgroup$
    – user64494
    Commented Jun 24 at 17:38
3
$\begingroup$

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]*)
$\endgroup$
3
  • 2
    $\begingroup$ Sorry, "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" are empty words. Maple 2024 returns the correct result. $\endgroup$
    – user64494
    Commented Jun 25 at 4:28
  • $\begingroup$ @user64494 Unfortunately I don't have maple available, so I tried it on MapleCloud. It did give the correct result for min(x,x), but not for min(x, sin(x-1)). And it can't calculate the derivative of min(x,y,z). Is this also the case on Maple? $\endgroup$
    – houzw
    Commented Jun 25 at 5:00
  • $\begingroup$ The partial derivative of min(x,y,z) with respect to x can be handled in Maple 2024 as follows. (convert(min(x, y, z), piecewise, x) assuming (y <= z));diff(%, x) results in piecewise(x < y, 1, x = y, undefined, y < x, 0) and (convert(min(x, y, z), piecewise, x) assuming (z < y)):diff(%,x) produces piecewise(x < z, 1, x = z, undefined, z < x, 0). Of course, every soft has its limitations. $\endgroup$
    – user64494
    Commented Jun 25 at 5:27
3
$\begingroup$

The case for $x=y$ should be undefined as is evident from:

Plot3D[Min[x, y], {x, -1, 1}, {y, -1, 1}, Mesh -> None]

enter image description here

You can "work around" using alternative representation of Min but Mma still does not express strict inequalities, only 2 cases not 3 cases ($ x<y, x=y, x>y$). I only present as the absolute value representation (to me) makes easier to see issue:

f[x, y] := (x + y - RealAbs[x - y])/2
d[x_, y_] := D[f[u, v], u] /. {u -> x, v -> y}
d[x, y]
d[x, x]
Limit[d[x, y], x -> y]
Reduce[d[x, y] == 1, x]
Reduce[d[x, y] == 0, x]

enter image description here

I will not be responding to comments.

$\endgroup$
1
  • $\begingroup$ +1 for another workaround, @yarchik was first. $\endgroup$
    – user64494
    Commented Jun 25 at 19:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.