# How to write a custom function to judge whether a bivariate function is differentiable at a certain point?

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

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.

• It's actually not difficult, but takes some effort. What have you tried so far? Commented Jul 1, 2020 at 8:26
• @SneezeFor16Min I want to know how you would write this universal decision function? Thank you. Commented Jul 1, 2020 at 8:47
• I would call this a bivariate function. When I read binary I thought of a function $f: \mathbb{R} \rightarrow \{0, 1\}$ and was wondering how you define differentiability. Commented Jul 1, 2020 at 11:07
• @Natas Thx, I have updated the question. Commented Jul 1, 2020 at 11:54
• Please check if the theorem is correct. $f(x,y,z)=|x|+|y|+|z|$ is differentiable at $(1,2,3)$, but the limit according to this theorem is $-\infty$. Commented Jul 1, 2020 at 16:00

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

• 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] Commented Jul 1, 2020 at 9:33
• @PleaseCorrectGrammarMistakes: Oh, I'll check it later. Commented Jul 1, 2020 at 11:30
• @PleaseCorrectGrammarMistakes: I've updated, and please try more examples. Commented Jul 1, 2020 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. Commented Jul 1, 2020 at 14:31
• 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. Commented Jul 2, 2020 at 6:04

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  *)

• 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! Commented Jul 2, 2020 at 6:00
• @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. Commented Jul 2, 2020 at 12:15
• 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. Commented Jul 2, 2020 at 12:21
• @MichaelE2 That's great! Maybe this definition is better to operate on. Commented Jul 2, 2020 at 14:26
• @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? Commented Aug 3, 2020 at 12:19

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}.