I have just released BRational version 1.0.0, which is a SageMath package to beautifully format certain rational functions. As an example, take the rational function \[ \dfrac{1 + 26T + 66T^2 + 26T^3 + T^4}{(1 - T)^5} \] in SageMath, this would be expressed as
(-T^4 - 26*T^3 - 66*T^2 - 26*T - 1)/(T^5 - 5*T^4 + 10*T^3 - 10*T^2 + 5*T - 1)
but using brational
, this is expressed as
(1 + 26*T + 66*T^2 + 26*T^3 + T^4)/(1 - T)^5
For what I end up writing about, this package has helped save me significant time (and errors!). I wrote brational
over the course of a few years, gathering together stand-alone functions I wrote to simplify and comprehend rational functions.
I primarily use brational
to control and understand denominators and to print expressions directly to $\LaTeX$ markup. I’ll give a taste of it here, but check out the documentation for more details. Feel free to contribute to the repository.
Without getting under the hood of SageMath, I want rational expressions to be written as \[ \dfrac{F(\bm{X})}{\prod_{i=1}^n(1 - \bm{X}^{\alpha_i})}. \]
For good reasons, SageMath simplifies and reformats such expressions, so that it can be a mental chore to get it back into this format (and often times too challenging to just do mentally). The module brational
keeps this format throughout, and enables one to get the relevant signature of the denominator, so features like poles and orders are quickly read off.
Sometimes I am looking for specific numerators from rational functions that do not come from reduced expressions. One example is the above expression in $T$. The polynomial $1 + 26T + 66T^2 + 26T^3 + T^4$ is not the numerator of the reduced expression but one of the form \[ \dfrac{f(T)}{(1 - T)^\alpha}. \] Importantly, this polynomial has non-negative coefficients, which is not true of the numerator polynomial of the reduced expression.
The second main feature that I use is printing directly to $\LaTeX$. Yes, SageMath can already print to $\LaTeX$ with its latex
function. This works quite well and is used within brational
. However, I wanted a little more control over how expressions get converted. Using this package, there are ways to print directly to an align
environment.
For example, (taken directly from the documentation) printing the expanded polynomial of \[ B_{20}(X) = (1 + X)^{20} \] yields
\begin{align*}
B_{20}(X) &= 1 + 20X + 190X^2 + 1140X^3 + 4845X^4 + 15504X^5 + 38760X^6 + 77520X^7 + 125970X^8 \\
&\quad + 167960X^9 + 184756X^{10} + 167960X^{11} + 125970X^{12} + 77520X^{13} + 38760X^{14} + 15504X^{15} \\
&\quad + 4845X^{16} + 1140X^{17} + 190X^{18} + 20X^{19} + X^{20}
\end{align*}
which compiles to look like \[ \begin{aligned} B_{20}(X) &= 1 + 20X + 190X^2 + 1140X^3 + 4845X^4 + 15504X^5 + 38760X^6 + 77520X^7 + 125970X^8 \\ &\quad + 167960X^9 + 184756X^{10} + 167960X^{11} + 125970X^{12} + 77520X^{13} + 38760X^{14} + 15504X^{15} \\ &\quad + 4845X^{16} + 1140X^{17} + 190X^{18} + 20X^{19} + X^{20} \end{aligned} \]