# MatCreateConstantDiagonal
Creates a matrix with a uniform value along the diagonal 
## Synopsis
```
#include "petscmat.h" 
PetscErrorCode MatCreateConstantDiagonal(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscScalar diag, Mat *J)
```
Collective


## Input Parameters

- ***comm -*** MPI communicator
- ***m -*** number of local rows (or `PETSC_DECIDE` to have calculated if M is given)
This value should be the same as the local size used in creating the
y vector for the matrix-vector product y = Ax.
- ***n -*** This value should be the same as the local size used in creating the
x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
calculated if N is given) For square matrices n is almost always m.
- ***M -*** number of global rows (or `PETSC_DETERMINE` to have calculated if m is given)
- ***N -*** number of global columns (or `PETSC_DETERMINE` to have calculated if n is given)
- ***diag -*** the diagonal value



## Output Parameter

- ***J -*** the diagonal matrix





## Notes
Only supports square matrices with the same number of local rows and columns


## See Also
 `MatDestroy()`, `MATCONSTANTDIAGONAL`, `MatScale()`, `MatShift()`, `MatMult()`, `MatGetDiagonal()`, `MatGetFactor()`, `MatSolve()`

## Level
advanced

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/cdiagonal/cdiagonal.c.html#MatCreateConstantDiagonal">src/mat/impls/cdiagonal/cdiagonal.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex27.c.html">src/ksp/ksp/tutorials/ex27.c.html</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/impls/cdiagonal/cdiagonal.c)


[Index of all Mat routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
