# MatSetOperation
Allows user to set a matrix operation for any matrix type 
## Synopsis
```
#include "petscmat.h" 
PetscErrorCode MatSetOperation(Mat mat, MatOperation op, void (*f)(void))
```
Logically Collective


## Input Parameters

- ***mat -*** the matrix
- ***op -*** the name of the operation
- ***f -*** the function that provides the operation





## Usage
```none
extern PetscErrorCode usermult(Mat,Vec,Vec);
```
```none
PetscCall(MatCreateXXX(comm,...&A);
```
```none
PetscCall(MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult);
```


## Notes
See the file include/petscmat.h for a complete list of matrix
operations, which all have the form MATOP_<OPERATION>, where
<OPERATION> is the name (in all capital letters) of the
user interface routine (e.g., MatMult() -> MATOP_MULT).

All user-provided functions (except for `MATOP_DESTROY`) should have the same calling
sequence as the usual matrix interface routines, since they
are intended to be accessed via the usual matrix interface
routines, e.g.,
```none
MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec)
```

In particular each function MUST return an error code of 0 on success and
nonzero on failure.

This routine is distinct from `MatShellSetOperation()` in that it can be called on any matrix type.


## See Also
 `MatGetOperation()`, `MatCreateShell()`, `MatShellSetContext()`, `MatShellSetOperation()`

## Level
developer

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/interface/matrix.c.html#MatSetOperation">src/mat/interface/matrix.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex29.c.html">src/ksp/ksp/tutorials/ex29.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex34.c.html">src/ksp/ksp/tutorials/ex34.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/bound/tutorials/jbearing2.c.html">src/tao/bound/tutorials/jbearing2.c.html</A><BR>


---
[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](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
