Пример #1
0
def add_vector(vector, tag=0, i0=None, i1=None):
    if not tag:
        return
    p = vector.p
    space = vector.space
    if isinstance(tag, str) and len(tag) == 1:
        if tag in "ABCTXZYDEIJUS0":
            space.additems_sparse(vector, tuple_to_sparse(p, tag, i0, i1))
        elif tag == "R":
            space.set_rand_uniform(vector)
        elif tag == "V":
            v1 = space.from_bytes(p, i0)
            space.iadd(vector, v1)
        else:
            err = "Illegal tag '%s' for MM vector"
            raise ValueError(err % tag)
    elif isinstance(tag, (AbstractMmRepVector, str)):
        add_conv_vector(vector, tag, factor=1)
    elif isinstance(tag, list):
        for x in tag:
            if isinstance(x, tuple):
                space.additems_sparse(vector, tuple_to_sparse(p, *x))
            elif isinstance(x, (str, AbstractMmRepVector)):
                add_vector(vector, x)
            elif x:
                raise TypeError(ERR_MMV_TYPE % type(x))
    elif isinstance(tag, Integral):
        add_conv_vector(vector, i0, factor=tag)
    else:
        if import_pending:
            complete_import()
        if isinstance(tag, XLeech2):
            space.additems_sparse(vector, (xleech2_to_sparse(p, tag)))
        else:
            raise TypeError(ERR_MMV_TYPE % type(tag))
Пример #2
0
    def unit(self, *data):
        """Return unit vector given by ([scalar], tag, i0, i1)

        Here (possibly a random multiple of) a basis vector is
        created according to the input parameters 
        ([scalar], tag, i0, i1). 
    
        The 'scalar' is optional and defaults to 1.
        """
        raise NotImplementedError
        return self.from_sparse(tuple_to_sparse(self.p, *data))
Пример #3
0
def sparse_subspace(*args):
    """Create a sparse representation of a subspace of MM vectors
    
    The function returns the description of a subspace of the space 
    R_p spanned by basis vectors of R_p. That description is returned
    as a sorted numpy array of type uint32. Each entry of that array 
    describes a basis vector in sparse representation (with component 
    'value' set to zero). 

    Each argument describes a subspace of R_p; and the span of all 
    these subspaces is returned. An argument may be:

    - A set or an array-like object containing integers
        Then all entries are interpreted as sparse representations
        of basis vectors, ignoring the scalar factor. Integers not
        representing basis vectors correctly are ignored.
        This feature is not implemented in the current version!!!

    - A tuple (tag, i0, i1) representing a basis vector.
        Here a tuple is interpreted as a vector in the same way as 
        in a call to an instance of class AbstractMmRepVector. 
        Note that such a call is used for generating a vector.

    - A vector v in a vector space R_p
        Here the vector space R_p must be an instance of a 
        (subclass of) class AbstractMmRepVector, and v must be a
        vector in that space. Then v describes the subspace spanned 
        by all basis vectors where the coordinate of v is not zero. 
        This feature is not implemented in the current version!!!
    """
    a = [np.empty(0, dtype=np.uint32)]
    for v in args:
        if isinstance(v, set):
            v = list(v)
        if isinstance(v, tuple):
            a.append(tuple_to_sparse(1, *v))
        elif isinstance(v, AbstractMmRepVector):
            err = "MM Vector input in function sparse_subspace not supported"
            raise (NotImplementedError, err)
            a.append(v.as_sparse())
        else:
            err = "Array input in function sparse_subspace not supported"
            raise (NotImplementedError, err)
            va = np.array(v, dtype=np.uint32).reshape(-1)
            for i, x in enumerate(va):
                va[i] = purge_sparse_entry(x)
            a.append(va)
    a_out = np.unique(np.concatenate(a) & 0xffffff00)
    if len(a_out) and a_out[0] == 0:
        a_out = a_out[1:]
    return a_out
Пример #4
0
 def __init__(self, *data):
     self.d = defaultdict(int)
     if len(data) == 0 or not data[0]:
         return
     scalar = 1
     if isinstance(data[0], vsparse):
         self.d.update(data[0].d)
         return
     if isinstance(data[0], Integral):
         scalar, data = data[0],  data[1:]
     if scalar == 0 or len(data) == 0:
         return
     if isinstance(data[0], str):
         if len(data[0]) != 1 or not data[0] in "ABCTXZYIDE0":
             err = "Illegal tag '%s' in tuple for class MMSpaceCRT"
             raise ValueError(err %  data[0]) 
     else:
         raise TypeError(ERR_CRT_TYPE % type(data[0]))          
     for t in tuple_to_sparse(255, *data):
         scalar1, t =  t & 0xff, t & 0xffffff00
         scalar1 = scalar1 if scalar1 < 128 else scalar1 - 255
         self.d[t] += scalar * scalar1
Пример #5
0
def value_from_ploop(ploop=0, cocode=None, *args):
    c = Cocode(cocode).ord if cocode else 0
    if isinstance(ploop, Integral):
        d = ploop & 0x1ffffff
    elif isinstance(ploop, PLoop):
        d = ploop.value & 0x1fff
        d = (d << 12) ^ mat24.ploop_theta(d)
    elif isinstance(ploop, XLeech2):
        d = ploop.value
    elif isinstance(ploop, Cocode):
        d = ploop.value & 0xfff
    elif isinstance(ploop, AbstractMMGroupWord):
        d = MM_to_Q_x0(ploop)
    elif isinstance(ploop, str):
        if len(ploop) == 1 and ploop in "BCTXES":
            d = 0
            a = tuple_to_sparse(0xff, ploop, cocode, *args)
            if len(a) == 1:
                a0 = a[0]
                d = mm_aux_index_sparse_to_leech2(a0)
                a0 &= 0xff
                if a0 == 0xfe:
                    d ^= 0x1000000
                elif a0 != 1:
                    d = 0
            if d:
                return d
        if ploop == "r":
            if cocode is None:
                return randint(0, 0x1ffffff)
            elif cocode in [0, 2, 3, 4]:
                return rand_xleech2_type(cocode)
        raise ValueError(ERR_XL_TUPLE)
    else:
        return TypeError(ERR_XL_TYPE % type(ploop))
    return d ^ c
Пример #6
0
 def iadd_tuple(self, v1, t):
     """Compute v += self.unit(*t), return v"""
     raise NotImplementedError
     v2 = tuple_to_sparse(v1.p, *t)
     return self.additems_sparse(v1, v2)