:orphan:
# MatDiagonalSet
Computes `Y` = `Y` + `D`, where `D` is a diagonal matrix that is represented as a vector. Or Y[i,i] = D[i] if `InsertMode` is `INSERT_VALUES`. 
## Synopsis
```
#include "petscmat.h"  
PetscErrorCode MatDiagonalSet(Mat Y, Vec D, InsertMode is)
```
Neighbor-wise Collective


## Input Parameters

- ***Y -*** the input matrix
- ***D -*** the diagonal matrix, represented as a vector
- ***i -*** `INSERT_VALUES` or `ADD_VALUES`





## Note
If the matrix `Y` is missing some diagonal entries this routine can be very slow. To make it fast one should initially
fill the matrix so that all diagonal entries have a value (with a value of zero for those locations that would not have an
entry).


## See Also
 [](ch_matrices), `Mat`, `MatShift()`, `MatScale()`, `MatDiagonalScale()`

## Level
intermediate

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/utils/axpy.c.html#MatDiagonalSet">src/mat/utils/axpy.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex21.c.html">src/ksp/ksp/tutorials/ex21.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/constrained/tutorials/tomographyADMM.c.html">src/tao/constrained/tutorials/tomographyADMM.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/pde_constrained/tutorials/elliptic.c.html">src/tao/pde_constrained/tutorials/elliptic.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/tutorials/ex4.c.html">src/tao/tutorials/ex4.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/unconstrained/tutorials/burgers_spectral.c.html">src/tao/unconstrained/tutorials/burgers_spectral.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex50.c.html">src/ts/tutorials/ex50.c</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpiaij.c.html#MatDiagonalSet_MPIAIJ">MatDiagonalSet_MPIAIJ in src/mat/impls/aij/mpi/mpiaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/aij.c.html#MatDiagonalSet_SeqAIJ">MatDiagonalSet_SeqAIJ in src/mat/impls/aij/seq/aij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/is/matis.c.html#MatDiagonalSet_IS">MatDiagonalSet_IS in src/mat/impls/is/matis.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/nest/matnest.c.html#MatDiagonalSet_Nest">MatDiagonalSet_Nest in src/mat/impls/nest/matnest.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sell/mpi/mpisell.c.html#MatDiagonalSet_MPISELL">MatDiagonalSet_MPISELL in src/mat/impls/sell/mpi/mpisell.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/shell/shell.c.html#MatDiagonalSet_Shell">MatDiagonalSet_Shell in src/mat/impls/shell/shell.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/utils/axpy.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)  
