MatSetValues — PETSc 3.25.2 documentation

Skip to main content
`Ctrl`+`K`

# MatSetValues

Inserts or adds a block of values into a matrix. These values may be cached, so `MatAssemblyBegin()` and `MatAssemblyEnd()` MUST be called after all calls to `MatSetValues()` have been completed.

## Synopsis

    #include "petscmat.h"
    PetscErrorCode MatSetValues(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[], InsertMode addv)

Not Collective

## Input Parameters

- mat - the matrix

- m - the number of rows

- idxm - the global indices of the rows

- n - the number of columns

- idxn - the global indices of the columns

- v - a one-dimensional array that contains the values implicitly stored as a two-dimensional array, by default in row-major order. See `MAT_ROW_ORIENTED` in `MatSetOption()` for how to use column-major order.

- addv - either `ADD_VALUES` to add values to any existing entries, or `INSERT_VALUES` to replace existing entries with new values

## Notes

Calls to `MatSetValues()` with the `INSERT_VALUES` and `ADD_VALUES` options cannot be mixed without intervening calls to the assembly routines.

`MatSetValues()` uses 0-based row and column numbers in Fortran as well as in C.

Negative indices may be passed in `idxm` and `idxn`, these rows and columns are simply ignored. This allows easily inserting element stiffness matrices with homogeneous Dirichlet boundary conditions that you don’t want represented in the matrix.

## Efficiency Alert

The routine `MatSetValuesBlocked()` may offer much better efficiency for users of block sparse formats (`MATSEQBAIJ` and `MATMPIBAIJ`).

## Fortran Notes

If any of `idxm`, `idxn`, and `v` are scalars pass them using, for example,

      call MatSetValues(mat, one, [idxm], one, [idxn], [v], INSERT_VALUES, ierr)

If `v` is a two-dimensional array use `reshape()` to pass it as a one dimensional array

## Developer Note

This is labeled with C so does not automatically generate Fortran stubs and interfaces because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays.

## See Also

Matrices, `Mat`, `MatSetOption()`, `MatAssemblyBegin()`, `MatAssemblyEnd()`, `MatSetValuesBlocked()`, `MatSetValuesLocal()`, `InsertMode`, `INSERT_VALUES`, `ADD_VALUES`

## Level

beginner

## Location

src/mat/interface/matrix.c

## Examples

src/ksp/ksp/tutorials/ex1.c
src/ksp/ksp/tutorials/ex15.c
src/ksp/ksp/tutorials/ex18.c
src/ksp/ksp/tutorials/ex19.c
src/ksp/ksp/tutorials/ex12.c
src/ksp/ksp/tutorials/ex16.c
src/ksp/ksp/tutorials/ex23.c
src/ksp/ksp/tutorials/ex11.c
src/ksp/ksp/tutorials/ex3.c
src/ksp/ksp/tutorials/ex13.c

## Implementations

MatSetValues_MPIAdj() in src/mat/impls/adj/mpi/mpiadj.c
MatSetValues_MPIAIJ() in src/mat/impls/aij/mpi/mpiaij.c
MatSetValues_SeqAIJ() in src/mat/impls/aij/seq/aij.c
MatSetValues_MPIBAIJ() in src/mat/impls/baij/mpi/mpibaij.c
MatSetValues_SeqBAIJ() in src/mat/impls/baij/seq/baij.c
MatSetValues_BlockMat() in src/mat/impls/blockmat/seq/blockmat.c
MatSetValues_MPIDense() in src/mat/impls/dense/mpi/mpidense.c
MatSetValues_SeqDense() in src/mat/impls/dense/seq/dense.c
MatSetValues_Elemental() in src/mat/impls/elemental/matelem.cxx
MatSetValues_HYPRE() in src/mat/impls/hypre/mhypre.c
MatSetValues_IS() in src/mat/impls/is/matis.c
MatSetValues_Preallocator() in src/mat/impls/preallocator/matpreallocator.c
MatSetValues_MPISBAIJ() in src/mat/impls/sbaij/mpi/mpisbaij.c
MatSetValues_SeqSBAIJ() in src/mat/impls/sbaij/seq/sbaij.c
MatSetValues_ScaLAPACK() in src/mat/impls/scalapack/matscalapack.c
MatSetValues_MPISELL() in src/mat/impls/sell/mpi/mpisell.c
MatSetValues_SeqSELL() in src/mat/impls/sell/seq/sell.c

Index of all Mat routines
Table of Contents for all manual pages
Index of all manual pages