Proves with $K\subset L^{2}(\mathbb{R})$ Define a subset $K \subset L^{2}(\mathbb{R})$ as
$K=\{f:\mathbb{R}\to \mathbb{C}|f \in L^{2}(\mathbb{R})$ and $f(x)\in \mathbb{R}$ for almost every $x\in \mathbb{R} \}$

*

*if $f\in L^{2}(\mathbb{R})$, prove that $f \in K$ if and only if $\langle f,g\rangle \in \mathbb{R}$ for every $g \in K$.

My prove:
First we assume that $f \in K$. Take $g \in K$random. Then \begin{align}
\langle f,g\rangle =\int_{\mathbb{R}}f(x)\overline{g(x)}d\lambda(x)
\end{align}
Because $g\in K$ is $g(x) \in \mathbb{R}$ for almost every $x \in \mathbb{R}$.
This means that $\lambda(\{x \in \mathbb{R}|g(x) \notin \mathbb{R})\}=0$. Because this is $0$ we can say that \begin{align}
\langle f,g\rangle &=\int_{\mathbb{R}}f(x)\overline{g(x)}d\lambda(x)\\
&=\int_{\mathbb{R}}f(x)g(x)d\lambda(x)\\
&=\int_{\mathbb{R}}\overline{f(x)g(x)}d\lambda(x)\\
&=\overline{\int_{\mathbb{R}}f(x)g(x)d\lambda(x)}\\
&=\overline{<f,g>}
\end{align}
This means $\langle f,g\rangle \in \mathbb{R}$
The other way is analogous.


*Prove that $K$ is a closed and convexed subset of $L^{2}(\mathbb{R})$
This is a difficult part for me. I already tried to prove it's closed and convex but I got stuck.
My attempt:
Proving $K$ is closed means proving $K^{c}$ is open. So we define $K^{c}=\{f:\mathbb{R}\to \mathbb{C}|f\in L^{2}(\mathbb{R})$ and $f(x) \notin \mathbb{R}$ for almost every $ x\in \mathbb{R}\}$.
Take $h \in K^{c}$. Now we look for an $\epsilon >0$. take $g\in L^{2}(\mathbb{R})$ random. Then $g$ is measurable and $g=0$ almost everywhere but $<g,g> \neq 0$.
\begin{align} 
\lVert h-g\rVert &=\langle h-g,h-g\rangle\\
&=\int_{\mathbb{R}}(h(x)-g(x))\overline{(h(x)-g(x))} d\lambda(x)\\
&=...
\end{align}
I got stuck form here so if someone could help me to finish it?


*if $f \in L^{2}(\mathbb{R})$, witch element of $K$ lies the closet with $f$? Wht is the distance from $f$ to that element

I think you need to find the orthonormal projection on $K$. But I can't find the point that we're looking for.
 A: For the "closed"-part: For $g\in L^2(\Bbb R)$ define the bounded linear functional $F_g:L^2(\Bbb R)\to \mathbb{C}, F_g(f)=\langle f,g\rangle$. By the first part we have the equality $$K=\bigcap_{g\in K}F_g^{-1}(\Bbb R)$$
Why is this closed?
For the closest element: If you take any complex number $z$ the closest real number to that is $\operatorname{Re}z$, this suggests to take $\tilde{f}=\operatorname{Re}f\in K$ for $f\in L^2(\Bbb R)$. Now you just need to prove that for all $g\in K$ we have $$\left\lVert g-f\right\rVert\geq \left\lVert \tilde{f}-f\right\rVert$$
Edit: (for the last part) If we have any complex number $z$ and any real number $x$ we have $$\left\vert x-z\right\vert\geq\left\vert \operatorname{Re}z-z\right\vert$$
(Maybe draw a picture)
So if we take any $g\in K$ we get $$\left\vert g(x)-f(x)\right\vert\geq \left\vert (\operatorname{Re}f(x))-f(x)\right\vert$$
for almost every $x\in \Bbb R$. Now square and integrate.
Edit 2:
$$\left\lVert g-f\right\rVert^2=\int_{\Bbb R}\left\vert g(x)-f(x)\right\vert^2dx\geq\int_{\Bbb R}\left\vert (\operatorname{Re}f(x))-f(x)\right\vert^2dx=\left\lVert (\operatorname{Re}f)-f\right\rVert^2$$
