Continuous linear extension Suppose to have a continuous function $f:[a_1,b_1]\to\mathbb{R}$ and let $c_1<a_1$ and $d_1>b_1$. Suppose that you want to extend $f$ to all $\mathbb{R}$ to a new function $g:\mathbb{R}\to\mathbb{R}$ with the following properties:

*

*$g$ has to be continuous

*$g\equiv f$ on $[a_1,b_1]$

*$g$ is constant $(g\equiv M)$ outside $[c_1,d_1]$

*$g$ is linear on $[c_1,a_1]$ and $[b_1,d_1]$.

This can be easily done by setting:
$$
g(x)=
\begin{cases}
M &\text{ if }x\leq c_1\\
M-\frac{c_1-x}{c_1-a_1} [M-f(a_1)]&\text{ if }c_1\leq x\leq a_1\\
f(x)&\text{ if }a_1\leq x\leq b_1\\
f(b_1)+\frac{x-b_1}{d_1-b_1} [M-f(b_1)]&\text{ if }b_1\leq x\leq d_1\\
M &\text{ if }x\geq d_1.\\
\end{cases}
$$
My question is the following.
How to generalize this result to a multidimensional setting? Precisely:
Suppose to have a continuous function $f:[a_1,b_1]\times\dots\times[a_n,b_n]\to\mathbb{R}$ and let $c_j<a_j$ and $d_j>b_j$ for every $j\in\{1,\dots,n\}$. Suppose that you want to extend $f$ to all $\mathbb{R}$ to a new function $g:\mathbb{R}^n\to\mathbb{R}$ with the following properties:

*

*$g$ has to be continuous

*$g\equiv f$ on $[a_1,b_1]\times\dots\times[a_n,b_n]$

*$g$ is constant $(g\equiv M)$ outside $[c_1,d_1]\times\dots\times[c_n,d_n]$

*$g$ is linear in each variable in between the two parallelepipeds.

I need an explicit formula like the one above. I tried an approach "componentwise" but I have problems in proving continuity and with the overlapping zones so generated.
Any hint will be greatly appreciated.
 A: This isn't generically possible. The problem is that the "boundary region" in 1D is not connected, but it is in higher dimensions. This conflicts with the requirement that "boundary function" needs to be linear.
For a counterexample, consider $f(x) \equiv 1$ on the point $(0, 0)$. Define $M = 0$. Call the larger rectangle $[-1, 1] \times [-1, 1]$. Then you are looking for a linear function $g$ that is $0$ at both $(-1, 0)$ and $(1, 0)$, but which is $1$ at $(0, 0)$, which is clearly not possible.
Slightly more generally, suppose $f \equiv 1$ on $[-1/2, 1/2] \times [-1/2, 1/2]$, $M= 0$, and the larger rectangle remains $[-1, 1] \times [-1, 1]$. The region inside the unit square and outside the $[-1/2, 1/2] \times [-1/2, 1/2]$ square is connected, and thus a linear function $g$ will have identical derivative $\partial_x g$ everywhere in this region. This is incompatible with requiring $g(-1, 0) = 0 = g(1, 0)$ and $g(-1/2, 0) = 1 = g(1/2, 0)$.
You will need to weaken the requirements you place on your function to allow such functions to exist.
