Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Partial Fractions

obscure_packages = ["sympy"]

for pkg in obscure_packages:
    try:
        __import__(pkg)  # check if installed
    except ImportError:
        print(f":warning: {pkg} not found. Installing...")
        !pip install {pkg}
        __import__(pkg)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from ipywidgets import interact
import ipywidgets as widgets
from sympy import Symbol, Eq, solve_undetermined_coeffs, apart

Partial Fractions

This example was copied from Wikipedia.

For example, we want to decompose:

f(x)=1x2+2x3f(x) = \frac{1}{x^2 + 2x - 3}

First, we factor:

q(x)=x2+2x3=(x+3)(x1)q(x) = x^2 + 2x - 3 = (x + 3)(x - 1)

So we have the PFD:

f(x)=1x2+2x3=Ax+3+Bx1f(x) = \frac{1}{x^2 + 2x - 3} = \frac{A}{x + 3} + \frac{B}{x - 1}

Giving us the polynomial identity: $$

1=A(x1)+B(x+3)1=AxA+Bx+3B0x+1=(A+B)x+(A+3B)    0=A+B1=A+3B\begin{align*} 1 &= A(x - 1) + B(x + 3) \\ 1 &= Ax - A + Bx + 3B \\ 0x + 1 &= (A + B)x + (-A + 3B) \\ \implies \\ 0 &= A + B \\ 1 &= -A + 3B \end{align*}

$$

Which we can solve using SymPy:

A = Symbol('A')
B = Symbol('B')
x = Symbol('x')
solve_undetermined_coeffs(Eq(1, A * (x - 1) + B * (x + 3)), [A, B], x)

Docs for solve_undteremined_coeffs

We can also have SymPy perform the entire PFD but this probably defeats the purpose:

apart(1 / (x**2 + 2*x - 3))
1(x1)(x+2)2=Ax1+Bx+2+C(x+2)2\frac{1}{(x-1)(x+2)^2} = \frac{A}{x-1} + \frac{B}{x+2} + \frac{C}{(x+2)^2}
1(x1)(x2+2x+5)=Ax1+Bx+Cx2+2x+5\frac{1}{(x-1)(x^2 + 2x + 5)} = \frac{A}{x-1} + \frac{Bx + C}{x^2 + 2x + 5}
1(x1)(x2+2x+5)2=Ax1+Bx+Cx2+2x+5+Dx+E(x2+2x+5)2\frac{1}{(x-1)(x^2 + 2x + 5)^2} = \frac{A}{x - 1} + \frac{Bx + C}{x^2 + 2x + 5} + \frac{Dx + E}{(x^2 + 2x + 5)^2}

question 1

1=A(x+2)2+B(x1)(x+2)+C(x1)1 = A(x+2)^2 + B(x-1)(x+2) + C(x-1)
C = Symbol('C')
solve_undetermined_coeffs(Eq(1, A * (x + 2) ** 2 + B * (x - 1) * (x + 2) + C * (x-1)), [A, B, C], x)

question 3

D = Symbol('D')
E = Symbol('E')

solve_undetermined_coeffs(Eq(1, A * (x ** 2 + 2 * x + 5) ** 2 + (B * x + C) * (x - 1) * (x ** 2 + 2 * x + 5) + (D * x + E) * (x-1)), [A, B, C, D, E], x)