Example #1
0
def insert_sparse_to_csr(mtx1, mtx2, irs, ics):
    """
    Insert a sparse matrix `mtx2` into a CSR sparse matrix `mtx1` at
    rows `irs` and columns `ics`. The submatrix `mtx1[irs,ics]` must
    already be preallocated and have the same structure as `mtx2`.
    """
    import sfepy.discrete.fem.extmods.assemble as asm

    if isinstance(irs, slice):
        irs = nm.arange(irs.start, irs.stop, irs.step, dtype=nm.int32)

    if isinstance(ics, slice):
        ics = nm.arange(ics.start, ics.stop, ics.step, dtype=nm.int32)

    n_row, n_col = mtx1.shape

    assert_((irs.min() >= 0) and (irs.max() < n_row))
    assert_((ics.min() >= 0) and (ics.max() < n_col))

    aux = mtx2.tocoo()
    data = nm.ascontiguousarray(aux.data[:,None,None,None])
    rows = irs[aux.row[:,None]]
    cols = ics[aux.col[:,None]]

    iels = nm.arange(rows.shape[0], dtype=nm.int32)
    asm.assemble_matrix(mtx1.data, mtx1.indptr, mtx1.indices, data,
                        iels, 1.0, rows, cols)
Example #2
0
    def test_assemble_matrix(self):
        from sfepy.discrete.fem.extmods.assemble import assemble_matrix

        mtx = sps.csr_matrix(nm.ones((self.num, self.num),
                                     dtype=nm.float64))
        mtx.data[:] = 0.0

        assemble_matrix(mtx.data, mtx.indptr, mtx.indices, self.mtx_in_els,
                        self.iels, 1, self.conn, self.conn)

        aux = nm.array([[1, 1, 1, 0, 0],
                        [1, 1, 1, 0, 0],
                        [1, 1, 3, 2, 2],
                        [0, 0, 2, 2, 2],
                        [0, 0, 2, 2, 2]], dtype=nm.float64)

        self.report('assembled:\n%s' % mtx.toarray())
        self.report('expected:\n%s' % aux)
        ok = self.compare_vectors(mtx, aux,
                                  label1='assembled',
                                  label2='expected')
        return ok