Translation of Mathematica code used in my thesis into Python