The closure of periodic functions and sums of waves with incommensurable periods

Consider the space {C(\mathbb R)} of all bounded continuous functions {f\colon \mathbb R\to\mathbb R}, with the uniform norm {\|f\| = \sup |f|}. Let {P} be its subset that consists of all periodic continuous functions: recall that {f} is periodic if there exists {T>0} such that {f(x+T)=f(x)} for all {x\in \mathbb R}.

The set {P} is not closed in the topology of {C(\mathbb R)}. Indeed, let {d(x) = \mathrm{dist}\,(x, \mathbb Z)} be the distance from {x} to nearest integer. The function {d} is periodic with {T=1}. Therefore, each sum of the form {\displaystyle \sum_{k=0}^n 2^{-k} d(2^{-k} x)} is periodic with {T=2^n}. Hence the sum of the infinite series {\displaystyle f(x) = \sum_{k=0}^\infty 2^{-k} d(2^{-k} x) } is a uniform limit of periodic functions. Yet, {f} is not periodic, because {f(0)=0} and {f(x)>0 } for {x\ne 0} (for every {x\ne 0} there exists {k} such that {2^{-k}x} is not an integer).

anti-blanc
Uniform limit of periodic functions

The above example (which was suggested to me by Yantao Wu) is somewhat similar to the Takagi function, which differs from it by the minus sign in the exponent: {\displaystyle T(x) = \sum_{k=0}^\infty 2^{-k} d(2^{k} x) }. Of course, the Takagi function is periodic with period {1}.

blankmange
Takagi function

Do we really need an infinite series to get such an example? In other words, does the set {\overline{P}\setminus P} contain an elementary function?

A natural candidate is the sum of trigonometric waves with incommensurable periods (that is, the ratio of periods must be irrational). For example, consider the function {g(x) = \cos (x) + \cos (\sqrt{2}x)} whose graph is shown below.

sum_cos
Looks somewhat periodic, does not it?

Since {g(0)=2} and {g(x) < 2} for all {x\ne 0}, the function {g} is not periodic. Its graph looks vaguely similar to the graph of {f}. Is {g} a uniform limit of periodic functions?

Suppose {h\colon \mathbb R\to\mathbb R} is a {T}-periodic function such that {\|h-g\|<\epsilon}. Then {h(0) > 2-\epsilon}, hence {h(nT)>2-\epsilon} for all {n\in \mathbb Z}, hence {g(nT) > 2- 2\epsilon }. By the definition of {g} this implies {\cos (nT) > 1-2\epsilon} and {\cos (\sqrt{2}nT) > 1-2\epsilon} for all {n\in \mathbb Z}. The following lemma shows a contradiction between these properties.

Lemma. If a real number {t} satisfies {\cos nt > -1/2} for all {n\in \mathbb Z}, then {t} is an integer multiple of {2\pi}.

Proof. Suppose {t} is not an integer multiple of {2\pi}. We can assume {0 < t < \pi} without loss of generality, because {t} can be replaced by {t - 2\pi k} to get it in the interval {(0, 2\pi)} and then by {2\pi - t} to get it in {(0, \pi)}. Since {\cos t > -1/2}, we have {t\in (0, 2\pi/3)}. Let {k} be the smallest positive integer such that {2^k t \ge 2\pi/3}. The minimality of {k} implies {2^{k-1} t < 2\pi/3}, hence {2^k t \in [2\pi/3, 4\pi/3)}. But then {\cos (2^k t) \le -1/2}, a contradiction. {\quad \Box}

The constant {-1/2} in the lemma is best possible, since {\cos (2n\pi/3)\ge -1/2} for all {n\in \mathbb Z}.

Returning to the paragraph before the lemma, choose {\epsilon=3/4} so that {1-2\epsilon = -1/2}. The lemma says that both {T} and {\sqrt{2} T} must be integer multiples of {2\pi}, which is impossible since they are incommensurable. This contradiction shows that {\|g-h\|\ge 3/4} for any periodic function {h}, hence {g} is not a uniform limit of periodic functions.

The above result can be stated as {\mathrm{dist}(g, P) \ge 3/4}. I guess {\mathrm{dist}(g, P)} is actually {1}. It cannot be greater than {1} since {|g(x)-\cos x|\le 1} for all {x}. (Update: Yantao pointed out that the density of irrational rotations implies the distance is indeed equal to 1.)

Note: the set {\overline{P}} is a proper subset of the set of (Bohr / Bochner / uniform) almost periodic functions (as Yemon Choi pointed out in a comment). The latter is a linear space while {\overline{P}} is not. I was confused by the sentence “Bohr defined the uniformly almost-periodic functions as the closure of the trigonometric polynomials with respect to the uniform norm” on Wikipedia. To me, a trigonometric polynomial is a periodic function of particular kind. What Bohr called Exponentialpolynom is a finite sum of the form {\sum a_n e^{\lambda_n x}} where {\lambda_n} can be any real numbers. To summarize: the set considered above is the closure of {P} while the set of almost periodic functions is the closed linear span of {P}. The function {g(x)=\cos (x) + \cos(\sqrt{2} x)} is an example of the latter, not of the former.

Unreasonable effectiveness of the left endpoint rule

The left endpoint rule, and its twin right endpoint rule, are ugly ducklings of integration methods. The left endpoint rule is just the average of the values of the integrand over left endpoints of equal subintervals:

\displaystyle \int_a^b f(x)\,dx \approx \frac{b-a}{n} \sum_{i=0}^{n-1} f(a+i/n)

Here is its graphical representation with {n=10} on the interval {[-1,1]}: the sample points are marked with vertical lines, the length of each line representing the weight given to that point. Every point has the same weight, actually.

Left endpoint rule
Left endpoint rule

Primitive, ineffective, with error {O(1/n)} for {n} points used.

Simpson’s rule is more sophisticated, with error {O(1/n^4)}. It uses weights of three sizes:

Simpson's rule
Simpson’s rule

Gaussian quadrature uses specially designed (and difficult to compute) sample points and weights: more points toward the edges, larger weights in the middle.

Gaussian quadrature
Gaussian quadrature

Let’s compare these quadrature rules on the integral {\int_{-1}^1 e^x \cos \pi x\,dx}, using {10} points as above. Here is the function:

Nice smooth function
Nice smooth function
  • The exact value of the integral is {\dfrac{e^{-1}-e}{1+\pi^2}}, about -0.216.
  • Simpson’s rule gets within 0.0007 of the exact value. Well done!
  • Gaussian quadrature gets within 0.000000000000003 of the exact value. Amazing!
  • And the lame left endpoint rule outputs… a positive number, getting even the sign wrong! This is ridiculous. The error is more than 0.22.

Let’s try another integral: {\int_{-1}^1 \sqrt{4+\cos \pi x +\sin \pi x} \,dx}, again using {10} points. The function looks like this:

Another nice function
Another nice function

The integral can be evaluated exactly… sort of. In terms of elliptic integrals. And preferably not by hand:

Maple output, "simplified"
Maple output, “simplified”
  • Simpson’s rule is within 0.00001 of the exact value, even better than the first time.
  • Gaussian quadrature is within 0.00000003, not as spectacular as in the first example.
  • And the stupid left endpoint rule is … accurate within 0.00000000000000005. What?

The integral of a smooth periodic function over its period amounts to integration over a circle. When translated to the circle, the elaborate placement of Gaussian sample points is… simply illogical. There is no reason to bunch them up at any particular point: there is nothing special about (-1,0) or any other point of the circle.

Gaussian nodes on a circle
Gaussian nodes on a circle

The only natural approach here is the simplest one: equally spaced points, equal weights. Left endpoint rule uses it and wins.

Equally spaced points
Equally spaced points