# MatShellSetOperation
Allows user to set a matrix operation for a `MATSHELL` shell matrix. 
## Synopsis
```
#include "petscmat.h" 
PetscErrorCode MatShellSetOperation(Mat mat, MatOperation op, void (*g)(void))
```
Logically Collective


## Input Parameters

- ***mat -*** the `MATSHELL` shell matrix
- ***op -*** the name of the operation
- ***g -*** the function that provides the operation.





## Usage
```none
      extern PetscErrorCode usermult(Mat,Vec,Vec);
      MatCreateShell(comm,m,n,M,N,ctx,&A);
      MatShellSetOperation(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.

Within each user-defined routine, the user should call
`MatShellGetContext()` to obtain the user-defined context that was
set by `MatCreateShell()`.

Use `MatSetOperation()` to set an operation for any matrix type. For matrix product operations (i.e. `MatMatXXX()`, `MatTransposeMatXXX()` etc) use `MatShellSetMatProductOperation()`


## Fortran Note
For `MatCreateVecs()` the user code should check if the input left or right matrix is -1 and in that case not
generate a matrix. See src/mat/tests/ex120f.F


## See Also
 `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellSetContext()`, `MatSetOperation()`, `MatShellSetManageScalingShifts()`, `MatShellSetMatProductOperation()`

## Level
advanced

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

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex14f.F90.html">src/ksp/ksp/tutorials/ex14f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex12.c.html">src/snes/tutorials/ex12.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex36.c.html">src/snes/tutorials/ex36.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex20opt_ic.c.html">src/ts/tutorials/ex20opt_ic.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex22f_mf.F90.html">src/ts/tutorials/ex22f_mf.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex50.c.html">src/ts/tutorials/ex50.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/unconstrained/tutorials/eptorsion1.c.html">src/tao/unconstrained/tutorials/eptorsion1.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/unconstrained/tutorials/eptorsion3.c.html">src/tao/unconstrained/tutorials/eptorsion3.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/bound/tutorials/plate2.c.html">src/tao/bound/tutorials/plate2.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/leastsquares/tutorials/tomography.c.html">src/tao/leastsquares/tutorials/tomography.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/pde_constrained/tutorials/elliptic.c.html">src/tao/pde_constrained/tutorials/elliptic.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/shell/shell.c.html#MatShellSetOperation_Shell(Mat mat, MatOperation op, void (*f)">MatShellSetOperation_Shell(Mat mat, MatOperation op, void (*f) in src/mat/impls/shell/shell.c</A><BR>


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