TITLE: Prove that $f:\mathbb R^n\to\mathbb R$ is affine if and only if it is convex and concave
QUESTION [12 upvotes]: Suppose $f:\mathbb{R}^n\to \mathbb{R}$ is both convex and concave, how to prove that $f$ is linear? or exactly speaking, $f$ is affine?
I thought for the whole day, but I cannot figure it out. 
When I was working on this problem, I met another problem, are all the convex function continuous? If not, is there any counter example?
Actually, I can prove for one dimensional case, in which $f:\mathbb{R}\to \mathbb{R}$. However, I cannot generalize it into n dimensional cases.
By the way, I use definition for convex(concave) like this:
$$f(t\vec{x}+(1-t)\vec{y})\leq(or \geq) tf(\vec{x})+(1-t)f(\vec{y}), \forall t\in[0,1].$$
Thank you so much!

REPLY [2 votes]: I think it becomes more clear when we simply apply the definition of convexity and concavity.
From the convexity of $f$ we have for $x_1, x_2\in \mathbb{R^n}$ and $\lambda \in [0,1]$$$f((1-\lambda)x_1 + \lambda x_2) \le (1-
\lambda)f(x_1)+\lambda f(x_2)$$
and similarly for concavity we have $$f((1-\lambda)x_1 + \lambda x_2) \ge (1-
\lambda)f(x_1)+\lambda f(x_2)$$
Thus it follows that $$f((1-\lambda)x_1 + \lambda x_2) = (1-
\lambda)f(x_1)+\lambda f(x_2)$$
We often think of $(1-\lambda)x_1 + \lambda x_2$ as a point on the line segment between $x_1$ and $x_2$. Similarly from above we can see that $f((1-\lambda)x_1 + \lambda x_2)$ lies on the line segment  between $f(x_1)$ and $f(x_2)$. Since we have chosen $x_1$ and $x_2$ arbitrarily it starts to become apparent why $f$ is affine. If we recall, an affine function takes the form $ax+b$ where $a \in \mathbb{R^n}$ and $ b \in \mathbb{R}$ which is a line.