Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lagrange_basis functions throws an error #3

Open
johnfrancmartin opened this issue Dec 8, 2019 · 1 comment · May be fixed by #5
Open

lagrange_basis functions throws an error #3

johnfrancmartin opened this issue Dec 8, 2019 · 1 comment · May be fixed by #5

Comments

@johnfrancmartin
Copy link

Lagrange Basis function throws an Attribute Error at the line

return (numerator * denominator.mod_inverse(o)) % o

Specifically:
AttributeError: 'int' object has no attribute 'mod_inverse'

Attempts to solve this by forcing denominator to a Bn cause further errors.

@Fronsto
Copy link

Fronsto commented Apr 17, 2024

Forcing both numerator and denominator to Bn works fine.

def lagrange_basis(indexes, o, x=0):
    """ generates all lagrange basis polynomials """
    l = []
    for i in indexes:
        numerator, denominator = Bn.from_num(1), Bn.from_num(1)
        for j in indexes:
            if j != i:
                numerator = (numerator * (x - j)) % o
                denominator = (denominator * (i - j)) % o
        l.append((numerator * denominator.mod_inverse(o)) % o)
    return l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants