While giving the first of eight lectures on introductory model theory and its applications yesterday, I stated Hilbert's 17th problem (or rather, Artin's Theorem): if $f \in \mathbb{R}[t_1,\ldots,t_n]$ is positive semidefinite -- i.e., non-negative when evaluated at every $x = (x_1,\ldots,x_n) \in \mathbb{R}^n$ -- then it is a sum of squares of rational functions. One naturally asks (i) must $f$ be a sum of squares of polynomials, and (ii) do we know how many rational functions are necessary? The general answers here are no (Motzkin) and no more than $2^n$ (Pfister). Then I mentioned that the case of $n=1$ is a very nice exercise, because one can prove in this case that indeed $f(t)$ is positive semidefinite iff it is a sum of two (and not necessarily one, clearly) squares of polynomials. Finally I muttered that this was a sort of function field analogue of Fermat's Two Squares Theorem (F2ST).
So I thought about how to prove this result, and I was able to come up with a proof that follows the same recipe as the Gaussian integers proof of F2ST. Then I realized that the key step of the proof was that a monic irreducible quadratic polynomial over $\mathbb{R}$ is a sum of two squares, which can be shown by...completing the square.
But then today I went back to the general setup of a "Gaussian integers" proof, and I came up with the following definition and theorem.
Definition: An integral domain $R$ is imaginary if $-1$ is a square in its fraction field; otherwise it is nonimaginary. (In fact I will mostly be considering Dedekind domains, hence integrally closed, and in this case if $-1$ is a square in the fraction field it's already a square in $R$, so no need to worry much about that distinction.) Note that nonimaginary is a much weaker condition than the fraction field being formally real.
(Definition: An element $f$ in a domain $R$ is a sum of two squares up to a unit if there exist $a,b \in R$ and $u \in R^{\times}$ such that $f = u(a^2+b^2)$.)
Theorem: Let $R$ be a nonimaginary domain such that $R[i]$ ($= R[t]/(t^2+1)$) is a PID.
a) Let $p$ be a prime element of $R$ (i.e., $pR$ is a prime ideal). Then $p$ is a sum of two squares up to a unit iff the residue field $R/(p)$ is imaginary.
b) Suppose moreover that $R$ is a PID. Then a nonzero element $f$ of $R$ is a sum of two squares up to a unit iff $\operatorname{ord}_p(f)$ is even for each prime element $p$ of $R$ such that $R/(p)$ is nonimaginary.
[Proof: Introduce the "Gaussian" ring $R[i]$ and the norm map $N: R[i] \rightarrow R$. Follow your nose, referring back to the proof of F2ST as needed.]
Corollaries: 1) F2ST. 2) Artin-Pfister for $n = 1$. 3) A characterization of sums of two squares in a polynomial ring over a nonimaginary finite field (a 1967 theorem of Leahey).
4) Let $p \equiv 3,7 \pmod{20}$ be a prime number. Then $p$ is a sum of two squares up to a unit in $\mathbb{Z}[\sqrt{-5}]$ but is not (by F2ST) a sum of two squares in $\mathbb{Z}$.
Finally the questions:
Have you seen anything like this result before?
I haven't, explicitly, but somehow I feel subconsciously that I may have. It's hard to believe that this is something new under the sun.
What do you make of the strange situation in which $R$ is not a PID but $R[i]$ is?
Note that one might think this impossible, but $R = \mathbb{R}[x,y]/(x^2+y^2-1)$ is an example. [Reference: Theorem 12 of Elliptic Dedekind domains revisited.] Do you have any idea about how one might go about producing more such examples, e.g. with $R$ the ring of integers of a number field (or a localization thereof)?
Addendum: As I commented on below, a good answer to the first question seems to be the paper
MR0578805 (81h:10028) Choi, M. D.; Lam, T. Y.; Reznick, B.; Rosenberg, A. Sums of squares in some integral domains. J. Algebra 65 (1980), no. 1, 234--256.
In this paper, they prove the theorem above with slightly different hypotheses: $R$ is a nonimaginary UFD such that $R[i]$ is also a UFD. Looking back at my proof, the only reason I assumed PID was not to worry about the distinction between $R/pR$ and its fraction field. Just now I went back to check that everything works okay with PID replaced by UFD. So the second question becomes more important: what are some examples to exploit the fact that $R[i]$, but not $R$, needs to be a UFD?