unlisted
ubuntu2004# -*- coding: utf-8 -*-1# ****************************************************************************2# Copyright (C) 2021, Vincent Delecroix <[email protected]>3#4# Distributed under the terms of the GNU General Public License (GPL)5# as published by the Free Software Foundation; either version 2 of6# the License, or (at your option) any later version.7# https://www.gnu.org/licenses/8# ****************************************************************************910def inverse_of_unit(elt):11"""12Generic inversion of univariate or multivariate element of a graded algebra.1314TESTS::1516sage: Integers(1)['x'](0).inverse_of_unit()17018"""19P = elt.parent()20if not elt.is_unit():21raise ArithmeticError("{} is not a unit in {}".format(elt, elt.parent()))2223u = elt.constant_coefficient()24ui = u.inverse_of_unit()25n = - ui * (elt - u) # nilpotent26nn = n27res = P.one()28while nn:29res += nn30nn *= n31return ui * res323334