{ "cells": [ { "cell_type": "markdown", "id": "2429cc88", "metadata": {}, "source": [ "# 二次方程式を解く\n", "\n", "二次方程式\n", "\n", "$$\n", "ax^2+bx+c=0,\\qquad a\\ne0,\n", "$$\n", "\n", "の二つの解は、次の公式で与えられます。\n", "\n", "$$\n", "x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}.\n", "$$\n", "\n", "例ごとに解を個別に入力するのではなく、Egisonプログラムで多項式の係数を読み取り、\n", "判別式を組み立て、一つの型付き記号計算ソルバーを適用します。\n" ] }, { "cell_type": "markdown", "id": "f0c1634d", "metadata": {}, "source": [ "## 係数と判別式\n", "\n", "Egisonの coefficients 関数は、次数の昇順で係数を返します。したがって、\n", "パターン $[a_0,a_1,a_2]$ は $a_2x^2+a_1x+a_0$ を認識します。補助関数では、\n", "分母を払った判別式 $b^2-4ac$ をそのまま保ち、分母を明示的に $2a$ として\n", "まとめます。\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": [ "## 円分多項式の例\n", "\n", "多項式 $x^2+x+1$ の判別式は $-3$ です。その解は、二つの1の原始3乗根です。\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": [ "## 記号のまま得られる公式\n", "\n", "$a$、$b$、$c$ を記号のままにすると、特別な整形コードを使わなくても通常の\n", "判別式がそのまま現れます。\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": [ "## 便利な係数の取り方\n", "\n", "一次の係数を $2b$ と書けば、同値で簡潔な次の形が得られます。\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": [ "## まとめ\n", "\n", "パターンマッチと型付き補助関数により、計算を見通しのよい二段階に分けられます。\n", "まず多項式の係数を読み取り、次に判別式と、その平方根の正負両方を組み立てます。\n", "表示される解は事前計算した出力を埋め込んだものではなく、各入力多項式から\n", "実際に生成されたものです。\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 }