本文整理汇总了Python中sage.rings.arith.is_prime函数的典型用法代码示例。如果您正苦于以下问题:Python is_prime函数的具体用法?Python is_prime怎么用?Python is_prime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_prime函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hadamard_matrix
def hadamard_matrix(n):
"""
Tries to construct a Hadamard matrix using a combination of Paley
and Sylvester constructions.
EXAMPLES::
sage: hadamard_matrix(12).det()
2985984
sage: 12^6
2985984
sage: hadamard_matrix(2)
[ 1 1]
[ 1 -1]
sage: hadamard_matrix(8)
[ 1 1 1 1 1 1 1 1]
[ 1 -1 1 -1 1 -1 1 -1]
[ 1 1 -1 -1 1 1 -1 -1]
[ 1 -1 -1 1 1 -1 -1 1]
[ 1 1 1 1 -1 -1 -1 -1]
[ 1 -1 1 -1 -1 1 -1 1]
[ 1 1 -1 -1 -1 -1 1 1]
[ 1 -1 -1 1 -1 1 1 -1]
sage: hadamard_matrix(8).det() == 8^4
True
"""
if not(n % 4 == 0) and (n != 2):
raise ValueError("The Hadamard matrix of order %s does not exist" % n)
if n == 2:
return matrix([[1, 1], [1, -1]])
if is_even(n):
N = Integer(n / 2)
elif n == 1:
return matrix([1])
if is_prime(N - 1) and (N - 1) % 4 == 1:
return hadamard_matrix_paleyII(n)
elif n == 4 or n % 8 == 0:
had = hadamard_matrix(Integer(n / 2))
chad1 = matrix([list(r) + list(r) for r in had.rows()])
mhad = (-1) * had
R = len(had.rows())
chad2 = matrix([list(had.rows()[i]) + list(mhad.rows()[i])
for i in range(R)])
return chad1.stack(chad2)
elif is_prime(N - 1) and (N - 1) % 4 == 3:
return hadamard_matrix_paleyI(n)
else:
raise ValueError("The Hadamard matrix of order %s is not yet implemented." % n)
示例2: __init__
def __init__(self, p, name=None, check=True):
"""
Return a new finite field of order $p$ where $p$ is prime.
INPUT:
- p -- an integer >= 2
- ``name`` -- ignored
- ``check`` -- bool (default: True); if False, do not
check p for primality
EXAMPLES::
sage: FiniteField(3)
Finite Field of size 3
sage: FiniteField(next_prime(1000))
Finite Field of size 1009
"""
p = integer.Integer(p)
if check and not arith.is_prime(p):
raise ArithmeticError, "p must be prime"
self.__char = p
self._IntegerModRing_generic__factored_order = factorization.Factorization([(p,1)], integer.Integer(1))
self._kwargs = {}
# FiniteField_generic does nothing more than IntegerModRing_generic, and
# it saves a non trivial overhead
integer_mod_ring.IntegerModRing_generic.__init__(self, p, category = _FiniteFields)
示例3: __init__
def __init__(self, p, name=None, check=True):
"""
Return a new finite field of order `p` where `p` is prime.
INPUT:
- ``p`` -- an integer at least 2
- ``name`` -- ignored
- ``check`` -- bool (default: ``True``); if ``False``, do not
check ``p`` for primality
EXAMPLES::
sage: F = FiniteField(3); F
Finite Field of size 3
"""
p = integer.Integer(p)
if check and not arith.is_prime(p):
raise ArithmeticError("p must be prime")
self.__char = p
self._kwargs = {}
# FiniteField_generic does nothing more than IntegerModRing_generic, and
# it saves a non trivial overhead
integer_mod_ring.IntegerModRing_generic.__init__(self, p, category = _FiniteFields)
示例4: apply_sparse
def apply_sparse(self, x):
"""
Return the image of x under self. If x is not in self.domain(),
raise a TypeError.
EXAMPLES:
sage: M = ModularSymbols(17,4,-1)
sage: T = M.hecke_operator(4)
sage: T.apply_sparse(M.0)
64*[X^2,(1,8)] + 24*[X^2,(1,10)] - 9*[X^2,(1,13)] + 37*[X^2,(1,16)]
sage: [ T.apply_sparse(x) == T.hecke_module_morphism()(x) for x in M.basis() ]
[True, True, True, True]
sage: N = ModularSymbols(17,4,1)
sage: T.apply_sparse(N.0)
Traceback (most recent call last):
...
TypeError: x (=[X^2,(0,1)]) must be in Modular Symbols space of dimension 4 for Gamma_0(17) of weight 4 with sign -1 over Rational Field
"""
if x not in self.domain():
raise TypeError("x (=%s) must be in %s"%(x, self.domain()))
# old version just to check for correctness
#return self.hecke_module_morphism()(x)
p = self.index()
if is_prime(p):
H = heilbronn.HeilbronnCremona(p)
else:
H = heilbronn.HeilbronnMerel(p)
M = self.parent().module()
mod2term = M._mod2term
syms = M.manin_symbols()
K = M.base_ring()
R = M.manin_gens_to_basis()
W = R.new_matrix(nrows=1, ncols = R.nrows())
B = M.manin_basis()
#from sage.all import cputime
#t = cputime()
v = x.element()
for i in v.nonzero_positions():
for h in H:
entries = syms.apply(B[i], h)
for k, w in entries:
f, s = mod2term[k]
if s:
W[0,f] += s*K(w)*v[i]
#print 'sym', cputime(t)
#t = cputime()
ans = M( v.parent()((W * R).row(0)) )
#print 'mul', cputime(t)
#print 'density: ', len(W.nonzero_positions())/(W.nrows()*float(W.ncols()))
return ans
示例5: is_primitive
def is_primitive(self,return_base=False):
r"""
A pillowcase cover is primitive if it does not cover an other pillowcase
cover.
"""
from sage.rings.arith import is_prime
if is_prime(self.degree()):
return True
return bool(gap.IsPrimitive(self.monodromy()))
示例6: create_key_and_extra_args
def create_key_and_extra_args(self, order, name=None, modulus=None, names=None, impl=None, proof=None, **kwds):
"""
EXAMPLES::
sage: GF.create_key_and_extra_args(9, 'a')
((9, ('a',), 'conway', None, '{}', 3, 2, True), {})
sage: GF.create_key_and_extra_args(9, 'a', foo='value')
((9, ('a',), 'conway', None, "{'foo': 'value'}", 3, 2, True), {'foo': 'value'})
"""
from sage.structure.proof.all import WithProof, arithmetic
if proof is None:
proof = arithmetic()
with WithProof("arithmetic", proof):
order = int(order)
if order <= 1:
raise ValueError("the order of a finite field must be > 1.")
if arith.is_prime(order):
name = None
modulus = None
p = integer.Integer(order)
n = integer.Integer(1)
elif arith.is_prime_power(order):
if not names is None:
name = names
name = normalize_names(1, name)
p, n = arith.factor(order)[0]
if modulus is None or modulus == "default":
if exists_conway_polynomial(p, n):
modulus = "conway"
else:
if p == 2:
modulus = "minimal_weight"
else:
modulus = "random"
elif modulus == "random":
modulus += str(random.randint(0, 1 << 128))
if isinstance(modulus, (list, tuple)):
modulus = FiniteField(p)["x"](modulus)
# some classes use 'random' as the modulus to
# generate a random modulus, but we don't want
# to cache it
elif sage.rings.polynomial.polynomial_element.is_Polynomial(modulus):
modulus = modulus.change_variable_name("x")
elif not isinstance(modulus, str):
raise ValueError("Modulus parameter not understood.")
else: # Neither a prime, nor a prime power
raise ValueError("the order of a finite field must be a prime power.")
return (order, name, modulus, impl, str(kwds), p, n, proof), kwds
示例7: is_integral_domain
def is_integral_domain(self, proof = True):
"""
Return True if and only if the order of self is prime.
EXAMPLES::
sage: Integers(389).is_integral_domain()
True
sage: Integers(389^2).is_integral_domain()
False
"""
return is_prime(self.order())
示例8: hadamard_matrix_paleyI
def hadamard_matrix_paleyI(n):
"""
Implements the Paley type I construction.
EXAMPLES::
sage: sage.combinat.matrices.hadamard_matrix.hadamard_matrix_paleyI(4)
[ 1 -1 -1 -1]
[ 1 1 1 -1]
[ 1 -1 1 1]
[ 1 1 -1 1]
"""
p = n - 1
if not(is_prime(p) and (p % 4 == 3)):
raise ValueError("The order %s is not covered by the Paley type I construction." % n)
return matrix(ZZ, [[H1(i, j, p) for i in range(n)] for j in range(n)])
示例9: hadamard_matrix_paleyII
def hadamard_matrix_paleyII(n):
"""
Implements the Paley type II construction.
EXAMPLES::
sage: sage.combinat.matrices.hadamard_matrix.hadamard_matrix_paleyI(12).det()
2985984
sage: 12^6
2985984
"""
N = Integer(n/2)
p = N - 1
if not(is_prime(p) and (p % 4 == 1)):
raise ValueError("The order %s is not covered by the Paley type II construction." % n)
S = matrix(ZZ, [[H2(i, j, p) for i in range(N)] for j in range(N)])
return block_matrix([[S + 1, S - 1], [S - 1, -S - 1]])
示例10: CO_delta
def CO_delta(r,p,N,eps):
r"""
This is used as an intermediate value in computations related to
the paper of Cohen-Oesterle.
INPUT:
- ``r`` - positive integer
- ``p`` - a prime
- ``N`` - positive integer
- ``eps`` - character
OUTPUT: element of the base ring of the character
EXAMPLES::
sage: G.<eps> = DirichletGroup(7)
sage: sage.modular.dims.CO_delta(1,5,7,eps^3)
2
"""
if not is_prime(p):
raise ValueError("p must be prime")
K = eps.base_ring()
if p%4 == 3:
return K(0)
if p==2:
if r==1:
return K(1)
return K(0)
# interesting case: p=1(mod 4).
# omega is a primitive 4th root of unity mod p.
omega = (IntegerModRing(p).unit_gens()[0])**((p-1)//4)
# this n is within a p-power root of a "local" 4th root of 1 modulo p.
n = Mod(int(omega.crt(Mod(1,N//(p**r)))),N)
n = n**(p**(r-1)) # this is correct now
t = eps(n)
if t==K(1):
return K(2)
if t==K(-1):
return K(-2)
return K(0)
示例11: _element_constructor_
def _element_constructor_(self, e):
"""
TESTS::
sage: P = Sets().example()
sage: P._element_constructor_(13) == 13
True
sage: P._element_constructor_(13).parent()
Integer Ring
sage: P._element_constructor_(14)
Traceback (most recent call last):
...
AssertionError: 14 is not a prime number
"""
p = self.element_class(e)
assert is_prime(p), "%s is not a prime number"%(p)
return p
示例12: eisen
def eisen(p):
"""
Return the Eisenstein number `n` which is the numerator of
`(p-1)/12`.
INPUT:
- ``p`` - a prime
OUTPUT: Integer
EXAMPLES::
sage: [(p,sage.modular.dims.eisen(p)) for p in prime_range(24)]
[(2, 1), (3, 1), (5, 1), (7, 1), (11, 5), (13, 1), (17, 4), (19, 3), (23, 11)]
"""
if not is_prime(p):
raise ValueError, "p must be prime"
return frac(p-1,12).numerator()
示例13: create_key_and_extra_args
def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
impl=None, proof=None, **kwds):
"""
EXAMPLES::
sage: GF.create_key_and_extra_args(9, 'a')
((9, ('a',), x^2 + 2*x + 2, None, '{}', 3, 2, True), {})
sage: GF.create_key_and_extra_args(9, 'a', foo='value')
((9, ('a',), x^2 + 2*x + 2, None, "{'foo': 'value'}", 3, 2, True), {'foo': 'value'})
"""
from sage.structure.proof.all import WithProof, arithmetic
if proof is None: proof = arithmetic()
with WithProof('arithmetic', proof):
order = int(order)
if order <= 1:
raise ValueError("the order of a finite field must be > 1.")
if arith.is_prime(order):
name = None
modulus = None
p = integer.Integer(order)
n = integer.Integer(1)
elif arith.is_prime_power(order):
if not names is None: name = names
name = normalize_names(1,name)
p, n = arith.factor(order)[0]
if modulus is None or isinstance(modulus, str):
# A string specifies an algorithm to find a suitable modulus.
if modulus == "default": # for backward compatibility
modulus = None
modulus = GF(p)['x'].irreducible_element(n, algorithm=modulus)
elif isinstance(modulus, (list, tuple)):
modulus = GF(p)['x'](modulus)
elif sage.rings.polynomial.polynomial_element.is_Polynomial(modulus):
modulus = modulus.change_variable_name('x')
else:
raise TypeError("wrong type for modulus parameter")
else:
raise ValueError("the order of a finite field must be a prime power.")
return (order, name, modulus, impl, str(kwds), p, n, proof), kwds
示例14: hadamard_matrix_paleyII
def hadamard_matrix_paleyII(n):
"""
Implements the Paley type II construction.
The Paley type II case corresponds to the case `p \cong 1 \mod{4}` for a
prime `p` (see [Hora]_).
EXAMPLES::
sage: sage.combinat.matrices.hadamard_matrix.hadamard_matrix_paleyII(12).det()
2985984
sage: 12^6
2985984
We note that the method returns a normalised Hadamard matrix ::
sage: sage.combinat.matrices.hadamard_matrix.hadamard_matrix_paleyII(12)
[ 1 1 1 1 1 1| 1 1 1 1 1 1]
[ 1 1 1 -1 -1 1|-1 -1 1 -1 -1 1]
[ 1 1 1 1 -1 -1|-1 1 -1 1 -1 -1]
[ 1 -1 1 1 1 -1|-1 -1 1 -1 1 -1]
[ 1 -1 -1 1 1 1|-1 -1 -1 1 -1 1]
[ 1 1 -1 -1 1 1|-1 1 -1 -1 1 -1]
[-----------------+-----------------]
[ 1 -1 -1 -1 -1 -1|-1 1 1 1 1 1]
[ 1 -1 1 -1 -1 1| 1 -1 -1 1 1 -1]
[ 1 1 -1 1 -1 -1| 1 -1 -1 -1 1 1]
[ 1 -1 1 -1 1 -1| 1 1 -1 -1 -1 1]
[ 1 -1 -1 1 -1 1| 1 1 1 -1 -1 -1]
[ 1 1 -1 -1 1 -1| 1 -1 1 1 -1 -1]
"""
N = Integer(n/2)
p = N - 1
if not(is_prime(p) and (p % 4 == 1)):
raise ValueError("The order %s is not covered by the Paley type II construction." % n)
S = matrix(ZZ, [[H2(i, j, p) for i in range(N)] for j in range(N)])
H = block_matrix([[S + 1, S - 1], [1 - S, S + 1]])
# normalising H so that first row and column have only +1 entries.
return normalise_hadamard(H)
示例15: is_blum_prime
def is_blum_prime(n):
r"""
Determine whether or not ``n`` is a Blum prime.
INPUT:
- ``n`` a positive prime.
OUTPUT:
- ``True`` if ``n`` is a Blum prime; ``False`` otherwise.
Let `n` be a positive prime. Then `n` is a Blum prime if `n` is
congruent to 3 modulo 4, i.e. `n \equiv 3 \pmod{4}`.
EXAMPLES:
Testing some integers to see if they are Blum primes::
sage: from sage.crypto.util import is_blum_prime
sage: from sage.crypto.util import random_blum_prime
sage: is_blum_prime(101)
False
sage: is_blum_prime(7)
True
sage: p = random_blum_prime(10**3, 10**5)
sage: is_blum_prime(p)
True
"""
if n < 0:
return False
if is_prime(n):
if mod(n, 4).lift() == 3:
return True
else:
return False
else:
return False