What are the conditions on $f$ for $\sup f$ and $f(\sup)$ be interchangeable? Let $f:\mathbb{R}^n \to \mathbb{R}$. Say that $f$ is magic if for every nonempty set $X$ and every functions $g_1,\dots,g_n : X \to \mathbb{R}$ the following holds:
$$\sup_{(x_1,\dots,x_n) \in X^n} f(g_1(x_1),\dots,g_n(x_n)) = f \left(\sup_{x \in X} g_1(x),\dots,\sup_{x \in X} g_n(x) \right)$$
I am looking for a characterization of magicness. In other words, I am looking for necessary and sufficient conditions for magicness, a theorem that looks like:

$f$ is magic if and only if [some conditions here]

Intuitively, it looks like being a linear operator with non-negative coefficients* is sufficient, but (1) I could be wrong and (2) it seems too strong to be a necessary condition anyway.

* By linear operator with non-negative coefficients I mean that $f$ has the form $f(x_1, \dots, x_n) = a_1 x_1 + \dots + a_n x_n$ where every $a_i$ is non-negative.
 A: Here is my attempt. First of all, each $g_i$ must have bounded image, otherwise, the identity that defines "magicness", wouldn't be defined, for we would have to calculate $f$ at points in $\overline{\mathbb R}^n$. So we implicitly assume that.

$f$ is "magic" if, and only if, the functions of the form $f(a_1,...,a_{i-1},\cdot,a_{i+1},...,a_n)$ are increasing and $f$ satisfies the following weak condition on continuity:
  $$
f(x_1^j,...,x_n^j) \to f(x_1,... x_n),
$$
  for any $(x_1^j,...,x_n^j) \nearrow (x_1,... x_n)$, where "$\nearrow$" means that $(x_1^j,...,x_n^j) \to (x_1,... x_n)$ and $(x_i^j)_{j\in\mathbb N}$ is increasing for any $i\in\{1,...,n\}$.

($\Rightarrow$) Suppose that $b>a$ and consider $X=[a,b]$, $g_j$ defined on $X$ such that $g_j\equiv a_j$ for each $j\neq i$ and $g_i=Id|_X$. Then it follows by the magicness that 
$$\sup_{x\in[a,b]}f(a_1,...,a_{i-1},x,a_{i+1},...,a_n) =f(a_1,...,a_{i-1},b,a_{i+1},...,a_n) $$ and, in particular,
$$
f(a_1,...,a_{i-1},b,a_{i+1},...,a_n)\geq f(a_1,...,a_{i-1},a,a_{i+1},...,a_n),
$$
so it follows that $f(a_1,...,a_{i-1},\cdot,a_{i+1},...,a_n)$ is increasing.
To see the weak condition on continuity, fix $(x_1^j,...,x_n^j) \nearrow (x_1,... x_n)$. Let $X=\left\{1-\dfrac{1}{j}: j\in\mathbb N\right\}$ and, for each $i\in\{1,...,n\}$, define 
$g_i: X\to \mathbb R$ such that $g_i\left(1-\dfrac{1}{j}\right)=x_i^j$. Since each $(x_i^j)_{j\in\mathbb N}$ is increasing, we have that $\displaystyle\sup_{x\in X} g_i(x)=x_i$ and consequently, by magicness,
$$
\sup_{j\in\mathbb N} f(x_1^j,...,x_n^j) = \sup_{(x_1,...,x_n)\in X^n} f(g_1(x_1),g_2(x_2),...,g_n(x_n)) = f(x_1,... x_n).
$$
But note the following:

Claim. Suppose that all the functions of the form $f(a_1,...,a_{i-1},\cdot,a_{i+1},...,a_n)$ are increasing. Suppose that $(x_1,...,x_n),(y_1,...,y_n)$ satisfy $x_i\leq y_i$ for all $1\leq i \leq n$. Then, we have that
  $$
f(x_1,x_2,...,x_n)\leq f(x_1,...,x_{n-1},y_n) \leq ... \leq f(x_1, y_2,...,y_n) \leq f(y_1,y_2,...,y_n).
$$
  We will call this the "increasing property".

Then, by the increasing property, we have that $(f(x_1^j,...,x_n^j))_{j\in\mathbb N}$ is increasing, so
$$
\lim_{j\in\mathbb N}f(x_1^j,...,x_n^j)=\sup_{j\in\mathbb N} f(x_1^j,...,x_n^j) = f(x_1,... x_n).
$$
($\Leftarrow$) Pick any functions $g_1,..., g_n: X\to \mathbb R$ and fix $\displaystyle \alpha_i=\sup_{x\in X} g_i(x)\in\mathbb R$, it follows that, for any $x\in X$, $g_i(x)\leq \alpha_i$, so, by the increasing property,
$$
f(g_1(x_1),g_2(x_2),...,g_n(x_n))\leq f(\alpha_1,\alpha_2,...,\alpha_n),\ \forall (x_1,...,x_n)\in\mathbb R^n.
$$
so
$$
\sup_{(x_1,...,x_n)\in X^n} f(g_1(x_1),g_2(x_2),...,g_n(x_n)) \leq f(\alpha_1,\alpha_2,...,\alpha_n).
$$
It lasts to prove the reverse inequality. For each $i\in\{1,...,n\}$, fix $(x^i_j)_{j\in\mathbb N}$ such that $g(x^i_j)\nearrow \alpha_i$, then, $(g(x^1_j),...,g(x^n_j))\nearrow (\alpha_1,...,\alpha_n)$ and by the weak condition on continuity, we have
$$
f(\alpha_1,\alpha_2,...,\alpha_n)=\lim_{j\in\mathbb N} f(g(x^1_j),...,g(x^n_j))\leq\sup_{(x_1,...,x_n)\in X^n} f(g_1(x_1),g_2(x_2),...,g_n(x_n)).
$$
In particular, if each function of the form $f(a_1,...,a_{i-1},\cdot,a_{i+1},...,a_n)$ is increasing and $f$ is continuous, then $f$ is "magic". We list some examples of "magic" functions below (each case is simple to verify):
1) $f:\mathbb R^n \to \mathbb R$ is an affine function $f(x_1,...,x_n)= a + a_1x_1 + ... + a_nx_n$, with $a_i\geq0$. It includes the linear functions that you mentioned, the identity and the constant functions;
2) $f(x_1,...,x_n)=f_1(x_1)...f_n(x_n)$, where each $f_i:\mathbb R \to \mathbb R$ is increasing and lower semicontinuous;
3) Sum and product of magic functions and product by a positive scalar;
4) Given a magic function $f$ and a continuous function $g$, the function $g\circ f$ is magic.
