1

We know that the necessary and sufficient conditions for a multivariate function to be differentiable at a certain point are complicated:

enter image description here

Suppose the function $z = f (x_ 1, x_ 2, ..., x_n) $ is defined in the neighborhood $U$ of the point $P_ 0 (x_ {10}, x_ {20}, ..., x_{n0}) $. Then the sufficient and necessary conditions for the function $z = f (x_ 1, x_ 2, ..., x_n) $ to be differentiable at the point $P_ 0 (x_ {10}, x_ {20}, ..., x_{n0}) $ are:

The n first-order partial derivatives of the function $z = f (x_ 1, x_ 2, ..., x_n) $ at the point $P_ 0 (x_ {10}, x_ {20}, ..., x_{n0}) $ all exist, and $$f (x_ 1, x_ 2, ..., x_n) - f (x_ {10}, x_ 2, ..., x_n) - f (x_ 1, x_ {20}, ..., x_n) - ... -f (x_ 1, x_ 2, ..., x_{n0}) + f (x_ {10}, x_ {20}, ..., x_{n0}) = o (\rho) $$

where $(x_ 1, x_ 2, ..., x_n) \in U$, $\rho = \sqrt{(x_ 1 - x_ {10})^2 + (x_ 2 - x_ {20})^2 + ... + (x_n - x_ {n0})^2}$.

I already know that the following bivariate function $f(x,y)$ is differentiable at point $(0,0)$, but its two first-order partial derivatives are not continuous at $(0,0)$:

$$f(x, y)=\begin{cases}(x^2 + y^2) \sin(\frac{1}{(x^2 + y^2)}), &(x, y) \neq (0, 0) \cr 0 , &(x, y)=(0, 0)\end{cases} $$

f[x_, y_] := 
 Piecewise[{{(x^2 + y^2) Sin[1/(x^2 + y^2)], x^2 + y^2 != 0}}, 0]
D[f[x, y], x] /. {x -> 0, y -> 0}
D[f[x, y], y] /. {x -> 0, y -> 0}
Limit[(f[x, y] - f[x, 0] - f[0, y] + f[0, 0])/Sqrt[
 x^2 + y^2], {x, y} -> {0, 0}]

I want to write a custom function to judge whether a bivariate function is differentiable at a certain point. How should I write this function?

For example, through this custom function, we'll be able to judge that the following bivariate function is NOT differentiable at $(0,0)$:

$$f(x, y)=\begin{cases}\frac{x^2y}{x^4 + y^2}, &(x, y) \neq (0, 0) \cr 0, &(x, y)=(0, 0)\end{cases} $$

The following bivariate function should be differentiable at point $(0,0)$:

$$f(x, y)=\begin{cases}(x^2 + y^3) \sin(\frac{1}{(x^2 + y^2)}), &(x, y) \neq (0, 0) \cr 0, &(x, y)=(0, 0)\end{cases} $$

f[x_, y_] := 
 Piecewise[{{(x^2 + y^3) Sin[1/Sqrt[x^2 + y^2]], x^2 + y^2 != 0}}, 0]
Limit[(f[0 + Δx, 
   0 + Δy] - (D[f[x, y], x] /. {x -> 0, 
      y -> 0}) Δx - (D[f[x, y], y] /. {x -> 0, 
      y -> 0}) Δy)/Sqrt[Δx^2 + Δy^2], {Δx, Δy} -> {0, 0}]

Correction information:

After careful examination, I found that the theorem in the paper was wrongly written due to the author's negligence. The correct form is as follows:

$$f (x_ 1, x_ 2, ..., x_n) - f (x_ 1, x_ {20}, ..., x_ {n0}) - f (x_ {10}, x_ 2, ..., x_ {n0}) - ... - f (x_ {10}, x_ {20}, ..., x_n) + (n - 1) f (x_ {10}, x_ {20}, ..., x_ {n0}) = o (\rho)$$

where n is the number of variables of this multivariate function.

3 Answers3

4

I'm quite interested in this problem so took some efforts to write it. Since you've given the conditions for multivariate functions, I wrote a more general version than just for bivariate functions.

ClearAll[differentiableAtQ];
differentiableAtQ[
  f_, p_?VectorQ, vars_?VectorQ, dom_ : Reals
  ] := With[{n = Length[vars], dimP = Length[p]},
  If[n < 1 || n != dimP, Return[]];
  If[n > 1,
   With[{pd = D[f, #] & /@ vars},
    With[{pdValues = ((Evaluate[vars] \[Function] #) @@ p) & /@ pd},
     (* All partial derivatives exist *)
     AllTrue[pdValues, NumericQ] &&
      With[{$f = Evaluate[vars] \[Function] Evaluate[f]},
       (* All partial derivatives are continuous *)
       AllTrue[{pd, pdValues}\[Transpose],
         Apply[Limit[#1, vars -> p] === #2 &]
         ] || Switch[ (* Taking limit *)
         Limit[FullSimplify[
           (If[MemberQ[#, _Piecewise, \[Infinity]],
               # // PiecewiseExpand, #] &)[
            (* Edit for correction (n-1) *)
            ($f @@ vars + (n - 1) $f @@ p
               - Total[
                $f @@@ (ConstantArray[vars, n]
                   + DiagonalMatrix[p - vars])
                ])/Norm[vars - p]],
           And @@ Thread[vars != p]
            && vars \[Element] dom],
          vars -> p],
         0, True,
         Indeterminate, False,
         _DirectedInfinity, False,
         _, Indeterminate
         ]
       ]]],
   D[f, vars] /. vars[[1]] -> p[[1]] // NumericQ
   ]]

Test

Examples in your question:

differentiableAtQ[
 Piecewise[
  {{0, {x, y} == {0, 0}}},
  (x^2 + y^2) Sin[1/(x^2 + y^2)]
  ], {0, 0}, {x, y}]
True
differentiableAtQ[
 Piecewise[
  {{0,
      {x, y} == {0, 0}}},
  (x^2 y)/(x^4 + y^2)],
 {0, 0}, {x, y}]
False

Example in comment:

differentiableAtQ[
 Piecewise[
  {{(x^2 + y^3) Sin[1/Sqrt[x^2 + y^2]], x^2 + y^2 != 0}},
  0],
 {0, 0}, {x, y}
 ]
True
differentiableAtQ[
 Piecewise[{
   {0,
       {x, y} == {0, 0}},
   {(x^2 + y^2),
       y < 0}},
  (x^2 + y^2) Sin[1/(x^2 + y^2)]],
 {0, 0}, {x, y}]
Indeterminate

Univariate:

differentiableAtQ[RealAbs[x], {0}, {x}]
False
differentiableAtQ[RealAbs[x], {1}, {x}]
True

Bivariate:

differentiableAtQ[RealAbs[x] + RealAbs[y], {1, 1}, {x, y}]
True
differentiableAtQ[RealAbs[x] + RealAbs[y], {0, 1}, {x, y}]
False

Trivariate:

differentiableAtQ[
 RealAbs[x] + RealAbs[y] + RealAbs[z],
 {1, 1, 1}, {x, y, z}]
True
differentiableAtQ[
 RealAbs[x] + RealAbs[y] + RealAbs[z],
 {1, 1, 0}, {x, y, z}]
False
SnzFor16Min
  • 2,230
  • 7
  • 19
  • I found that your custom function should be wrong in judging whether this binary function is differentiable at {0,0}: f[x_, y_] := Piecewise[{{(x^2 + y^3) Sin[1/Sqrt[x^2 + y^2]], x^2 + y^2 != 0}}, 0] – A little mouse on the pampas Jul 01 '20 at 09:33
  • 1
    @PleaseCorrectGrammarMistakes: Oh, I'll check it later. – SnzFor16Min Jul 01 '20 at 11:30
  • @PleaseCorrectGrammarMistakes: I've updated, and please try more examples. – SnzFor16Min Jul 01 '20 at 12:35
  • Hi, it seems to fail on this: Piecewise[{{0, {x, y} == {0, 0}}, {(x^2 + y^2), y < 0}}, (x^2 + y^2) Sin[1/(x^2 + y^2)]] at {0, 0}, if you're applying the $(F - dF.\Delta x)/\Vert \Delta x \Vert \rightarrow 0$ definition. – Michael E2 Jul 01 '20 at 14:31
  • @MichaelE2: The problem is taking limit on a piecewise function. I'll mark it as Indeterminate. – SnzFor16Min Jul 01 '20 at 16:23
  • @MichaelE2: It would be better if you can post formula or Mathematica code (like attach it to my answer) to show it's (non-)differentiable so that improvement may be made. – SnzFor16Min Jul 01 '20 at 16:29
  • OK, done. See my answer. Limit was a bit unpredictable when Piecewise was involved, so I hope I didn't overlook something. – Michael E2 Jul 01 '20 at 21:11
  • @SneezeFor16Min I'm sorry. I found that the theorem in the paper is not correct. I've corrected the errors, but I'm not sure whether my corrections are completely correct. – A little mouse on the pampas Jul 02 '20 at 02:43
  • 1
    PleaseCorrectGrammarMistakes: I've modified my code according to your correction. Yesterday I used an alternative way, the continuity of partial derivatives, to determine the differentiability of $|x|+|y|+|z|$, but it's still not enough to determine the example given by @MichaelE2. – SnzFor16Min Jul 02 '20 at 06:04
3

If Limit were infallible, then the following would do it:

differentiableQ[f_, spec : (v_ -> v0_)] := With[{jac = D[f, {v}]},
   Module[{f0, jac0},
     {f0, jac0} = {f, jac} /. Thread[spec];
     VectorQ[Flatten@{f0, jac0}, NumericQ] &&
       Limit[(f - f0 - jac0.(v - v0))/Sqrt@Total[(v - v0)^2], spec] === 0 
     ] /; VectorQ[jac]
   ];

But Limit is not infallible, so it might pay to work around its limitations. In particular, it is not yet robust on Piecewise functions, which is of particular interest to the OP.

We can add a step to the above to try harder when Limit fails and a Piecewise function is present.

ClearAll[differentiableQ, dLimit];
differentiableQ[f_, spec : (v_ -> v0_)] := With[{jac = D[f, {v}]},
   Module[{f0, jac0, res},
     {f0, jac0} = {f, jac} /. Thread[spec];
     If[VectorQ[Flatten@{f0, jac0}, NumericQ],
      res = 
       Limit[(f - f0 - jac0.(v - v0))/Sqrt@Total[(v - v0)^2], spec] /.
          HoldPattern[Limit[df_, s_]] /; ! FreeQ[df, Piecewise] :> 
         With[{L = dLimit[df, s]}, L /; FreeQ[L, dLimit]];
      res = FreeQ[res, Indeterminate] &&
         And @@ Thread[Flatten@{res} == 0],
      res = False
      ]] /; VectorQ[jac]
   ];
dLimit[df_, spec_] := Module[{f0, jac0, pcs = {}, z, res},
   pcs = Replace[
     (* Solve[.., Reals] separates PW fn *)
     z /. Solve[z == df, z, Reals],
     {ConditionalExpression[y_, c_] :> {y, c}, y_ :> {y, True}},
     1];
   If[ListQ[pcs],
    res = (Limit[Piecewise[{#}], spec] /.
         HoldPattern[Limit[Piecewise[{{y_, _}}, 0], s_]] :> 
          With[{L = Limit[y, s]}, L /; FreeQ[L, Limit]]
        & /@ pcs)
    ];
   res /; ListQ[pcs]
   ];

Examples:

differentiableQ[
 Piecewise[{{(x^2 + y^2) Sin[1/(x^2 + y^2)], {x, y} != {0, 0}}}],
  {x, y} -> {0, 0}]
(*  True  *)
differentiableQ[
 Piecewise[{{0, {x, y} == {0, 0}},
   {(x^2 + y^2), y < 0}}, (x^2 + y^2) Sin[1/(x^2 + y^2)]],
  {x, y} -> {0, 0}]
(*  True  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • What definition do you use? Because I see the jac0.(v - v0). BTW, I like your spec : (v_ -> v0_) -- I should've written like this! – SnzFor16Min Jul 02 '20 at 06:00
  • @Michael E2 Thank you very much for your help. – A little mouse on the pampas Jul 02 '20 at 07:56
  • 1
    @SneezeFor16Min The limit of $[,f(x) - (,f(x_0) + \nabla f(x_0)\cdot(x-x_0),),]/\Vert x-x_0 \Vert$ should be zero at $x\rightarrow x_0$. I didn't understand the (original) statement in the quoted theorem at first, as it seemed ("We know...") a botched attempt at the usual definition and was in Chinese. So I took a stab at the question in the title. One might be able to apply the dLimit idea to limits of piecewise functions to the desired approach in the theorem. Conceivably PiecewiseExpand might be needed to help Solve. – Michael E2 Jul 02 '20 at 12:15
  • 1
    I'd also add that the advantage of the theorem seems to be, *if* you can assume the partial derivatives exist, then the criterion does not require computing them. – Michael E2 Jul 02 '20 at 12:21
  • 1
    @MichaelE2 That's great! Maybe this definition is better to operate on. – SnzFor16Min Jul 02 '20 at 14:26
  • Unfortunately, differentiableQ[ Piecewise[{{(x^2 + y^2) Sin[1/(x^2 + y^2)^3], {x, y} != {0, 0}}}], {x, y} -> {0, 0}] performs True which is wrong in view of D[r^2*Sin[r^-6], r]. – user64494 Jul 28 '20 at 06:55
  • @user64494 Your example is differentiable at the origin, so True is the result I would want. Piecewise[{{r^2*Sin[r^-6], r != 0}}] is also differentiable at r = 0: apply the limit definition. D[r^2*Sin[r^-6], r] is valid only for r != 0 and does not apply to the example. – Michael E2 Jul 28 '20 at 10:57
  • @MichaelE2 Your answer is great, but when dealing with these examples of unit functions, your results are not correct:differentiableQ[RealAbs[x] Sin[RealAbs[x]], {x} -> {0}] differentiableQ[Cos[Sqrt[RealAbs[x]]], {x} -> {0}] or here. – A little mouse on the pampas Aug 03 '20 at 07:29
  • 1
    @Ordinaryusers68 It fails on the first because the built-in D fails on it. Note sure if that's worth addressing. I guess I could throw in a PiecewiseExpand: differentiableQ[ PiecewiseExpand[RealAbs[x] Sin[RealAbs[x]]], {x} -> {0}]. I get the correct answer for Cos[Sqrt[RealAbs[x]]], i.e., False. Did you get True? – Michael E2 Aug 03 '20 at 12:19
  • Note in my previous comment, I said, "if you can assume the partial derivatives exist": I would add, if you can assume D[] computes them correctly. – Michael E2 Aug 03 '20 at 19:12
  • @MichaelE2 Thank you very much. After adding PiecewiseExpand, I did get the right result. – A little mouse on the pampas Aug 03 '20 at 22:08
0

As Michael E2 said, it is not always reliable to use the Limit function to find the limit. In addition, I found that the definition in the paper should be wrong. For example, the result of the following example is not correct:

f[x_, y_, z_] := (RealAbs[x] + RealAbs[y] + RealAbs[z])
Limit[(f[x, y, z] - f[1, y, z] - f[x, 2, z] - f[x, y, 3] + 
  f[1, 2, 3])/Sqrt[(x - 1)^2 + (y - 2)^2 + (z - 3)^2], {x, y, 
   z} -> {1, 2, 3}]
(*According to the theorem mentioned in the question description*)
(*-∞*)
Limit[(f[0 + Δx, 0 + Δy, 
   0 + Δz] - Δx - Δy - \
Δz)/Sqrt[Δx^2 + Δy^2 + Δz^2], {Δx, Δy, \
Δz} -> {1, 2, 
   3}](*According to the differentiable definition of multivariate \
function*)
(*0*)

We can find that the results of the two methods are not the same, but in the following simplified example, the results are consistent:

g[x_, y_] := (RealAbs[x] + RealAbs[y])
Limit[(g[x, y] - g[1, y] - g[x, 2] + 
  g[1, 2])/Sqrt[(x - 1)^2 + (y - 2)^2], {x, y} -> {1, 2}]
(*According to the theorem mentioned in the question description*)
(*0*)
Limit[(g[0 + Δx, 
   0 + Δy] - Δx - Δy)/Sqrt[\
Δx^2 + Δy^2], {Δx, Δy} -> {1, 2}]
(*According to the differentiable definition of multivariate function*)

(0)

After careful examination, I found that some details of the theorem in the paper are wrong, which should be written in the following way:

f[x_, y_, z_] := (RealAbs[x] + RealAbs[y] + RealAbs[z])
Limit[1/(Sqrt[(x - 1)^2 + (y - 2)^2 + (z - 3)^2])*((f[x, y, z] - 
     f[1, 2, 3])(*Δz*)- ((f[x, 2, 3] - 
       f[1, 2, 3])(*fx*Δx*)+ (f[1, y, 3] - 
       f[1, 2, 3])(*fy*Δy*)+ (f[1, 2, z] - 
       f[1, 2, 3])(*fz*Δz*))), {x, y, z} -> {1, 2, 3}]

It can also be abbreviated to the following format:

Limit[(f[x, y, z] - f[x, 2, 3] - f[1, y, 3] - f[1, 2, z] + 
  (3-1)* f[1, 2, 3])/Sqrt[(x - 1)^2 + (y - 2)^2 + (z - 3)^2], {x, y, 
   z} -> {1, 2, 3}]

The result of this limit is 0, which is consistent with the result of derivation by definition, that is, the function $f(x)=\lvert x\rvert+\lvert y\rvert+\lvert z\rvert$ is differentiable at point {1,2,3}.