Sage / Python: 来自非空生成器的空元组
Sage / Python: Empty tuple from non-empty generator
Sage 的新手,我 运行 遇到了这段代码的以下问题:
abc2 = ((x * d).numerator() for x in abc)
for x in abc2:
show (type(x))
show (gcd (x.coefficients()))
g = (gcd (x.coefficients()) for x in abc2)
print ("g =", tuple(g))
打印以下输出:
<class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
177282774041995797378340812360777728/101
<class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
177282774041995797378340812360777728
<class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
177282774041995797378340812360777728
g = ()
正如您可以从循环中的打印中推断的那样,按照 gcd (x.coefficients())
的预期计算了 3 个 gcds。但是,当循环后在生成器中使用 gcd (x.coefficients())
时,生成器是空的,我希望它有 3 个元素?
我在这里错过了什么?我检查了生成的.py
文件,那部分没有改变,所以我对Python的理解是错误的?
$ sage --version
SageMath version 9.0, Release Date: 2020-01-01
仅供参考,这是完整的 Sage 代码,上面的片段从第 24 行开始:
#!/usr/bin/env sage
R.<a,b,c> = QQ[]
def to_xy (abc, E):
(a2, a1) = E
(a, b, c) = abc
d = 6*a + 6*b - c
x = -28 * (a + b + 2*c) / d
y = 364 * (a - b) / d
return (x, y)
def to2_abc (P, E):
""" Get back from a point P = (x,y) on
E: y^2 = x^3 + a2 x^2 + a1 x to (a,b,c).
"""
(x,y) = P
abc = (-56/101 - 12 * x, 56 - x + y, 56 - x - y)
d = lcm (x.denominator() for x in abc)
abc2 = ((x * d).numerator() for x in abc)
for x in abc2:
show (type(x))
show (gcd (x.coefficients()))
g = (gcd (x.coefficients()) for x in abc2)
print ("g =", tuple(g))
def ECadd (P, E):
(xP, yP) = P
(a2,a1) = E
# Get to short Weierstrass form E': y^2 = x^2 + A x + B.
k = -a2 / 3
A = 3*k*k + 2*a2*k + a1
# B = (k*k + a2*k + a1) * k
(xS, yS) = (xP-k, yP)
# Add S to itself on E'.
s = (3 * xS*xS + A) / (2 * yS)
xSS = s*s - xS - xS
ySS = -yS - s * (xSS - xS)
# Return P+P on E.
(xPP, yPP) = (xSS+k, ySS)
return (xPP, yPP)
E = (109, 224)
P = to_xy ((a,b,c), E)
P2 = ECadd (P, E)
to2_abc (P2, E)
当你遍历生成器时,当生成器耗尽时迭代停止,所以当你第二次遍历它时,没有剩下的元素。
如果您需要多次迭代数据,请先将其转换为另一种类型,例如列表,然后再对其进行迭代。
Sage 的新手,我 运行 遇到了这段代码的以下问题:
abc2 = ((x * d).numerator() for x in abc)
for x in abc2:
show (type(x))
show (gcd (x.coefficients()))
g = (gcd (x.coefficients()) for x in abc2)
print ("g =", tuple(g))
打印以下输出:
<class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
177282774041995797378340812360777728/101
<class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
177282774041995797378340812360777728
<class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
177282774041995797378340812360777728
g = ()
正如您可以从循环中的打印中推断的那样,按照 gcd (x.coefficients())
的预期计算了 3 个 gcds。但是,当循环后在生成器中使用 gcd (x.coefficients())
时,生成器是空的,我希望它有 3 个元素?
我在这里错过了什么?我检查了生成的.py
文件,那部分没有改变,所以我对Python的理解是错误的?
$ sage --version
SageMath version 9.0, Release Date: 2020-01-01
仅供参考,这是完整的 Sage 代码,上面的片段从第 24 行开始:
#!/usr/bin/env sage
R.<a,b,c> = QQ[]
def to_xy (abc, E):
(a2, a1) = E
(a, b, c) = abc
d = 6*a + 6*b - c
x = -28 * (a + b + 2*c) / d
y = 364 * (a - b) / d
return (x, y)
def to2_abc (P, E):
""" Get back from a point P = (x,y) on
E: y^2 = x^3 + a2 x^2 + a1 x to (a,b,c).
"""
(x,y) = P
abc = (-56/101 - 12 * x, 56 - x + y, 56 - x - y)
d = lcm (x.denominator() for x in abc)
abc2 = ((x * d).numerator() for x in abc)
for x in abc2:
show (type(x))
show (gcd (x.coefficients()))
g = (gcd (x.coefficients()) for x in abc2)
print ("g =", tuple(g))
def ECadd (P, E):
(xP, yP) = P
(a2,a1) = E
# Get to short Weierstrass form E': y^2 = x^2 + A x + B.
k = -a2 / 3
A = 3*k*k + 2*a2*k + a1
# B = (k*k + a2*k + a1) * k
(xS, yS) = (xP-k, yP)
# Add S to itself on E'.
s = (3 * xS*xS + A) / (2 * yS)
xSS = s*s - xS - xS
ySS = -yS - s * (xSS - xS)
# Return P+P on E.
(xPP, yPP) = (xSS+k, ySS)
return (xPP, yPP)
E = (109, 224)
P = to_xy ((a,b,c), E)
P2 = ECadd (P, E)
to2_abc (P2, E)
当你遍历生成器时,当生成器耗尽时迭代停止,所以当你第二次遍历它时,没有剩下的元素。
如果您需要多次迭代数据,请先将其转换为另一种类型,例如列表,然后再对其进行迭代。