本文整理汇总了Python中sage.all.PolynomialRing.random_element方法的典型用法代码示例。如果您正苦于以下问题:Python PolynomialRing.random_element方法的具体用法?Python PolynomialRing.random_element怎么用?Python PolynomialRing.random_element使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.all.PolynomialRing
的用法示例。
在下文中一共展示了PolynomialRing.random_element方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_karatsuba_multiplication
# 需要导入模块: from sage.all import PolynomialRing [as 别名]
# 或者: from sage.all.PolynomialRing import random_element [as 别名]
def test_karatsuba_multiplication(base_ring, maxdeg1, maxdeg2,
ref_mul=lambda f, g: f._mul_generic(g), base_ring_random_elt_args=[],
numtests=10, verbose=False):
"""
Test univariate karatsuba multiplication against other multiplication algorithms.
EXAMPLES:
First check that random tests are reproducible::
sage: import sage.rings.tests
sage: sage.rings.tests.test_karatsuba_multiplication(ZZ, 6, 5, verbose=True, seed=42)
test_karatsuba_multiplication: ring=Univariate Polynomial Ring in x over Integer Ring, threshold=2
(2*x^6 - x^5 - x^4 - 3*x^3 + 4*x^2 + 4*x + 1)*(4*x^4 + x^3 - 2*x^2 - 20*x + 3)
(16*x^2)*(x^2 - 41*x + 1)
(-x + 1)*(x^2 + 2*x + 8)
(-x^6 - x^4 - 8*x^3 - x^2 - 4*x + 3)*(-x^3 - x^2)
(2*x^2 + x + 1)*(x^4 - x^3 + 3*x^2 - x)
(-x^3 + x^2 + x + 1)*(4*x^2 + 76*x - 1)
(6*x + 1)*(-5*x - 1)
(-x^3 + 4*x^2 + x)*(-x^5 + 3*x^4 - 2*x + 5)
(-x^5 + 4*x^4 + x^3 + 21*x^2 + x)*(14*x^3)
(2*x + 1)*(12*x^3 - 12)
Test Karatsuba multiplication of polynomials of small degree over some common rings::
sage: for C in [QQ, ZZ[I], ZZ[I, sqrt(2)], GF(49, 'a'), MatrixSpace(GF(17), 3)]:
....: sage.rings.tests.test_karatsuba_multiplication(C, 10, 10)
Zero-tests over ``QQbar`` are currently very slow, so we test only very small examples::
sage.rings.tests.test_karatsuba_multiplication(QQbar, 3, 3, numtests=2)
Larger degrees (over ``ZZ``, using FLINT)::
sage: sage.rings.tests.test_karatsuba_multiplication(ZZ, 1000, 1000, ref_mul=lambda f,g: f*g, base_ring_random_elt_args=[1000])
Some more aggressive tests::
sage: for C in [QQ, ZZ[I], ZZ[I, sqrt(2)], GF(49, 'a'), MatrixSpace(GF(17), 3)]:
....: sage.rings.tests.test_karatsuba_multiplication(C, 10, 10) # long time
sage: sage.rings.tests.test_karatsuba_multiplication(ZZ, 10000, 10000, ref_mul=lambda f,g: f*g, base_ring_random_elt_args=[100000])
"""
from sage.all import randint, PolynomialRing
threshold = randint(0, min(maxdeg1,maxdeg2))
R = PolynomialRing(base_ring, 'x')
if verbose:
print "test_karatsuba_multiplication: ring={}, threshold={}".format(R, threshold)
for i in range(numtests):
f = R.random_element(randint(0, maxdeg1), *base_ring_random_elt_args)
g = R.random_element(randint(0, maxdeg2), *base_ring_random_elt_args)
if verbose:
print " ({})*({})".format(f, g)
if ref_mul(f, g) - f._mul_karatsuba(g, threshold) != 0:
raise ValueError("Multiplication failed")
return