TITLE: What is the intuition behind a function being 'weakly differentiable'?
QUESTION [17 upvotes]: As part of an optimization paper I am reading now, they are talking about a function $g$ being "weakly differentiable". 
I looked it up on the wiki but I do not have enough context to start cracking into it. 
I understand what it means for a function to be differentiable, (converges to the same answer if taken in the limit from both sides, IIRC), but what does it mean for a function to be weakly differentiable? What would such a function look like? What would it not look like?
Thanks!

REPLY [29 votes]: The basic intuition is that a weakly differentiable function looks differentiable except for on sets of zero measure. This allows functions that are not normally considered differentiable at "corners" to have a weak derivative that is defined everywhere on the original function's domain. The reason why weak derivatives ignore sets of zero measure is precisely because weak derivatives are defined by integrals, and integrals cannot see behavior on sets of zero measure.
The two examples on Wikipedia are great examples of what weak derivatives can look like:

$f(x) = |x|$. Classically, the derivative of $f$ is only defined when $x \neq 0$. However, the weak derivative ignores the behavior of this corner, and thus any function that equals $-1$ almost everywhere for $x < 0$, and $1$ for $x> 0$ is a weak derivative for $f(x)$. Notice that we can choose any value for the weak derivative at $x = 0$.
$f(x) = 1_{\mathbb{Q}}$, the characteristic function of the rationals (a.k.a. $f(x) = 1$ when $x$ is rational, $f(x) = 0$ when $x$ is irrational). What does this function look like? It is "mostly" zero, since there are uncountably many irrationals and only countably many rationals. Thus, intuitively, $f(x)$ is constant up to a set of zero measure. Notice, however, that normally this function is differentiable nowhere, but if were to just be able to modify this function on a set of rationals, we could make it differentiable. This intuition carries over to the actual weak derivative, which must be a function that is equal to zero almost everywhere.

Now, it is also important to recognize what a weak derivative cannot be, or more precisely, when a function does not have a weak derivative. Intuitively, functions that have discontinuities that cannot be "fixed" by changing the function on a set of zero measure cannot be weakly differentiable. Consider the function
$$ f(x) = \begin{cases} -1 & x \le 0 \\ 1 & x > 0 \end{cases} $$
I claim that $f(x)$ is not weakly differentiable. To prove this, take any $\varphi \in C_c^{\infty}(\mathbb{R})$. Then
$$ \int_{\mathbb{R}} \varphi'(x) f(x) \, dx = \int_{-\infty}^0 - \varphi'(x) \, dx + \int_{0}^{\infty} \varphi'(x) \, dx = -2 \varphi(0)$$
If $f(x)$ has a weak derivative $v(x)$, then it would follows that, for all $\varphi \in C_c^{\infty}(\mathbb{R})$, we would have the identity
$$ \int_{\mathbb{R}} \varphi(x) v(x) \, dx = 2 \varphi(0) $$
Now choose a sequence of compactly supported smooth bump functions $\varphi_n(x)$ such that $0 \le \varphi_n \le 1$, $\varphi(0) = 1$, and $\varphi_n(x) \rightarrow 0$ pointwise for each $x \neq 0$, as $n \rightarrow \infty$. Then
$$ 2 = \lim_{n \rightarrow \infty} \int_{\mathbb{R}} \varphi_n(x) v(x) \, dx = 0,$$
a contradiction.