Description: Compute sparse - sparse matrix addition of matrices
stored in CSR format
325597
public static int cusparseXcsrgeamNnz(
        cusparseHandle handle, 
        int m, 
        int n, 
        cusparseMatDescr descrA, 
        int nnzA, 
        Pointer csrSortedRowPtrA, 
        Pointer csrSortedColIndA, 
        cusparseMatDescr descrB, 
        int nnzB, 
        Pointer csrSortedRowPtrB, 
        Pointer csrSortedColIndB, 
        cusparseMatDescr descrC, 
        Pointer csrSortedRowPtrC, 
        Pointer nnzTotalDevHostPtr)
    {
        return checkResult(cusparseXcsrgeamNnzNative(handle, m, n, descrA, nnzA, csrSortedRowPtrA, csrSortedColIndA, descrB, nnzB, csrSortedRowPtrB, csrSortedColIndB, descrC, csrSortedRowPtrC, nnzTotalDevHostPtr));
    }
475694
def scipy_sparse_to_csv(filepath, matrix, separator=",", directed=False, numbering="matlab"):
    
    matrix = spsp.coo_matrix(matrix)

    shape = matrix.shape
    nnz = matrix.getnnz()

    if numbering == "matlab":
        row = matrix.row + 1
        col = matrix.col + 1
        data = matrix.data
    elif numbering == "c":
        row = matrix.row
        col = matrix.col
        data = matrix.data
    else:
        print("Invalid numbering style.")
        raise RuntimeError

    with open(filepath, "w") as f:
        # Write metadata.
        file_row = "n_rows:" + separator + str(shape[0]) + separator +\
                   "n_cols:" + separator + str(shape[1]) + separator +\
                   "nnz:" + separator + str(nnz) + separator +\
                   "directed:" + separator + str(directed) +\
                   "\n"
        f.write(file_row)

        for edge in range(row.size):
            if directed is False:
                if col[edge] < row[edge]:
                    continue
            file_row = str(row[edge]) + separator + str(col[edge]) + separator + str(data[edge]) + "\n"
            f.write(file_row)
753850
def _random_stochastic_matrix(m, n, k=None, sparse=False, format='csr',
                              random_state=None):
    
    if k is None:
        k = n
    # m prob vectors of dimension k, shape (m, k)
    probvecs = probvec(m, k, random_state=random_state)

    if k == n:
        P = probvecs
        if sparse:
            return scipy.sparse.coo_matrix(P).asformat(format)
        else:
            return P

    # if k < n:
    rows = np.repeat(np.arange(m), k)
    cols = \
        sample_without_replacement(
            n, k, num_trials=m, random_state=random_state
        ).ravel()
    data = probvecs.ravel()

    if sparse:
        P = scipy.sparse.coo_matrix((data, (rows, cols)), shape=(m, n))
        return P.asformat(format)
    else:
        P = np.zeros((m, n))
        P[rows, cols] = data
        return P
1363549
def _convert_to_spmatrix(operators):
        """"""
        # Lazy import to improve `import odl` time
        import scipy.sparse

        # Convert ops to sparse representation. This is not trivial because
        # operators can be indexable themselves and give the wrong impression
        # of an extra dimension. So we have to infer the shape manually
        # first and extract the indices of nonzero positions.
        nrows = len(operators)
        ncols = None
        irow, icol, data = [], [], []
        for i, row in enumerate(operators):
            try:
                iter(row)
            except TypeError:
                raise ValueError(
                    '`operators` must be a matrix of `Operator` objects, `0` '
                    'or `None`, got {!r} (row {} = {!r} is not iterable)'
                    ''.format(operators, i, row))

            if isinstance(row, Operator):
                raise ValueError(
                    '`operators` must be a matrix of `Operator` objects, `0` '
                    'or `None`, but row {} is an `Operator` {!r}'
                    ''.format(i, row))

            if ncols is None:
                ncols = len(row)
            elif len(row) != ncols:
                raise ValueError(
                    'all rows in `operators` must have the same length, but '
                    'length {} of row {} differs from previous common length '
                    '{}'.format(len(row), i, ncols))

            for j, col in enumerate(row):
                if col is None or col is 0:
                    pass
                elif isinstance(col, Operator):
                    irow.append(i)
                    icol.append(j)
                    data.append(col)
                else:
                    raise ValueError(
                        '`operators` must be a matrix of `Operator` objects, '
                        '`0` or `None`, got entry {!r} at ({}, {})'
                        ''.format(col, i, j))

        # Create object array explicitly, threby avoiding erroneous conversion
        # in `coo_matrix.__init__`
        data_arr = np.empty(len(data), dtype=object)
        data_arr[:] = data
        return scipy.sparse.coo_matrix((data_arr, (irow, icol)),
                                       shape=(nrows, ncols))
562076
private static int getDataRows(Chunk[] chunks, Frame f, int[] chunksIds, int cols) {
        double totalRows = 0;
        if(null != chunks) {
            for (Chunk ch : chunks) {
                totalRows += ch.len();
            }
        } else {
            for(int chunkId : chunksIds) {
                totalRows += f.anyVec().chunkLen(chunkId);
            }
        }
        return (int) Math.ceil(totalRows * cols / SPARSE_MATRIX_DIM);
    }
930699
def _set_many(self, i, j, x):
    
    i, j, M, N = self._prepare_indices(i, j)

    n_samples = len(x)
    offsets = np.empty(n_samples, dtype=self.indices.dtype)
    ret = _sparsetools.csr_sample_offsets(M, N, self.indptr, self.indices,
                                          n_samples, i, j, offsets)
    if ret == 1:
        # rinse and repeat
        self.sum_duplicates()
        _sparsetools.csr_sample_offsets(M, N, self.indptr,
                                        self.indices, n_samples, i, j,
                                        offsets)

    if -1 not in offsets:
        # make a list for interaction with h5py
        offsets = list(offsets)
        # only affects existing non-zero cells
        self.data[offsets] = x
        return

    else:
        # raise ValueError(
        #     'Currently, you cannot change the sparsity structure of a SparseDataset.')
        # replace where possible
        mask = offsets > -1
        self.data[offsets[mask]] = x[mask]
        # only insertions remain
        mask = ~mask
        i = i[mask]
        i[i < 0] += M
        j = j[mask]
        j[j < 0] += N
        self._insert_many(i, j, x[mask])
956147
def read_matrix_sparse(filename, dtype=float, comments='#'):
    coo = np.loadtxt(filename, comments=comments, dtype=dtype)

    """"""
    if len(coo.shape) == 2 and coo.shape[1] == 3:
        row = coo[:, 0]
        col = coo[:, 1]
        values = coo[:, 2]

        """Check if imaginary part of row and col is zero"""
        if np.all(np.isreal(row)) and np.all(np.isreal(col)):
            row = row.real
            col = col.real

            """Check if first and second column contain only integer entries"""
            if np.all(is_integer(row)) and np.all(is_integer(col)):

                """Convert row and col to int"""
                row = row.astype(int)
                col = col.astype(int)

                """Create coo-matrix"""
                A = scipy.sparse.coo_matrix((values, (row, col)))
                return A
            else:
                raise ValueError('File contains non-integer entries for row and col.')
        else:
            raise ValueError('File contains complex entries for row and col.')
    else:
        raise ValueError('Given file is not a sparse matrix in coo-format.')
616238
private static void enlargeTables(float[][] data, int[][] rowIndex, int cols, int currentRow, int currentCol) {
        while (data[currentRow].length < currentCol + cols) {
            if(data[currentRow].length == SPARSE_MATRIX_DIM) {
                currentCol = 0;
                cols -= (data[currentRow].length - currentCol);
                currentRow++;
                data[currentRow] = malloc4f(ALLOCATED_ARRAY_LEN);
                rowIndex[currentRow] = malloc4(ALLOCATED_ARRAY_LEN);
            } else {
                int newLen = (int) Math.min((long) data[currentRow].length << 1L, (long) SPARSE_MATRIX_DIM);
                data[currentRow] = Arrays.copyOf(data[currentRow], newLen);
                rowIndex[currentRow] = Arrays.copyOf(rowIndex[currentRow], newLen);
            }
        }
    }
1311244
public SparseMatrix toSparseMatrix() {
        int[] pos = new int[numColumns];
        int[] colIndex = new int[numColumns + 1];
        for (int i = 0; i < numColumns; i++) {
            colIndex[i + 1] = colIndex[i] + colSize[i];
        }

        int nrows = size();
        int[] rowIndex = new int[n];
        double[] x = new double[n];

        for (int i = 0; i < nrows; i++) {
            for (int j : get(i).x) {
                int k = colIndex[j] + pos[j];

                rowIndex[k] = i;
                x[k] = 1;
                pos[j]++;
            }
        }

        return new SparseMatrix(nrows, numColumns, x, rowIndex, colIndex);
    }
764775
def copy(self):
        
        return cspmatrix(self.symb, blkval = +self.blkval, factor = self.is_factor)
217730
def save_sparse_data(self, fname):
        
        data = self.adata_raw.X.T
        if data.getformat()=='csr':
            data=data.tocsc()

        cell_names = np.array(list(self.adata_raw.obs_names))
        gene_names = np.array(list(self.adata_raw.var_names))
        
        pickle.dump((data, cell_names, gene_names), open(fname, 'wb'))
1140447
def _init_from_csr(self, csr):
        
        if len(csr.indices) != len(csr.data):
            raise ValueError('length mismatch: {} vs {}'.format(len(csr.indices), len(csr.data)))
        handle = ctypes.c_void_p()
        _check_call(_LIB.XGDMatrixCreateFromCSREx(c_array(ctypes.c_size_t, csr.indptr),
                                                  c_array(ctypes.c_uint, csr.indices),
                                                  c_array(ctypes.c_float, csr.data),
                                                  ctypes.c_size_t(len(csr.indptr)),
                                                  ctypes.c_size_t(len(csr.data)),
                                                  ctypes.c_size_t(csr.shape[1]),
                                                  ctypes.byref(handle)))
        self.handle = handle
1164642
def write_sparse(self, file_path: str):
        """"""

        coo = sparse.coo_matrix(self.X)
        data = OrderedDict([(0, coo.row+1), (1, coo.col+1), (2, coo.data)])
        df = pd.DataFrame(data, columns=data.keys())
        with open(file_path, 'w') as ofh:
            ofh.write('%%MatrixMarket matrix coordinate real general\n')
            ofh.write('%%%s\n' % '\t'.join(self.genes.astype(str)))
            ofh.write('%%%s\n' % '\t'.join(self.cells.astype(str)))
            ofh.write('%\n')
            ofh.write('%d %d %d\n' % (coo.shape[0], coo.shape[1], coo.nnz))
            df.to_csv(ofh, sep=' ', float_format='%.5f',
                      header=None, index=None)
510620
public static int cusparseScsru2csr_bufferSizeExt(
        cusparseHandle handle, 
        int m, 
        int n, 
        int nnz, 
        Pointer csrVal, 
        Pointer csrRowPtr, 
        Pointer csrColInd, 
        csru2csrInfo info, 
        long[] pBufferSizeInBytes)
    {
        return checkResult(cusparseScsru2csr_bufferSizeExtNative(handle, m, n, nnz, csrVal, csrRowPtr, csrColInd, info, pBufferSizeInBytes));
    }
1061856
def sparse(csv, sparse):
    ''' 
    '''
    import pandas as pd
    df = pd.read_csv(csv, index_col=0, header=0)
    pd.Series(df.index).to_csv(sparse + ".rownames", index=False)
    pd.Series(df.columns.values).to_csv(sparse + ".colnames", index=False)
    with open(sparse, "w+b") as out_handle:
        scipy.io.mmwrite(out_handle, scipy.sparse.csr_matrix(df))
172170
private void indexSparse(DBIDRef ref, SparseNumberVector obj) {
    double len = 0.;
    for(int iter = obj.iter(); obj.iterValid(iter); iter = obj.iterAdvance(iter)) {
      final int dim = obj.iterDim(iter);
      final double val = obj.iterDoubleValue(iter);
      if(val == 0. || val != val) {
        continue;
      }
      len += val * val;
      getOrCreateColumn(dim).add(val, ref);
    }
    length.put(ref, len);
  }
221722
private void svdImpute(double[][] raw, double[][] data) {
        SVD svd = Matrix.newInstance(data).svd();

        int d = data[0].length;

        for (int i = 0; i < raw.length; i++) {
            int missing = 0;
            for (int j = 0; j < d; j++) {
                if (Double.isNaN(raw[i][j])) {
                    missing++;
                } else {
                    data[i][j] = raw[i][j];
                }
            }

            if (missing == 0) {
                continue;
            }

            DenseMatrix A = Matrix.zeros(d - missing, k);
            double[] b = new double[d - missing];

            for (int j = 0, m = 0; j < d; j++) {
                if (!Double.isNaN(raw[i][j])) {
                    for (int l = 0; l < k; l++) {
                        A.set(m, l, svd.getV().get(j, l));
                    }
                    b[m++] = raw[i][j];
                }
            }

            double[] s = new double[k];
            QR qr = A.qr();
            qr.solve(b, s);

            for (int j = 0; j < d; j++) {
                if (Double.isNaN(raw[i][j])) {
                    data[i][j] = 0;
                    for (int l = 0; l < k; l++) {
                        data[i][j] += s[l] * svd.getV().get(j, l);
                    }
                }
            }
        }
    }