当前位置: 首页>>代码示例>>Python>>正文


Python ZZ.random_element方法代码示例

本文整理汇总了Python中sage.rings.all.ZZ.random_element方法的典型用法代码示例。如果您正苦于以下问题:Python ZZ.random_element方法的具体用法?Python ZZ.random_element怎么用?Python ZZ.random_element使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sage.rings.all.ZZ的用法示例。


在下文中一共展示了ZZ.random_element方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: random_element

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ 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)
开发者ID:sagemath,项目名称:sage,代码行数:29,代码来源:qmodnz.py

示例2: __init__

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ import random_element [as 别名]
    def __init__(self, params, asym=False):
        # set parameters
        (self.alpha, self.beta, self.rho, self.rho_f, self.eta, self.bound, self.n, self.k) = params

        self.x0 = ZZ(1)
        
        self.primes = [random_prime(2**self.eta, lbound = 2**(self.eta - 1), proof=False) for i in range(self.n)]
        primes = self.primes
        
        self.x0 = prod(primes)

        # generate CRT coefficients
        self.coeff = [ZZ((self.x0/p_i) * ZZ(Zmod(p_i)(self.x0/p_i)**(-1))) for p_i in primes]

        # generate secret g_i
        self.g = [random_prime(2**self.alpha, proof=False) for i in range(self.n)]

        # generate zs and zs^(-1)
        z = []
        zinv = []

        # generate z and z^(-1)
        if not asym:
            while True:
                z = ZZ.random_element(self.x0)  
                try:
                    zinv = ZZ(Zmod(self.x0)(z)**(-1))
                    break
                except ZeroDivisionError:
                    ''' Error occurred, retry sampling '''

            z, self.zinv = zip(*[(z,zinv) for i in range(self.k)])

        else: # asymmetric version
            for i in range(self.k):
                while True:
                    z_i = ZZ.random_element(self.x0)  
                    try:
                        zinv_i = ZZ(Zmod(self.x0)(z_i)**(-1))
                        break
                    except ZeroDivisionError:
                        ''' Error occurred, retry sampling '''

                z.append(z_i)
                zinv.append(zinv_i)

            self.zinv = zinv

        # generate p_zt
        zk = Zmod(self.x0)(1)
        self.p_zt = 0
        for z_i in z:
            zk *= Zmod(self.x0)(z_i)
        for i in range(self.n):
            self.p_zt += Zmod(self.x0)(ZZ(Zmod(self.primes[i])(self.g[i])**(-1) * Zmod(self.primes[i])(zk)) * ZZ.random_element(2**self.beta) * (self.x0/self.primes[i]))

        self.p_zt = Zmod(self.x0)(self.p_zt)
开发者ID:zrathustra,项目名称:mmap,代码行数:59,代码来源:clt.py

示例3: random_solution

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ import random_element [as 别名]
def random_solution(B,K):
    r"""
    Returns a random solution in non-negative integers to the equation `a_1 + 2
    a_2 + 3 a_3 + ... + B a_B = K`, using a greedy algorithm.

    Note that this is *much* faster than using
    ``WeightedIntegerVectors.random_element()``.

    INPUT:

    - ``B``, ``K`` -- non-negative integers.

    OUTPUT:

    - list.

    EXAMPLES::

        sage: from sage.modular.overconvergent.hecke_series import random_solution
        sage: random_solution(5,10)
        [1, 1, 1, 1, 0]
    """
    a = []
    for i in xrange(B,1,-1):
        ai = ZZ.random_element((K // i) + 1)
        a.append(ai)
        K = K - ai*i
    a.append(K)
    a.reverse()

    return a
开发者ID:aaditya-thakkar,项目名称:sage,代码行数:33,代码来源:hecke_series.py

示例4: encode

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ import random_element [as 别名]
    def encode(self, m, S):
        ''' encodes a vector m (in ZZ^n) to index set S '''

        c = Zmod(self.x0)(0)

        for i in range(self.n):
            r_i = ZZ.random_element(2**self.rho)
            c += Zmod(self.x0)((m[i] + self.g[i] * r_i) * self.coeff[i])

        zinv = prod([self.zinv[i] for i in S])

        return Zmod(self.x0)(c * zinv)
开发者ID:zrathustra,项目名称:mmap,代码行数:14,代码来源:clt.py

示例5: probable_pivot_columns

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ import random_element [as 别名]
def probable_pivot_columns(A):
    """
    INPUT:
        A -- a matrix
    OUTPUT:
        a tuple of integers

    EXAMPLES:
        sage: import sage.matrix.matrix_integer_dense_hnf as matrix_integer_dense_hnf
        sage: a = matrix(ZZ,3,[0, -1, -1, 0, -20, 1, 0, 1, 2])
        sage: a
        [  0  -1  -1]
        [  0 -20   1]
        [  0   1   2]
        sage: matrix_integer_dense_hnf.probable_pivot_columns(a)
        (1, 2)
    """
    p = ZZ.random_element(10007, 46000).next_prime()
    pivots = A._reduce(p).pivots()
    return pivots
开发者ID:Etn40ff,项目名称:sage,代码行数:22,代码来源:matrix_integer_dense_hnf.py

示例6: sample

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ import random_element [as 别名]
    def sample(self, S):
        ''' sample an element at index set S '''
        m = [ZZ.random_element(self.g[i]) for i in range(self.n)]

        return self.encode(m, S)
开发者ID:zrathustra,项目名称:mmap,代码行数:7,代码来源:clt.py

示例7: random_new_basis_modp

# 需要导入模块: from sage.rings.all import ZZ [as 别名]
# 或者: from sage.rings.all.ZZ import random_element [as 别名]
def random_new_basis_modp(N,p,k,LWBModp,TotalBasisModp,elldash,bound):
    r"""
    Returns ``NewBasisCode``. Here `NewBasisCode` is a list of lists of lists
    ``[j,a]``. This encodes a choice of basis for the ith complementary space
    `W_i`, as explained in the documentation for the function
    :func:`complementary_spaces_modp`.

    INPUT:

    - ``N`` -- positive integer at least 2 and not divisible by p (level).
    - ``p`` -- prime at least 5.
    - ``k`` -- non-negative integer.
    - ``LWBModp`` -- list of list of q-expansions modulo
      `(p,q^\text{elldash})`.
    - ``TotalBasisModp`` -- matrix over GF(p).
    - ``elldash`` - positive integer.
    - ``bound`` -- positive even integer (twice the length of the list
      ``LWBModp``).

    OUTPUT:

    - A list of lists of lists ``[j,a]``.

    .. note::

        As well as having a non-trivial return value, this function also
        modifies the input matrix ``TotalBasisModp``.

    EXAMPLES::

        sage: from sage.modular.overconvergent.hecke_series import random_low_weight_bases, complementary_spaces_modp
        sage: LWB = random_low_weight_bases(2,5,2,4,6)
        sage: LWBModp = [ [f.change_ring(GF(5)) for f in x] for x in LWB]
        sage: complementary_spaces_modp(2,5,2,3,4,LWBModp,4) # random, indirect doctest
        [[[[0, 0]]], [[[0, 0], [1, 1]]], [[[0, 0], [1, 0], [1, 1]]], [[[0, 0], [1, 0], [1, 1], [1, 1]]]]

    """

    R = LWBModp[0][0].parent()

    # Case k0 + i(p-1) = 0 + 0(p-1) = 0

    if k == 0:
        TotalBasisModp[0,0] = 1
        return [[]]

    # Case k = k0 + i(p-1) > 0

    di = dimension_modular_forms(N, k)
    diminus1 = dimension_modular_forms(N, k-(p-1))
    mi = di - diminus1

    NewBasisCode = []
    rk = diminus1
    for i in xrange(1,mi+1):
        while (rk < diminus1 + i):
            # take random product of basis elements
            exps = random_solution(bound // 2, k // 2)
            TotalBasisi = R(1)
            TotalBasisiCode = []
            for j in xrange(len(exps)):
                for l in xrange(exps[j]):
                    a = ZZ.random_element(len(LWBModp[j]))
                    TotalBasisi = TotalBasisi*LWBModp[j][a]
                    TotalBasisiCode.append([j,a])
            TotalBasisModp[rk] = [TotalBasisi[j] for j in range(elldash)]
            TotalBasisModp.echelonize()
            rk = TotalBasisModp.rank()
        NewBasisCode.append(TotalBasisiCode) # this choice increased the rank

    return NewBasisCode
开发者ID:aaditya-thakkar,项目名称:sage,代码行数:73,代码来源:hecke_series.py


注:本文中的sage.rings.all.ZZ.random_element方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。