:orphan:
# MatSetValuesStencil
Inserts or adds a block of values into a matrix. Using structured grid indexing
## Synopsis
```
#include "petscmat.h"
PetscErrorCode MatSetValuesStencil(Mat mat, PetscInt m, const MatStencil idxm[], PetscInt n, const MatStencil idxn[], const PetscScalar v[], InsertMode addv)
```
Not Collective
## Input Parameters
- ***mat -*** the matrix
- ***m -*** number of rows being entered
- ***idxm -*** grid coordinates (and component number when dof > 1) for matrix rows being entered
- ***n -*** number of columns being entered
- ***idxn -*** grid coordinates (and component number when dof > 1) for matrix columns being entered
- ***v -*** a logically two-dimensional array of values
- ***addv -*** either `ADD_VALUES` to add to existing entries at that location or `INSERT_VALUES` to replace existing entries with new values
## Notes
By default the values, `v`, are row-oriented. See `MatSetOption()` for other options.
Calls to `MatSetValuesStencil()` with the `INSERT_VALUES` and `ADD_VALUES`
options cannot be mixed without intervening calls to the assembly
routines.
The grid coordinates are across the entire grid, not just the local portion
`MatSetValuesStencil()` uses 0-based row and column numbers in Fortran
as well as in C.
For setting/accessing vector values via array coordinates you can use the `DMDAVecGetArray()` routine
In order to use this routine you must either obtain the matrix with `DMCreateMatrix()`
or call `MatSetLocalToGlobalMapping()` and `MatSetStencil()` first.
The columns and rows in the stencil passed in MUST be contained within the
ghost region of the given process as set with DMDACreateXXX() or `MatSetStencil()`. For example,
if you create a `DMDA` with an overlap of one grid level and on a particular process its first
local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the
first i index you can use in your column and row indices in `MatSetStencil()` is 5.
For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
`DM_BOUNDARY_PERIODIC` boundary type.
For indices that don't mean anything for your case (like the k index when working in 2d) or the c index when you have
a single value per point) you can skip filling those indices.
Inspired by the structured grid interface to the HYPRE package
(https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods)
## Efficiency Alert
The routine `MatSetValuesBlockedStencil()` may offer much better efficiency
for users of block sparse formats (`MATSEQBAIJ` and `MATMPIBAIJ`).
## Fortran Note
`idxm` and `idxn` should be declared as
```none
MatStencil idxm(4,m),idxn(4,n)
```
and the values inserted using
```none
idxm(MatStencil_i,1) = i
idxm(MatStencil_j,1) = j
idxm(MatStencil_k,1) = k
idxm(MatStencil_c,1) = c
etc
```
## See Also
[](ch_matrices), `Mat`, `DMDA`, `MatSetOption()`, `MatAssemblyBegin()`, `MatAssemblyEnd()`, `MatSetValuesBlocked()`, `MatSetValuesLocal()`
`MatSetValues()`, `MatSetValuesBlockedStencil()`, `MatSetStencil()`, `DMCreateMatrix()`, `DMDAVecGetArray()`, `MatStencil`
## Level
beginner
## Location
src/mat/interface/matrix.c
## Examples
src/ksp/ksp/tutorials/ex22f.F90
src/ksp/ksp/tutorials/ex25.c
src/ksp/ksp/tutorials/ex28.c
src/ksp/ksp/tutorials/ex29.c
src/ksp/ksp/tutorials/ex32.c
src/ksp/ksp/tutorials/ex34.c
src/ksp/ksp/tutorials/ex42.c
src/ksp/ksp/tutorials/ex43.c
src/ksp/ksp/tutorials/ex45.c
src/ksp/ksp/tutorials/ex45f.F90
src/ksp/ksp/tutorials/ex46.c
---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/interface/matrix.c)
[Index of all Mat routines](index.md)
[Table of Contents for all manual pages](/manualpages/index.md)
[Index of all manual pages](/manualpages/singleindex.md)