Let $(X, \mu)$ be your favourite measure space (finite or $\sigma$-finite if you like), let $g \in L^2$ (say, the scalar field of $L^2$ is $\mathbb{R}$, though this probably doesn't matter). Let $\tilde g: X \to \mathbb{R}$ be a measurable function and assume that there exists a norm dense vector subspace $D$ of $L^2$ with the following property:


For every $f \in D$ the function $\tilde g f$ is integrable, and we have $\int \tilde g f \, d\mu = \int g f \, d\mu$.


**Question.** Does it follow that $\tilde g = g$ almost everywhere?


*Remarks.*


* Under the given assumptions, the propertiy $\tilde g = g$ almost everyhwere is equivalent to $\tilde g \in L^2$. For if $\tilde g \in L^2$, then the integral equality in the assumption extends to all $f \in L^2$ by density, and for $f := \tilde g - g$ we thus obtain that $\lVert \tilde g - g\rVert^2 = \int (\tilde g - g) f \, d\mu = 0$.


*In other words, the question asks whether it is impossible to represent, on any dense subspace of $L^2$, a continuous linear functional on $L^2$ by a non-$L^2$-function.*
* If $D$ is a *lattice ideal* in $L^2$, meaning that $f\_1 \in D$ whenever $\lvert f\_1 \rvert \le \lvert f\_2 \rvert$ for some $f\_2 \in D$, then the answer to the question is **yes**.


*Proof of the claim in the second bullet point.*
Assume that $D$ is a lattice ideal, and let $f \in D$. There exists a measurable function $s: X \to \mathbb{R}$ of modulus $\lvert s \rvert = 1$ such that $\tilde g f s \ge 0$. Note that this implies $\lvert \tilde g f \rvert = \tilde g f s$. As $\lvert s f \rvert = \lvert f \rvert$ we have $sf \in D$, so it follows that
$$
 \label{1}\tag{$\*$}
 \int \lvert \tilde g f \rvert \, d\mu 
 = 
 \int \tilde g f s \, d\mu 
 = 
 \int g f s \, d\mu 
 \le 
 \lVert f \rVert \lVert g \rVert.
$$
Now, consider a function $0 \le h \in L^2$. By density, there exists a sequence $(f\_n)$ in $D$ which converges to $h$. By replacing each $f\_n$ with $(h \land f\_n) \lor 0$ (where $\land$ denotes the pointwise minimum and $\lor$ denotes the pointwise maximum of functions) we may assume that $0 \le f\_n \le h$ for each $n$. Moreover, by then replacing each $f\_n$ with the pointwise maximum of the functions $f\_1, \dots, f\_n$, we may also assume that the sequence $(f\_n)$ is increasing. Hence, it follows from the monotone convergence theorem and from \eqref{1} that
$$
 \int \lvert \tilde g h \rvert \, d\mu 
 = 
 \lim\_{n \to \infty} \int \lvert \tilde g \rvert f\_n \, d \mu
 \le 
 \lim\_{n \to \infty} \lVert f\_n \rVert \lVert g \rVert
 = 
 \lVert h \rVert \lVert g \rVert.
$$
For a general (i.e., not necessarily positive) function $h \in L^2$ we can apply the estimate that we just proved to $\lvert h \rvert$ and thus obtain the same estimate
$$
 \int \lvert \tilde g h \rvert \, d\mu 
 \le 
 \lVert h \rVert \lVert g \rVert
$$
for even all $h \in L^2$. But this shows that $\tilde g\in L^2$, so $\tilde g = g$ almost everyhwere due to the remark in the first bullet point. $\square$



Representing an $L^2$-functional by a non-$L^2$-function on a dense subspace
The answer is **no** and the following result provides a quite interesting counterexample. This is a known result, but I am not sure where to find it in the literature.



> 
> **Theorem.** If $f\in L^1\_{\rm loc}(\mathbb{R}^n)\setminus L^2(\mathbb{R}^n)$, then there is an orthonormal basis
> $\{\varphi\_k\}\_{k=1}^\infty$ of $L^2(\mathbb{R}^n)$ consisting of compactly supported smooth functions, such that
> $$
> \int\_{\mathbb{R}^n}f(x)\varphi\_n(x)\, dx=0
> \quad
> \text{for all $n=1,2,3\ldots$}
> \tag1
> $$
> 
> 
> 


Indeed, if $D=\operatorname{span}\{\varphi\_k\}$, then
$$
\int\_{\mathbb{R}^n} f\varphi = \int\_{\mathbb{R}^n} 0\cdot\varphi,
\quad
\forall \varphi\in D.
$$


**Proof.** Let $\mathbb{K}=\mathbb{R}$ or $\mathbb{C}$ be the given field.
In the proof we will need the following result.



> 
> **Lemma.**
> Let $L:X\to\mathbb{K}$ be a discontinuous linear functional defined on a normed space $X$. Then $\ker L$ is a dense linear subspace of $X$.
> 
> 
> 


**Proof.**
Since $L$ is discontinuous, it is unbounded, so there is a sequence $y\_n\in X$ such that $|Ly\_n|>n\Vert y\_n\Vert$. Thus $x\_n=y\_n/\Vert y\_n\Vert$ satisfies
$$
\Vert x\_n\Vert =1
\quad
\text{and}
\quad
|Lx\_n|>n. \tag2
$$
Let $y\in X$ be arbitrary. Then (2) yields
$$
\ker L\ni y-\Big(\frac{Ly}{Lx\_n}\Big)x\_n\to y
$$
so $\ker L$ is dense in $X$.
$\Box$


**Proof of the theorem.**
$X:=C\_c^\infty(\mathbb{R}^n)\subset L^2(\mathbb{R}^n)$ is a dense linear subspace. Note that
$$
L:X\to\mathbb{K},
\qquad
L\varphi=\int\_{\mathbb{R}^n}f(x)\varphi(x)\, dx
$$
is a linear functional on $X$. We will show that $L$ is unbounded (with respect to the $L^2$ norm in $X$). Suppose to the contrary that $L$ is bounded. Then it extends uniquely to a bounded linear functional
$\tilde{L}:L^2(\mathbb{R}^n)\to\mathbb{K}$. Therefore, the Riesz Representation yields existence of $g\in L^2(\mathbb{R}^n)$, such that
$$
\tilde{L}\varphi=\int\_{\mathbb{R}^n}\varphi(x)\overline{g(x)}\, dx
\quad
\varphi\in L^2(\mathbb{R}^n).
$$
In particular
$$
\int\_{\mathbb{R}^n}(f(x)-\overline{g(x)})\varphi(x)\, dx=0
\quad
\text{for all $\varphi\in C\_c^{\infty}(\mathbb{R}^n)$.}
$$
Therefore $f-\overline{g}=0$ a.e., $f=\overline{g}\in L^2(\mathbb{R}^n)$ which contradicts the assumption about $f$.


We proved that $L$ is discontinuous, hence by the lemma, $\ker L\subset X=C\_c^\infty(\mathbb{R}^n)$ is dense and hence it is dense in $L^2(\mathbb{R}^n)$. Now applying the Gramm-Schmidt orthogonalization to a countable and dense subset of $\ker L$ we obtain an orthonormal basis
$\{\varphi\_k\}\_{k=1}^\infty\subset\ker L$ of $L^2(\mathbb{R}^n)$ consisting of compactly supported smooth functions. Since $L\varphi\_k=0$ for all $k$, (1) follows.
$\Box$


