Ejemplo n.º 1
0
    def divmod(self, a: 'GaussInt',
               b: 'GaussInt') -> Tuple['GaussInt', 'GaussInt']:
        """Returns the quotient and the remainder of the euclidean division of two gaussian integers
        """
        p_a = Polynomial([a.x, a.y], IntegerRing())
        p_b = Polynomial([b.x, b.y], IntegerRing())
        q, r = p_a.long_division_reversed(p_b)
        base = self.base_polynomial
        q %= base
        r %= base

        return self.element_from_polynomial(q), self.element_from_polynomial(r)
Ejemplo n.º 2
0
 def polynomial_from_element(self, e: 'FFElement') -> Polynomial:
     """Returns a polynomial whose coefficients are those of the element
     e.g. `(1+j+j^2)` would yield `1 + X + X^2`
     """
     return Polynomial(e.vector,
                       self.prime_field,
                       indeterminate=self.root_symbol)
Ejemplo n.º 3
0
 def polynomial(self,
                *args,
                indeterminate: Optional[str] = 'X') -> Polynomial:
     """Returns a polynomial built from a list of coefficients."""
     coefficients = [c for c in args]
     return Polynomial(coefficients,
                       base_field=self,
                       indeterminate=indeterminate)
Ejemplo n.º 4
0
    def testStrAndRepr(self):
        """Check representations of a polynomial over a prime field
        """
        f5 = self.f5
        p1 = f5.polynomial(-2, 1, 2, indeterminate='z')
        p2 = Polynomial([-2, 1, 2], f5, indeterminate='z')
        self.assertTrue(p1 == p2 == eval(repr(p1)))

        self.assertEqual(str(p1), '-2 + z + 2z^2')
Ejemplo n.º 5
0
    def test1(self):
        """Extended checks for monic and non-monic polynomials in Z
        """
        p = Polynomial([0, 1, 1], IntegerRing())
        self.assertTrue(str(p) == 'X + X^2')

        f5 = PrimeField(5)
        p = f5.linear_polynomial(f5(-2))
        self.assertTrue(str(p) == '2 + X')

        p = f5.polynomial(0, -1, 1)
        self.assertTrue(str(p) == '-X + X^2')

        p = symbolic_polynomial('-2 - X^16', PrimeField(5))
        self.assertTrue(str(p) == '-2 - X^16')
Ejemplo n.º 6
0
    def polynomial(self,
                   *args,
                   indeterminate: Optional[str] = 'X') -> Polynomial:
        """Returns a polynomial from a list of coefficients. The coefficients are converted to `FFElement`
        """
        coefficients = []
        for c in args:
            if isinstance(c, (tuple, list)):
                coefficients.append(self.element(c))
            elif isinstance(c, FFElement):
                coefficients.append(c)
            else:
                # shall be an integer or, at least an atom (e.g. a PFElement)
                c = [c] + [self.prime_field.zero] * (self.dimension - 1)
                coefficients.append(self.element(c))

        return Polynomial(coefficients,
                          base_field=self,
                          indeterminate=indeterminate)
Ejemplo n.º 7
0
 def __init__(self):
     self.root_symbol = 'i'
     self.base_polynomial = Polynomial(
         [1, 0, 1], IntegerRing())  # X2 + 1 irreducible over Z
Ejemplo n.º 8
0
 def polynomial_from_element(self, e: 'GaussInt') -> Polynomial:
     """Returns the polynomial `yZ + x` from the element `'(x, y)`
      """
     return Polynomial([e.x, e.y],
                       IntegerRing(),
                       indeterminate=self.root_symbol)
Ejemplo n.º 9
0
 def polynomial(self,
                coefficients: Sequence,
                indeterminate='z') -> Polynomial:
     """Returns a polynomial from its coefficients
     """
     return Polynomial(coefficients, self, indeterminate)
Ejemplo n.º 10
0
 def polynomial(self, *args, indeterminate: str = 'X') -> Polynomial:
     """Returns a polynomial from the arguments
     """
     return Polynomial([self.element(c) for c in args],
                       base_field=self,
                       indeterminate=indeterminate)
Ejemplo n.º 11
0
 def setUp(self):
     self.f25 = FiniteField(5, 2,
                            Polynomial([1, 1, 1], base_field=PrimeField(5)))
Ejemplo n.º 12
0
 def setUp(self):
     self.f27 = FiniteField(
         3, 3, Polynomial([-1, -1, 0, 1], base_field=PrimeField(3)))