{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Algebraic Manipulation\n", "\n", "\n", "Here is a small sample of algebraic manipulation functions in `Symata`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using Symata # load Symata and enter symata mode" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{JupyterForm} $$" ], "text/plain": [ "L\"$$ \\text{JupyterForm} $$\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(JupyterForm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Common subexpression elimination" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) + \\left( \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) \\right) ^{\\frac{1}{2}} $$" ], "text/plain": [ "L\"$$ \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) + \\left( \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) \\right) ^{\\frac{1}{2}} $$\"" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex = (x-y)*(z-y) + Sqrt((x-y)*(z-y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Cse(expr)` recursively replaces subexpressions that occur more than once in `expr` with names. The transformed expression is returned with a list of rules that can be used to recover `expr`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\left[ x1 + x1^{\\frac{1}{2}} \\right] , \\left[ x1 \\Rightarrow \\left( \\left( x + x0 \\right) \\ \\left( x0 + z \\right) \\right) ,x0 \\Rightarrow \\left( - \\ y \\right) \\right] \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\left[ x1 + x1^{\\frac{1}{2}} \\right] , \\left[ x1 \\Rightarrow \\left( \\left( x + x0 \\right) \\ \\left( x0 + z \\right) \\right) ,x0 \\Rightarrow \\left( - \\ y \\right) \\right] \\right] $$\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Cse(ex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Applying in order the replacement rules in the second list to the expression in the first list results in the original expression.\n", "\n", "To apply the rules, we will use `Splat`, which works like this," ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( a,b,c,d \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( a,b,c,d \\right) $$\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(a,b,Splat([c,d]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and `Fold`, which works like this," ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( f \\! \\left( f \\! \\left( x,a \\right) ,b \\right) ,c \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( f \\! \\left( f \\! \\left( x,a \\right) ,b \\right) ,c \\right) $$\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Fold(f, [x,a,b,c])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apply the replacement rules like this," ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) + \\left( \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) \\right) ^{\\frac{1}{2}} $$" ], "text/plain": [ "L\"$$ \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) + \\left( \\left( x \\ - \\ y \\right) \\ \\left( - \\ y + z \\right) \\right) ^{\\frac{1}{2}} $$\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Fold(ReplaceAll, Splat(Cse(ex)))[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check that the reconstructed expression is equal to the original expression." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{True} $$" ], "text/plain": [ "L\"$$ \\text{True} $$\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Fold(ReplaceAll, Splat(Cse(ex)))[1] == ex" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "ClearAll(ex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `Together` and `Apart`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Together` rewrites rational expressions as a single fraction." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{x \\ y + x \\ z + y \\ z}{x \\ y \\ z} $$" ], "text/plain": [ "L\"$$ \\frac{x \\ y + x \\ z + y \\ z}{x \\ y \\ z} $$\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(1/x + 1/y + 1/z)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{x + y}{x \\ y^{2}} $$" ], "text/plain": [ "L\"$$ \\frac{x + y}{x \\ y^{2}} $$\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(1/(x*y) + 1/y^2) " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{x \\ \\left( 1 + y \\right) + \\left( 1 + x \\right) \\ y}{ \\left( 1 + x \\right) \\ \\left( 1 + y \\right) } $$" ], "text/plain": [ "L\"$$ \\frac{x \\ \\left( 1 + y \\right) + \\left( 1 + x \\right) \\ y}{ \\left( 1 + x \\right) \\ \\left( 1 + y \\right) } $$\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(1/(1 + 1/x) + 1/(1 + 1/y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, `Together` only works at the topmost level." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\mathbb{e} ^{x^{-1} + y^{-1}} $$" ], "text/plain": [ "L\"$$ \\mathbb{e} ^{x^{-1} + y^{-1}} $$\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(Exp(1/x + 1/y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Together` is applied at all levels if the option `Deep` is true." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\mathbb{e} ^{\\frac{x + y}{x \\ y}} $$" ], "text/plain": [ "L\"$$ \\mathbb{e} ^{\\frac{x + y}{x \\ y}} $$\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(Exp(1/x + 1/y), Deep => True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Apart` gives the partial fraction decomposition of a rational expression" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{y}{1 + x} \\ + \\frac{- \\ y}{2 + x} $$" ], "text/plain": [ "L\"$$ \\frac{y}{1 + x} \\ + \\frac{- \\ y}{2 + x} $$\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Apart(y/(x + 2)/(x + 1), x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the denominator has non-rational roots, the option ``Full => True`` must be given." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{\\frac{- \\ y}{3} + \\frac{ \\left( -2 \\right) \\ \\left( \\frac{-1}{2} + \\left( \\frac{-1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} \\right) \\ y}{3}}{\\frac{1}{2} + \\left( \\frac{1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} + x} + \\frac{\\frac{- \\ y}{3} + \\frac{ \\left( -2 \\right) \\ \\left( \\frac{-1}{2} + \\left( \\frac{1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} \\right) \\ y}{3}}{\\frac{1}{2} + \\left( \\frac{-1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} + x} $$" ], "text/plain": [ "L\"$$ \\frac{\\frac{- \\ y}{3} + \\frac{ \\left( -2 \\right) \\ \\left( \\frac{-1}{2} + \\left( \\frac{-1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} \\right) \\ y}{3}}{\\frac{1}{2} + \\left( \\frac{1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} + x} + \\frac{\\frac{- \\ y}{3} + \\frac{ \\left( -2 \\right) \\ \\left( \\frac{-1}{2} + \\left( \\frac{1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} \\right) \\ y}{3}}{\\frac{1}{2} + \\left( \\frac{-1}{2} \\ \\mathbb{i} \\right) \\ 3^{\\frac{1}{2}} + x} $$\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Apart(y/(x^2 + x + 1), x, Full=>True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `Collect`\n", "\n", "Collect coefficients of powers of `x`." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ c + \\left( a \\ - \\ b \\right) \\ x + \\left( a + b \\right) \\ x^{2} $$" ], "text/plain": [ "L\"$$ c + \\left( a \\ - \\ b \\right) \\ x + \\left( a + b \\right) \\ x^{2} $$\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Collect(a*x^2 + b*x^2 + a*x - b*x + c, x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Collect coefficients of an expression." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left( 2 \\ a + b \\right) \\ x \\ \\text{Log} \\! \\left( x \\right) $$" ], "text/plain": [ "L\"$$ \\left( 2 \\ a + b \\right) \\ x \\ \\text{Log} \\! \\left( x \\right) $$\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Collect(a*x*Log(x) + (b+a)*(x*Log(x)), x*Log(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Version and date" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.6\n", "Julia version 1.6.0-DEV.58\n", "Python version 3.8.3\n", "SymPy version 1.5.1\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "2020-05-29T22:21:37.831" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "InputForm(Now())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Original Version and date" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "VersionInfo()\n", "\n", "symata version 0.3.0-dev.7\n", "julia version 0.6.0-dev.435\n", "python version 2.7.12\n", "sympy version 1.0" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "Now()\n", "2016−11−28T22:34:33.713" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.6.0-DEV", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }