Пример #1
0
    def __new__(cls, ps): # automagically class method
        """Create a descriptor for a type of octet-based block.

        Required argument, ps, is an initial segment of the infinite sequence of
        primes.  The new object is a sorted tuple of all the naturals, coprime
        to every p in ps, less than the product of the ps.  This product is
        saved as .modulus on the new object.  The new object, as per the
        semantics of __new__, is returned.

        (An arbitrary sequence of distinct primes could be used analogously, but
        the present implementation exploits certain simplifications arising from
        using an initial one.  In any case, the compression ratio gets a factor
        of 1-1/p from each prime, which is most efficient if p is small; so
        using smaller primes is better than using larger ones, making an initial
        segment more efficient than any alternative of the same length.)\n"""

        assert tuple(ps[:3]) == (2, 3, 5), \
               "I need at least the first three primes to work with octets"
        # ... actually, 17 and arbitrary others would suffice without them; and
        # 2's not crucial.  But I want an initial chunk of primes, anyway.

        mod, vals = coprimes(ps)
        self = Tuple.__new__(cls, vals)
        self.modulus = mod
        return self
Пример #2
0
    def __new__(cls, ps):  # automagically class method
        """Create a descriptor for a type of octet-based block.

        Required argument, ps, is an initial segment of the infinite sequence of
        primes.  The new object is a sorted tuple of all the naturals, coprime
        to every p in ps, less than the product of the ps.  This product is
        saved as .modulus on the new object.  The new object, as per the
        semantics of __new__, is returned.

        (An arbitrary sequence of distinct primes could be used analogously, but
        the present implementation exploits certain simplifications arising from
        using an initial one.  In any case, the compression ratio gets a factor
        of 1-1/p from each prime, which is most efficient if p is small; so
        using smaller primes is better than using larger ones, making an initial
        segment more efficient than any alternative of the same length.)\n"""

        assert tuple(ps[:3]) == (2, 3, 5), \
               "I need at least the first three primes to work with octets"
        # ... actually, 17 and arbitrary others would suffice without them; and
        # 2's not crucial.  But I want an initial chunk of primes, anyway.

        mod, vals = coprimes(ps)
        self = Tuple.__new__(cls, vals)
        self.modulus = mod
        return self
Пример #3
0
    def __new__(cls, perm): # automagically an @staticmethod
        """Create a permutation.

        Single argument must be a sequence of natural numbers in which no number
        is repeated and every natural less than each entry is present in the
        sequence.  Raises ValueError otherwise; see .isa() if you only want to
        test validity.  For convenience constructors, see .identity() and
        .fromSwaps().\n"""
        perm = tuple(perm) # so we only read perm once, in case it's an iterator.
        if not cls.isa(perm):
            raise ValueError('Is not a permutation', perm)
        return Tuple.__new__(cls, perm)
Пример #4
0
    def __new__(cls, perm):  # automagically an @staticmethod
        """Create a permutation.

        Single argument must be a sequence of natural numbers in which no number
        is repeated and every natural less than each entry is present in the
        sequence.  Raises ValueError otherwise; see .isa() if you only want to
        test validity.  For convenience constructors, see .identity() and
        .fromSwaps().\n"""
        perm = tuple(
            perm)  # so we only read perm once, in case it's an iterator.
        if not cls.isa(perm):
            raise ValueError('Is not a permutation', perm)
        return Tuple.__new__(cls, perm)