madng-tpsa

Python bindings for MAD-NG’s Generalised Truncated Power Series Algebra (GTPSA). It provides real and complex truncated multivariate power series with a small, Pythonic interface backed by the MAD-NG C library.

Quick start

Start by defining the algebraic space: its variables, optional parameters, and maximum order. Series created from the same Descriptor can then be combined algebraically and differentiated.

from madng_tpsa import Descriptor

descriptor = Descriptor(variables=['x', 'y'], order=3)
x, y = descriptor.vars()
f = x**2 + 2 * y

f.format()  # '2 * y + x**2'
f.derivative('y')  # Tpsa({(0, 0): 2.0})
f.monomial_coeffs()  # {(0, 1): 2.0, (2, 0): 1.0}

NumPy compatibility

TPSA objects implement the NumPy ufunc protocol for supported elementary functions, so ufuncs preserve the series rather than converting it to a scalar. The same applies to supported SciPy special-function ufuncs. They return Tpsa objects for real inputs.

import numpy as np
import scipy.special

np.sin(f)  # A Tpsa containing the truncated sine series
scipy.special.erf(f)  # A Tpsa containing the truncated error function

Complex series

ComplexTpsa has the corresponding complex-coefficient API. Promote a real series with from_tpsa(), or use a function whose mathematical result is complex. In particular, scipy.special.wofz promotes a real TPSA result to ComplexTpsa to match SciPy’s scalar behaviour.

import scipy.special
from madng_tpsa import ComplexTpsa

z = ComplexTpsa.from_tpsa(x, y)  # x + 1j * y
np.exp(z)  # A ComplexTpsa
scipy.special.wofz(f)  # A ComplexTpsa