Partial fractions - equivalent of MATLAB's partfrac and Mathematica's apart in Julia

155 views Asked by At

I am trying to natively do partial fraction decomposition in Julia. In MATLAB the necessary command to use is partfrac and in Mathematica it is Apart. I know I can probably use PyCall.jl to use its version of apart but that probably incurs some overhead..

I am wondering if anyone knows any packages/gists/codelets that lets you do this natively in Julia; i.e. get the equivalent of partfrac (MATLAB), Apart (Mathematica) or apart (SymPy).

The ones I found by Googling are here (which was last updated 6 years ago) and here which again seems outdated (if not deprecated).

I was expecting it to be part of some standard package in Julia. I found a mention of residues in Polynomials.jl but no other documentation of it ... it doesn't look like it is the equivalent of Apart, partfrac, or apart either.

1

There are 1 answers

1
Bill On

The documentation for residues() in Polynomials says

help?> ?residues search: residues

residues(pq::AbstractRationalFunction; method=:numerical, kwargs...)

If p/q =d + r/q, returns d and the residues of a rational fraction r/q.

First expresses p/q =d + r/q with r of lower degree than q through divrem. Then finds the poles of r/q. For a pole, λj of multiplicity k there are k residues, rⱼ[k]/(z-λⱼ)^k, rⱼ[k-1]/(z-λⱼ)^(k-1), rⱼ[k-2]/(z-λⱼ)^(k-2),
…, rⱼ[1]/(z-λⱼ). The residues are found using this formula: 1/j! * dʲ/dsʲ (F(s)(s - λⱼ)^k evaluated at λⱼ
(5-28 (https://stanford.edu/~boyd/ee102/rational.pdf)).

Here is an example:

using Polynomials

let 
    nume = Polynomial([6, 9, 16, 8, 1])
    den = Polynomial([6, 11, 6, 1])
    @show residues(nume // den)
end

residues(nume // den) = (Polynomial(2.0 + 1.0*x), 
Dict(-1.9999999999999998 => [-4.000000000000001], -1.0000000000000002 => 
[3.0000000000000036], -3.0 => [-6.0]))

it looks as if the residues of a division can be returned as floating point, so you may need to restore them to integer after the division.