本文整理汇总了Python中sage.rings.all.QQ.random_element方法的典型用法代码示例。如果您正苦于以下问题:Python QQ.random_element方法的具体用法?Python QQ.random_element怎么用?Python QQ.random_element使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.rings.all.QQ
的用法示例。
在下文中一共展示了QQ.random_element方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_commutes
# 需要导入模块: from sage.rings.all import QQ [as 别名]
# 或者: from sage.rings.all.QQ import random_element [as 别名]
def test_add_commutes(trials, verbose=False):
r"""
This is a simple demonstration of the :func:`random_testing` decorator and
its recommended usage.
We test that addition is commutative over rationals.
EXAMPLES::
sage: from sage.misc.random_testing import test_add_commutes
sage: test_add_commutes(2, verbose=True, seed=0)
a == -4, b == 0 ...
Passes!
a == -1/2, b == -1/95 ...
Passes!
sage: test_add_commutes(10)
sage: test_add_commutes(1000) # long time
"""
from sage.rings.all import QQ
for _ in xrange(trials):
a = QQ.random_element()
b = QQ.random_element()
if verbose:
print "a == %s, b == %s ..." % (a, b)
assert(a+b == b+a)
if verbose:
print "Passes!"
示例2: random_element
# 需要导入模块: from sage.rings.all import QQ [as 别名]
# 或者: from sage.rings.all.QQ import random_element [as 别名]
def random_element(self):
r"""
Return a random element of `\Q/n\Z`.
The denominator is selected
using the ``1/n`` distribution on integers, modified to return
a positive value. The numerator is then selected uniformly.
EXAMPLES::
sage: G = QQ/(6*ZZ)
sage: G.random_element()
47/16
sage: G.random_element()
1
sage: G.random_element()
3/5
"""
if self.n == 0:
return self(QQ.random_element())
d = ZZ.random_element()
if d >= 0:
d = 2 * d + 1
else:
d = -2 * d
n = ZZ.random_element((self.n * d).ceil())
return self(n / d)
示例3: test_add_is_mul
# 需要导入模块: from sage.rings.all import QQ [as 别名]
# 或者: from sage.rings.all.QQ import random_element [as 别名]
def test_add_is_mul(trials, verbose=False):
r"""
This example demonstrates a failing :func:`random_testing` test,
and shows how to reproduce the error.
DO NOT USE THIS AS AN EXAMPLE OF HOW TO USE
:func:`random_testing`! Instead, look at
:func:`sage.misc.random_testing.test_add_commutes`.
We test that ``a+b == a*b``, for *a*, *b* rational. This is of
course false, so the test will almost always fail.
EXAMPLES::
sage: from sage.misc.random_testing import test_add_is_mul
We start by testing that we get reproducible results when setting
*seed* to 0.
::
sage: test_add_is_mul(2, verbose=True, seed=0)
a == -4, b == 0 ...
Random testing has revealed a problem in test_add_is_mul
Please report this bug! You may be the first
person in the world to have seen this problem.
Please include this random seed in your bug report:
Random seed: 0
AssertionError()
Normally in a ``@random_testing`` doctest, we would leave off the
``verbose=True`` and the ``# random``. We put it in here so that we can
verify that we are seeing the exact same error when we reproduce
the error below.
::
sage: test_add_is_mul(10, verbose=True) # random
a == -2/7, b == 1 ...
Random testing has revealed a problem in test_add_is_mul
Please report this bug! You may be the first
person in the world to have seen this problem.
Please include this random seed in your bug report:
Random seed: 216390410596009428782506007128692114173
AssertionError()
OK, now assume that some user has reported a
:func:`test_add_is_mul` failure. We can specify the same
*random_seed* that was found in the bug report, and we will get the
exact same failure so that we can debug the "problem".
::
sage: test_add_is_mul(10, verbose=True, seed=216390410596009428782506007128692114173)
a == -2/7, b == 1 ...
Random testing has revealed a problem in test_add_is_mul
Please report this bug! You may be the first
person in the world to have seen this problem.
Please include this random seed in your bug report:
Random seed: 216390410596009428782506007128692114173
AssertionError()
"""
from sage.rings.all import QQ
for _ in xrange(trials):
a = QQ.random_element()
b = QQ.random_element()
if verbose:
print "a == %s, b == %s ..." % (a, b)
assert(a+b == a*b)
if verbose:
print "Passes!"