{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Math examples\n", "With attempts to use Unicode operators where possible to make the Raku code look like the math." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Cauchy-Schwarz Inequality\n", " from the [jupyter docs](http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Typesetting%20Equations.html)\n", "\\begin{equation*}\n", "\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)\n", "\\end{equation*}" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sub cauchy-schwarz(@a,@b) {\n", " ( [+] @a Z× @b )² ≤ ( [+] @a»² ) × ( [+] @b»² )\n", "}\n", "\n", "cauchy-schwarz( ( ^100 ).pick(5), ( ^100 ).pick(5) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Cubic formula\n", "see this [discussion](http://www.perlmonks.org/?node_id=1189383) and [wikipedia](https://en.wikipedia.org/wiki/Cubic_function#Algebraic_solution)\n", "\\begin{equation*}\n", "\\Delta_0 = b^2 - 3ac\n", "\\\\\n", "\\Delta_1 = 2b^3 - 9abc + 27a^2d\n", "\\\\\n", "C = \\sqrt[3]{ \\frac{ \\Delta_1 \\pm \\sqrt{\\Delta_1^2 - 4 \\Delta_0^3 } }{2} }\n", "\\\\\n", "x_k = - \\frac{1}{3a}( b + ς^k C + \\frac{\\Delta_0}{ς^k C}), k ∊ {0,1,2}\n", "\\end{equation*}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[True True True]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sub cubic(\\a,\\b,\\c,\\d) {\n", " my \\Δ0 = b²\t- 3 × a × c;\n", " # note: special case when Δ0 == 0\n", " my \\Δ1 = 2 * b³ - 9 × a × b × c + 27 × a² × d;\n", " my \\C = ( ( Δ1 + sqrt( Δ1² - 4 × Δ0³ + 0i) ) / 2 ).roots(3)[0];\n", " my \\ς = 1.roots(3); # cubic roots of unity\n", " return [0,1,2].map: -> \\k {\n", " ( -1 / ( 3 × a ) ) × ( b + ς[k] × C + Δ0 / ( C × ς[k] ) )\n", " }\n", "}\n", "\n", "my @vals = cubic(1,10,10,-10);\n", "my $f = -> \\x { x³ + 10 * x² + 10 * x - 10 };\n", "\n", "my $*TOLERANCE = 1e-10;\n", "\n", "[ $f( $_ ) ≅ 0 for @vals ]" ] } ], "metadata": { "kernelspec": { "display_name": "Raku", "language": "raku", "name": "raku" }, "language_info": { "file_extension": ".raku", "mimetype": "text/x-raku", "name": "raku", "version": "6.d" } }, "nbformat": 4, "nbformat_minor": 2 }