{ "cells": [ { "cell_type": "markdown", "id": "4fe0795e", "metadata": {}, "source": [ "# Leibniz Formula from a Fourier Series\n", "\n", "The Leibniz series\n", "\n", "$$\n", "\\frac{\\pi}{4}=1-\\frac13+\\frac15-\\frac17+\\cdots\n", "$$\n", "\n", "follows by evaluating the Fourier series of the sawtooth\n", "$f(x)=x$ at $x=\\pi/2$. This notebook derives the coefficients\n", "symbolically before making that substitution.\n" ] }, { "cell_type": "markdown", "id": "7a46b4a7", "metadata": {}, "source": [ "## The sawtooth and its coefficients\n", "\n", "Since $f$ is odd, only sine terms occur:\n", "\n", "$$\n", "x=\\sum_{k=1}^{\\infty}b_k\\sin(kx),\\qquad\n", "b_k=\\frac1\\pi\\int_{-\\pi}^{\\pi}x\\sin(kx)\\,dx.\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "8fe91f34", "metadata": {}, "outputs": [], "source": [ "declare symbol x, n : MathValue\n", "\n", "def f (x : MathValue) : MathValue := x\n", "\n", "def cosinePrimitive (k : MathValue) : MathValue :=\n", " x * sin (k * x) / k + cos (k * x) / k^2\n", "\n", "def sinePrimitive (k : MathValue) : MathValue :=\n", " (- x) * cos (k * x) / k + sin (k * x) / k^2\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "ea1343e8", "metadata": {}, "outputs": [], "source": [ "def cosineCoefficients : [MathValue] :=\n", " map\n", " (\\k ->\n", " let primitive := cosinePrimitive k\n", " in (substitute [(x, π)] primitive - substitute [(x, - π)] primitive) / π)\n", " nats\n", "\n", "def sineCoefficients : [MathValue] :=\n", " map\n", " (\\k ->\n", " let primitive := sinePrimitive k\n", " in (substitute [(x, π)] primitive - substitute [(x, - π)] primitive) / π)\n", " nats\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "81fe69e4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$\\{2, -1, \\frac{2}{3}, \\frac{-1}{2}, \\frac{2}{5}, \\frac{-1}{3}, \\frac{2}{7}, \\frac{-1}{4}, \\frac{2}{9}, \\frac{-1}{5}\\}$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 10 sineCoefficients\n" ] }, { "cell_type": "markdown", "id": "92a62bbc", "metadata": {}, "source": [ "## Fourier terms\n", "\n", "Egison now combines each coefficient with its basis function.\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "d71c6139", "metadata": {}, "outputs": [], "source": [ "def fourierTerms : [MathValue] :=\n", " map (\\(k, b) -> b * sin (k * x)) (zip nats sineCoefficients)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "64cced3c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$\\{2 \\sin(x), -\\sin(2 x), \\frac{2}{3} \\sin(3 x), \\frac{-1}{2} \\sin(4 x), \\frac{2}{5} \\sin(5 x), \\frac{-1}{3} \\sin(6 x), \\frac{2}{7} \\sin(7 x), \\frac{-1}{4} \\sin(8 x), \\frac{2}{9} \\sin(9 x), \\frac{-1}{5} \\sin(10 x)\\}$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 10 fourierTerms\n" ] }, { "cell_type": "markdown", "id": "08076199", "metadata": {}, "source": [ "## Evaluate at $x=\\pi/2$\n", "\n", "Even harmonics vanish, while successive odd harmonics alternate in\n", "sign. We encode the exact four-step pattern\n", "\n", "$$\n", "\\sin(k\\pi/2)=1,0,-1,0,\\ldots\n", "$$\n", "\n", "before dividing the Fourier terms by two. Thus the identity\n", "$\\pi/2=2(1-1/3+1/5-\\cdots)$ gives the desired series.\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "fb0deb25", "metadata": {}, "outputs": [], "source": [ "def sinAtHalfPi (k : Integer) : MathValue :=\n", " if isEven k\n", " then 0\n", " else (-1) ^ (i.quotient (k - 1) 2)\n", "\n", "def leibnizTerms : [MathValue] :=\n", " map\n", " (\\(k, b) -> b * sinAtHalfPi k / 2)\n", " (zip nats sineCoefficients)\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "a08d47cd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$\\{1, 0, \\frac{-1}{3}, 0, \\frac{1}{5}, 0, \\frac{-1}{7}, 0, \\frac{1}{9}, 0\\}$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 10 leibnizTerms\n" ] }, { "cell_type": "markdown", "id": "7ad715af", "metadata": {}, "source": [ "Reading the nonzero entries yields\n", "$1,-1/3,1/5,-1/7,\\ldots$. Their infinite sum is $\\pi/4$;\n", "the zeros record the even Fourier modes that vanish at $\\pi/2$.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Egison", "language": "egison", "name": "egison" }, "language_info": { "codemirror_mode": "egison", "file_extension": ".egi", "mimetype": "text/x-egison", "name": "egison" } }, "nbformat": 4, "nbformat_minor": 5 }