예제 #1
0
    def tocoo(self, copy=True):
        major_dim, minor_dim = self._swap(self.shape)
        minor_indices = self.indices
        major_indices = np.empty(len(minor_indices), dtype=self.indices.dtype)
        _sparsetools.expandptr(major_dim, self.indptr, major_indices)
        row, col = self._swap((major_indices, minor_indices))

        from scipy.sparse.coo import coo_matrix
        return coo_matrix((self.data, (row, col)),
                          self.shape,
                          copy=copy,
                          dtype=self.dtype)
예제 #2
0
    def tocoo(self, copy=True):
        major_dim, minor_dim = self._swap(self.shape)
        minor_indices = self.indices
        major_indices = np.empty(len(minor_indices), dtype=self.indices.dtype)
        _sparsetools.expandptr(major_dim, self.indptr, major_indices)
        row, col = self._swap((major_indices, minor_indices))

        from pyomo.contrib.pynumero.sparse.coo import COOSymMatrix
        return COOSymMatrix((self.data, (row, col)),
                            self.shape,
                            copy=copy,
                            dtype=self.dtype)
예제 #3
0
    def tocoo(self,copy=True):
        """Return a COOrdinate representation of this matrix
        When copy=False the index and data arrays are not copied.
        """
        major_dim,minor_dim = self._swap(self.shape)

        data = self.data
        minor_indices = self.indices

        if copy:
            data = data.copy()
            minor_indices = minor_indices.copy()

        major_indices = np.empty(len(minor_indices), dtype=self.indices.dtype)

        _sparsetools.expandptr(major_dim,self.indptr,major_indices)

        row,col = self._swap((major_indices,minor_indices))

        from .coo import coo_matrix
        return coo_matrix((data,(row,col)), self.shape)
예제 #4
0
def mul_coo_coo(A, B):

    M, K = A.shape
    K_, N = B.shape
    assert(K == K_)

    # convert dtype to c_int and c_double
    rowIndex_A = get_indptr(A)
    #columns_A = A.col.astype(np.int32)
    columns_A = A.coords[1].astype(np.int32)
    values_A = A.data.astype(np.double)
    
    rowIndex_B = get_indptr(B)
    #columns_B = B.col.astype(np.int32)
    columns_B = B.coords[1].astype(np.int32)
    values_B = B.data.astype(np.double)

    # output variables
    pointerB_C_p = POINTER(c_int)()
    pointerE_C_p = POINTER(c_int)()
    columns_C_p = POINTER(c_int)()
    values_C_p = POINTER(c_double)()
    nnz = c_int(0)
    handle_C = c_void_p() # used to free data

    # calculation
    libspmm.spmm(byref(c_int(M)), byref(c_int(N)), byref(c_int(K)), \
            rowIndex_A.ctypes.data_as(c_void_p), columns_A.ctypes.data_as(c_void_p), values_A.ctypes.data_as(c_void_p), \
            rowIndex_B.ctypes.data_as(c_void_p), columns_B.ctypes.data_as(c_void_p), values_B.ctypes.data_as(c_void_p), \
            byref(pointerB_C_p), byref(pointerE_C_p), byref(columns_C_p), byref(values_C_p), byref(nnz), byref(handle_C))
    
    nnz = nnz.value

    # convert to numpy object
    buffer_tmp = np.core.multiarray.int_asbuffer(addressof(values_C_p.contents),\
            np.dtype(np.double).itemsize * nnz)
    values_C = np.frombuffer(buffer_tmp, np.double).copy()

    buffer_tmp = np.core.multiarray.int_asbuffer(
                addressof(columns_C_p.contents), np.dtype(np.int32).itemsize * nnz)
    columns_C = np.frombuffer(buffer_tmp, np.int32).copy()

    buffer_tmp = np.core.multiarray.int_asbuffer(
                addressof(pointerB_C_p.contents), np.dtype(np.int32).itemsize * M)
    pointerB_C = np.frombuffer(buffer_tmp, np.int32).copy()
    pointerB_C = np.append(pointerB_C, np.array(nnz, dtype = np.int32)) # automatically do the copy

    # free C
    libspmm.free_handle(byref(handle_C))

    # compute full row of C
    rows_C = np.empty(nnz, dtype = columns_C.dtype)
    _sparsetools.expandptr(M, pointerB_C, rows_C)
   
    # sort the cols and data
    idx_C = np.empty(nnz, dtype = np.int64)
    pre_num = 0
    for i in xrange(M):
        idx_C[pointerB_C[i]:pointerB_C[i + 1]] = np.argsort(columns_C[pointerB_C[i]:pointerB_C[i + 1]]) + pre_num
        pre_num += pointerB_C[i + 1] - pointerB_C[i]
    columns_C = columns_C[idx_C]
    values_C = values_C[idx_C]
    
    return COO(np.vstack((rows_C, columns_C)), data = values_C, shape = (M, N), sorted = True, has_duplicates = False)