Ejemplo n.º 1
0
 def __init__(self,
              subs,
              vals,
              shape = None,
              func = sum.__call__):
     """Create a sptensor object"""
     
     if(subs.__class__ == list):
         subs = numpy.array(subs);
     if(vals.__class__ == list):
         vals = numpy.array(vals);
     if(shape.__class__ == list):
         shape = numpy.array(shape);
         
     
     if not(tools.tt_subscheck(subs)):
         raise ValueError("Error in subscripts");
     if not(tools.tt_valscheck(vals)):
         raise ValueError("Error in values");
     if (shape != None and not tools.tt_sizecheck(shape)):
         raise ValueError("Error in shape");
     
     if(vals.size != 0 and vals.size != 1 and len(vals) != len(subs)):
         raise ValueError("Number of subscripts and values must be equal");
     
     if (shape == None):
         self.shape = tuple(subs.max(0) + 1);
     else:
         self.shape = tuple(shape);
     
     # if func is given by user
     if(func != None):
         self.func = func;
     
     if(subs.size == 0):
         nzsub = numpy.array([]);
         nzval = numpy.array([]);
     else:
         (newsub, loc) = uniquerows(subs);
         newval = numpy.ndarray([len(newsub), 1]);
         newval.fill(0);
         
         for i in range(0, len(loc)):
             newval[(int)(loc[i])] = func(vals[i], newval[(int)(loc[i])]);
     
         nzsub = newsub.tolist();
         nzval = newval.tolist();
         
         
         i = 0;
         while (i < len(nzsub)):
             if(nzval[i][0] == 0):
                 nzsub.remove(nzsub[i]);
                 nzval.remove(nzval[i]);
             else:
                 i = i+1;
             
     self.subs = numpy.array(nzsub);
     self.vals = numpy.array(nzval);        
Ejemplo n.º 2
0
 def __init__(self,
              subs,
              vals,
              shape = None,
              func = sum.__call__):
     """Create a sptensor object"""
     
     if(subs.__class__ == list):
         subs = numpy.array(subs);
     if(vals.__class__ == list):
         vals = numpy.array(vals);
     if(shape.__class__ == list):
         shape = numpy.array(shape);
         
     
     if not(tools.tt_subscheck(subs)):
         raise ValueError("Error in subscripts");
     if not(tools.tt_valscheck(vals)):
         raise ValueError("Error in values");
     if (shape != None and not tools.tt_sizecheck(shape)):
         raise ValueError("Error in shape");
     
     if(vals.size != 0 and vals.size != 1 and len(vals) != len(subs)):
         raise ValueError("Number of subscripts and values must be equal");
     
     if (shape == None):
         self.shape = tuple(subs.max(0) + 1);
     else:
         self.shape = tuple(shape);
     
     # if func is given by user
     if(func != None):
         self.func = func;
     
     if(subs.size == 0):
         nzsub = numpy.array([]);
         nzval = numpy.array([]);
     else:
         (newsub, loc) = uniquerows(subs);
         newval = numpy.ndarray([len(newsub), 1]);
         newval.fill(0);
         
         for i in range(0, len(loc)):
             newval[(int)(loc[i])] = func(vals[i], newval[(int)(loc[i])]);
     
         nzsub = newsub.tolist();
         nzval = newval.tolist();
         
         
         i = 0;
         while (i < len(nzsub)):
             if(nzval[i][0] == 0):
                 nzsub.remove(nzsub[i]);
                 nzval.remove(nzval[i]);
             else:
                 i = i+1;
             
     self.subs = numpy.array(nzsub);
     self.vals = numpy.array(nzval);        
Ejemplo n.º 3
0
    def __init__(self,
                 subs,
                 vals,
                 shape = None,
                 func = sum.__call__):
        """
        Create a sptensor object. The subs array specifies the nonzero entries
        in the tensor, with the kth row of subs corresponding to the kth entry 
        in vals.
        
        Parameters
        ----------
        subs - p x n array specifying the subscripts of nonzero entries
        vals - the corresponding value of the nonzero entries
        shape - the shape of the tensor object.
        func - accumulation function for repeated subscripts

        Returns
        -------
        out : sparse tensor object
        
        """
        
        if(subs.__class__ == list):
            subs = numpy.array(subs);
        if(vals.__class__ == list):
            vals = numpy.array(vals);
        if(shape.__class__ == list):
            shape = numpy.array(shape);
            
        
        if not(tools.tt_subscheck(subs)):
            raise ValueError("Error in subscripts");
        if not(tools.tt_valscheck(vals)):
            raise ValueError("Error in values");
        if (shape != None and not tools.tt_sizecheck(shape)):
            raise ValueError("Error in shape");
        
        if(vals.size != 0 and vals.size != 1 and len(vals) != len(subs)):
            raise ValueError("Number of subscripts and values must be equal");
        
        if (shape == None):
            self.shape = tuple(subs.max(0) + 1);
        else:
            self.shape = tuple(shape);
        
        # if func is given by user
        if(func != None):
            self.func = func;
        
        if(subs.size == 0):
            nzsub = numpy.array([]);
            nzval = numpy.array([]);
        else:
            (newsub, loc) = uniquerows(subs);
            newval = numpy.ndarray([len(newsub), 1]);
            newval.fill(0);
            
            for i in range(0, len(loc)):
                newval[(int)(loc[i])] = func(vals[i], newval[(int)(loc[i])]);
        
            nzsub = newsub.tolist();
            nzval = newval.tolist();
            
            
            i = 0;
            while (i < len(nzsub)):
                if(nzval[i][0] == 0):
                    nzsub.remove(nzsub[i]);
                    nzval.remove(nzval[i]);
                else:
                    i = i+1;
                
        self.subs = numpy.array(nzsub);
        self.vals = numpy.array(nzval);