{ "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": { "collapsed": false }, "outputs": [], "source": [ "using Symata; isymata() # load Symata and enter symata mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Common subexpression elimination" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex = (x-y)*(z-y) + Sqrt((x-y)*(z-y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Cse(expr)` recursivley 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": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ [[x1 + x1^{\\frac{1}{2}}],[x1 \\Rightarrow \\left( \\left( x + x0 \\right) \\ \\left( x0 + z \\right) \\right) ,x0 \\Rightarrow \\left( - \\ y \\right) ]] $$" ], "text/plain": [ "L\"$$ [[x1 + x1^{\\frac{1}{2}}],[x1 \\Rightarrow \\left( \\left( x + x0 \\right) \\ \\left( x0 + z \\right) \\right) ,x0 \\Rightarrow \\left( - \\ y \\right) ]] $$\"" ] }, "execution_count": 3, "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", "We will use `Splat`, which works like this," ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( a,b,c,d \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( a,b,c,d \\right) $$\"" ] }, "execution_count": 4, "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": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( \\left( f \\! \\left( \\left( f \\! \\left( x,a \\right) \\right) ,b \\right) \\right) ,c \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( \\left( f \\! \\left( \\left( f \\! \\left( x,a \\right) \\right) ,b \\right) \\right) ,c \\right) $$\"" ] }, "execution_count": 5, "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": 6, "metadata": { "collapsed": false }, "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": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Fold(ReplaceAll, Splat(Cse(ex)))[1]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "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": 8, "metadata": { "collapsed": false }, "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": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(1/x + 1/y + 1/z)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{x + y}{x \\ y^{2}} $$" ], "text/plain": [ "L\"$$ \\frac{x + y}{x \\ y^{2}} $$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Together(1/(x*y) + 1/y^2) " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "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": 10, "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": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\mathbb{e} ^{x^{-1} + y^{-1}} $$" ], "text/plain": [ "L\"$$ \\mathbb{e} ^{x^{-1} + y^{-1}} $$\"" ] }, "execution_count": 11, "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": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\mathbb{e} ^{\\frac{x + y}{x \\ y}} $$" ], "text/plain": [ "L\"$$ \\mathbb{e} ^{\\frac{x + y}{x \\ y}} $$\"" ] }, "execution_count": 12, "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": 13, "metadata": { "collapsed": false }, "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": 13, "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": 14, "metadata": { "collapsed": false }, "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": 14, "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": 15, "metadata": { "collapsed": false }, "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": 15, "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": 16, "metadata": { "collapsed": false }, "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": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Collect(a*x*Log(x) + (b+a)*(x*Log(x)), x*Log(x))" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.6.0-dev", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }