Skip to main content
`Ctrl`+`K`

# MatCreateDiagonal

Creates a matrix defined by a given vector along its diagonal.

## Synopsis

    #include "petscmat.h"
    #include "petscvec.h"
    PetscErrorCode MatCreateDiagonal(Vec diag, Mat *J)

Collective

## Input Parameter

- diag - vector for the diagonal

## Output Parameter

- J - the diagonal matrix

## Notes

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

The input vector `diag` will be referenced internally: any changes to `diag` will affect the matrix `J`.

## See Also

Matrices, `Mat`, `MatDestroy()`, `MATDIAGONAL`, `MatScale()`, `MatShift()`, `MatMult()`, `MatGetDiagonal()`, `MatSolve()`, `MatDiagonalRestoreInverseDiagonal()`, `MatDiagonalGetDiagonal()`, `MatDiagonalRestoreDiagonal()`, `MatDiagonalGetInverseDiagonal()`, `MATCONSTANTDIAGONAL`

## Level

advanced

## Location

src/mat/impls/diagonal/diagonal.c

## Examples

src/tao/unconstrained/tutorials/elastic_net_regularization.c

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