# MatCreate

Creates a matrix where the type is determined from either a call to MatSetType() or from the options database with a call to MatSetFromOptions(). The default matrix type is AIJ, using the routines MatCreateSeqAIJ() or MatCreateMPIAIJ() if you do not set a type in the options database. If you never call MatSetType() or MatSetFromOptions() it will generate an error when you try to use the matrix.

### Synopsis

    #include "petscmat.h"
    PetscErrorCode MatCreate(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,Mat *A)

Collective on MPI_Comm

### Input Parameters

| m | - number of local rows (or PETSC_DECIDE)
| n | - number of local columns (or PETSC_DECIDE)
| M | - number of global rows (or PETSC_DETERMINE)
| N | - number of global columns (or PETSC_DETERMINE)
| comm | - MPI communicator

### Output Parameter

A -the matrix

### Options Database Keys

| -mat_type seqaij | - AIJ type, uses MatCreateSeqAIJ()
| -mat_type mpiaij | - AIJ type, uses MatCreateMPIAIJ()
| -mat_type seqbdiag | - block diagonal type, uses MatCreateSeqBDiag()
| -mat_type mpibdiag | - block diagonal type, uses MatCreateMPIBDiag()
| -mat_type mpirowbs | - rowbs type, uses MatCreateMPIRowbs()
| -mat_type seqdense | - dense type, uses MatCreateSeqDense()
| -mat_type mpidense | - dense type, uses MatCreateMPIDense()
| -mat_type seqbaij | - block AIJ type, uses MatCreateSeqBAIJ()
| -mat_type mpibaij | - block AIJ type, uses MatCreateMPIBAIJ()

### Even More Options Database Keys

See the manpages for particular formats (e.g., MatCreateSeqAIJ()) for additional format-specific options.

### Notes

If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the user must ensure that they are chosen to be compatible with the vectors. To do this, one first considers the matrix-vector product 'y = A x'. The 'm' that is used in the above routine must match the local size used in the vector creation routine VecCreateMPI() for 'y'. Likewise, the 'n' used must match that used as the local size in VecCreateMPI() for 'x'.

### User manual sections

| Section 3.1 Creating and Assembling Matrices | - - Chapter 3 Matrices

### Keywords

matrix, create

### See Also

MatCreateSeqAIJ((), MatCreateMPIAIJ(),
MatCreateSeqBDiag(),MatCreateMPIBDiag(), MatCreateSeqDense(), MatCreateMPIDense(), MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(), MatConvert()

Level:beginner
Location:src/mat/utils/gcreate.c
Index of all Mat routines
Table of Contents for all manual pages
Index of all manual pages

### Examples

src/mat/examples/tutorials/ex2.c.html
src/ksp/examples/tutorials/ex1.c.html
src/ksp/examples/tutorials/ex2.c.html
src/ksp/examples/tutorials/ex3.c.html
src/ksp/examples/tutorials/ex4.c.html
src/ksp/examples/tutorials/ex5.c.html
src/ksp/examples/tutorials/ex7.c.html
src/ksp/examples/tutorials/ex8.c.html
src/ksp/examples/tutorials/ex9.c.html
src/ksp/examples/tutorials/ex11.c.html
src/ksp/examples/tutorials/ex12.c.html