Ask Your Question

PolynomialRing in Sage

asked 2018-09-21 11:21:12 +0100

ZacariasSatrustegui gravatar image

What does this "fastfrac" function do? Because I don't understand for example, what does "numerator()" function?

What is the sintaxis R(numerator(top)) I don't understand.

R.<ua,ud,ux2,uy2,ux1,uy1,ux1,uy1,uz1,ux2,uy2,uz2> = PolynomialRing(QQ,12,order='invlex')

class fastfrac:

def __init__(self,top,bot=1):

if parent(top) == ZZ or parent(top) == R: = R(top) = R(bot)
elif top.__class__ == fastfrac: = = * bot
else: = R(numerator(top)) = R(denominator(top)) * bot

Thank you so much.

edit retag flag offensive close merge delete


Welcome to Ask Sage! Thank you for your question!

slelievre gravatar imageslelievre ( 2018-09-21 22:05:36 +0100 )edit

Is this from the file group_prover.sage of the seqp256k1 repository of the bitcoin-core organisation? Or did you come across this code somewhere else?

slelievre gravatar imageslelievre ( 2018-09-21 22:47:45 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-09-21 22:05:06 +0100

slelievre gravatar image

Given a polynomial ring R, one wants to be able to define elements in its fraction field Q.

Here, this is achieved by creating a class fastfrac where an element in the fraction field is stored as an object f in this class, with a numerator stored as and a denominator stored as (the numerator and denominator are the "top" and "bottom" of the fraction).

The __init__ method of this class fastfrac is built such that:

  • calling fastfrac(a, b), with a and b in R, will return a fraction representing a/b, so it will have top equal to a and bot equal to b;

  • this should also work if a is already in Q; in that case, imagining that a = c/d with c, d in R, the fraction f equal to a/b should be equal to (c/d)/b, that is, c/(d*b);

    the implementation is split into two subcases:

    • if a is in the class fastfrac, this means the top of a/b should be and its bot should be * b;

    • otherwise (maybe a belongs to some other implementation of the fraction field), we access the top and bottom of a by numerator(a) and denominator(b) and then make sure to transform them into elements of R by doing R(numerator(a)) and R(denominator(a)); so f has top equal to R(numerator(a)) and bottom equal to R(denominator(a)) * b;

  • finally, one also wants to be able to call fastfrac(a) instead of fastfrac(a, 1); so if fastfrac is called with just one argument (playing the role of a), an extra argument equal to 1 (playing the role of b) is inserted.

The if/elif/else clause in the __init__ method of the class fastfrac corresponds to the first list item and the two subitems of the second list item from the explanation.

The first part (the "if") corresponds to a in ZZ or R, where ZZ is for the case of fractions of type 1/b. The "elif" is for a already in the class fastfrac; the "else" is for a already in the fraction field but not in the class fastfrac.

Compared to this explanation, a and b are called top and bot in the code, so we have and instead of and, and we have bot instead of b.

The presence of the optional argument bot with a default value of 1 takes care of the last item in the wishlist.

Let us illustrate using polynomials in one variable, for simplicity.


sage: R.<x> = PolynomialRing(QQ)


sage: class fastfrac:
....:   def __init__(self, top, bot=1):
....:     if parent(top) == ZZ or parent(top) == R:
....: = R(top)
....: = R(bot)
....:     elif top.__class__ == fastfrac:
....: =
....: = * bot
....:     else:
....: = R(numerator(top))
....: = R(denominator(top)) * bot

Then with

sage: a = x^2 + 1
sage: b = x^3 - 2

one can define their quotient as

sage: f = fastfrac(a, b)

and recover the "top" and "bottom" of the fraction:

x^2 + 1
x^3 - 2

By contrast we might have tried to produce the quotient by dividing a by b:

sage: g = a/b

and then we can't get the "top" and "bottom" in the same way:

Traceback (most recent call last)
AttributeError: 'FractionFieldElement_1poly_field' object has no attribute 'top'

This is because the way it was defined, g belongs to Sage's implementation of the fraction field of R, and not to the class fastfrac:

sage: g.parent()
Fraction Field of Univariate Polynomial Ring in x over Rational Field

But nothing to worry about, we can convert g to the class fastfrac easily:

sage: h = fastfrac(g)

and then we get, as expected:

x^2 + 1
x^3 - 2
edit flag offensive delete link more


OHHHHHH!!! Really soo soooo thanks!!! but... one more things, I put here -> ( a question, and I know you would know answer me. Really thank you so much!!

ZacariasSatrustegui gravatar imageZacariasSatrustegui ( 2018-09-22 10:55:06 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-09-21 11:21:12 +0100

Seen: 427 times

Last updated: Sep 21 '18