Algebraic Manipulation

Here is a small sample of algebraic manipulation functions in Symata.


In [1]:
using Symata # load Symata and enter symata mode

In [2]:
OutputStyle(JupyterForm)


Out[2]:
$$ \text{JupyterForm} $$

Common subexpression elimination


In [3]:
ex  = (x-y)*(z-y) + Sqrt((x-y)*(z-y))


Out[3]:
$$ \left( x \ - \ y \right) \ \left( - \ y + z \right) + \left( \left( x \ - \ y \right) \ \left( - \ y + z \right) \right) ^{\frac{1}{2}} $$

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.


In [4]:
Cse(ex)


Out[4]:
$$ \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] $$

Applying in order the replacement rules in the second list to the expression in the first list results in the original expression.

To apply the rules, we will use Splat, which works like this,


In [5]:
f(a,b,Splat([c,d]))


Out[5]:
$$ f \! \left( a,b,c,d \right) $$

and Fold, which works like this,


In [6]:
Fold(f, [x,a,b,c])


Out[6]:
$$ f \! \left( f \! \left( f \! \left( x,a \right) ,b \right) ,c \right) $$

Apply the replacement rules like this,


In [7]:
Fold(ReplaceAll, Splat(Cse(ex)))[1]


Out[7]:
$$ \left( x \ - \ y \right) \ \left( - \ y + z \right) + \left( \left( x \ - \ y \right) \ \left( - \ y + z \right) \right) ^{\frac{1}{2}} $$

Check that the reconstructed expression is equal to the original expression.


In [8]:
Fold(ReplaceAll, Splat(Cse(ex)))[1] == ex


Out[8]:
$$ \text{True} $$

In [9]:
ClearAll(ex)

Together and Apart

Together rewrites rational expressions as a single fraction.


In [10]:
Together(1/x + 1/y + 1/z)


Out[10]:
$$ \frac{x \ y + x \ z + y \ z}{x \ y \ z} $$

In [11]:
Together(1/(x*y) + 1/y^2)


Out[11]:
$$ \frac{x + y}{x \ y^{2}} $$

In [12]:
Together(1/(1 + 1/x) + 1/(1 + 1/y))


Out[12]:
$$ \frac{x \ \left( 1 + y \right) + \left( 1 + x \right) \ y}{ \left( 1 + x \right) \ \left( 1 + y \right) } $$

By default, Together only works at the topmost level.


In [13]:
Together(Exp(1/x + 1/y))


Out[13]:
$$ \mathbb{e} ^{x^{-1} + y^{-1}} $$

Together is applied at all levels if the option Deep is true.


In [14]:
Together(Exp(1/x + 1/y), Deep => True)


Out[14]:
$$ \mathbb{e} ^{\frac{x + y}{x \ y}} $$

Apart gives the partial fraction decomposition of a rational expression


In [15]:
Apart(y/(x + 2)/(x + 1), x)


Out[15]:
$$ \frac{y}{1 + x} \ + \frac{- \ y}{2 + x} $$

If the denominator has non-rational roots, the option Full => True must be given.


In [16]:
Apart(y/(x^2 + x + 1), x, Full=>True)


Out[16]:
$$ \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} $$

Collect

Collect coefficients of powers of x.


In [17]:
Collect(a*x^2 + b*x^2 + a*x - b*x + c, x)


Out[17]:
$$ c + \left( a \ - \ b \right) \ x + \left( a + b \right) \ x^{2} $$

Collect coefficients of an expression.


In [18]:
Collect(a*x*Log(x) + (b+a)*(x*Log(x)), x*Log(x))


Out[18]:
$$ \left( 2 \ a + b \right) \ x \ \text{Log} \! \left( x \right) $$

Version and date


In [19]:
VersionInfo()


Symata version     0.4.1-dev.3
Julia version      0.7.0-beta2.1
Python version     2.7.14+
SymPy version      1.0

In [20]:
InputForm(Now())


Out[20]:
2018-07-20T13:14:10.907

Original Version and date

VersionInfo()

symata version 0.3.0-dev.7 julia version 0.6.0-dev.435 python version 2.7.12 sympy version 1.0

Now() 2016−11−28T22:34:33.713