{ "cells": [ { "cell_type": "markdown", "id": "2429cc88", "metadata": {}, "source": [ "# Solving a Quadratic Equation\n", "\n", "For\n", "\n", "$$\n", "ax^2+bx+c=0,\\qquad a\\ne0,\n", "$$\n", "\n", "the two roots are\n", "\n", "$$\n", "x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}.\n", "$$\n", "\n", "Rather than entering roots separately for each example, the Egison\n", "program reads the coefficients of a polynomial, constructs its\n", "discriminant, and applies one typed symbolic solver.\n" ] }, { "cell_type": "markdown", "id": "f0c1634d", "metadata": {}, "source": [ "## Coefficients and the discriminant\n", "\n", "Egison's coefficients function returns coefficients in ascending\n", "degree order. Thus the pattern $[a_0,a_1,a_2]$ recognizes\n", "$a_2x^2+a_1x+a_0$. The helper keeps the cleared-denominator\n", "discriminant $b^2-4ac$ intact and explicitly groups the denominator\n", "as $2a$.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "23be8a3d", "metadata": {}, "outputs": [], "source": [ "declare symbol x, a, b, c: MathValue\n", "\n", "def solveQuadraticCoefficientsDemo\n", " (a : MathValue)\n", " (b : MathValue)\n", " (c : MathValue)\n", " : (MathValue, MathValue) :=\n", " let discriminant := b ^ 2 - 4 * a * c\n", " in ( ((- b) + sqrt discriminant) / (2 * a)\n", " , ((- b) - sqrt discriminant) / (2 * a) )\n", "\n", "def solveQuadraticDemo\n", " (f : MathValue)\n", " (x : MathValue)\n", " : (MathValue, MathValue) :=\n", " match coefficients f x as list mathValue with\n", " | [$a_0, $a_1, $a_2] ->\n", " solveQuadraticCoefficientsDemo a_2 a_1 a_0\n" ] }, { "cell_type": "markdown", "id": "61c83725", "metadata": {}, "source": [ "## A cyclotomic example\n", "\n", "The polynomial $x^2+x+1$ has discriminant $-3$. Its roots are the\n", "two primitive cube roots of unity.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "a97642ab", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$(\\frac{1}{2} \\sqrt{3} i + \\frac{-1}{2}, \\frac{-1}{2} \\sqrt{3} i + \\frac{-1}{2})$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "solveQuadraticDemo (x ^ 2 + x + 1) x\n" ] }, { "cell_type": "markdown", "id": "c9217626", "metadata": {}, "source": [ "## The symbolic formula\n", "\n", "Leaving $a$, $b$, and $c$ symbolic exposes the usual discriminant\n", "without any special formatting code.\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "c72484c5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$(\\frac{-1}{2} a^{-1} b + \\frac{1}{2} \\sqrt{b^{2} - 4 a c} a^{-1}, \\frac{-1}{2} a^{-1} b + \\frac{-1}{2} \\sqrt{b^{2} - 4 a c} a^{-1})$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "solveQuadraticDemo (a * x ^ 2 + b * x + c) x\n" ] }, { "cell_type": "markdown", "id": "4ca393c5", "metadata": {}, "source": [ "## A useful rescaling\n", "\n", "Writing the middle coefficient as $2b$ gives the equivalent compact\n", "form\n", "\n", "$$\n", "x=\\frac{-b\\pm\\sqrt{b^2-ac}}{a}.\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "e43288c2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$(-a^{-1} b + \\sqrt{b^{2} - a c} a^{-1}, -a^{-1} b - \\sqrt{b^{2} - a c} a^{-1})$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "solveQuadraticDemo (a * x ^ 2 + 2 * b * x + c) x\n" ] }, { "cell_type": "markdown", "id": "d6bfd467", "metadata": {}, "source": [ "## Takeaway\n", "\n", "Pattern matching and the typed helper separate the calculation into\n", "two transparent stages: read the polynomial coefficients, then form\n", "the discriminant and both signs of its square root. The displayed\n", "answers are produced from each input polynomial rather than inserted\n", "as precomputed outputs.\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 }