# KSPComputeRitz
Computes the Ritz or harmonic Ritz pairs associated with the smallest or largest in modulus, for the preconditioned operator. 
## Synopsis
```
#include "petscksp.h" 
#include "petscmat.h" 
PetscErrorCode KSPComputeRitz(KSP ksp, PetscBool ritz, PetscBool small, PetscInt *nrit, Vec S[], PetscReal tetar[], PetscReal tetai[])
```
Not Collective


## Input Parameters

- ***ksp   -*** iterative context obtained from `KSPCreate()`
- ***ritz  -*** `PETSC_TRUE` or `PETSC_FALSE` for Ritz pairs or harmonic Ritz pairs, respectively
- ***small -*** `PETSC_TRUE` or `PETSC_FALSE` for smallest or largest (harmonic) Ritz values, respectively



## Output Parameters

- ***nrit  -*** On input number of (harmonic) Ritz pairs to compute; on output, actual number of computed (harmonic) Ritz pairs
- ***S     -*** an array of the Ritz vectors, pass in an array of vectors of size nrit
- ***tetar -*** real part of the Ritz values, pass in an array of size nrit
- ***tetai -*** imaginary part of the Ritz values, pass in an array of size nrit



## Notes
This only works with a `KSPType` of `KSPGMRES`.

One must call `KSPSetComputeRitz()` before calling `KSPSetUp()` in order for this routine to work correctly.

This routine must be called after `KSPSolve()`.

In GMRES, the (harmonic) Ritz pairs are computed from the Hessenberg matrix obtained during
the last complete cycle of the GMRES solve, or during the partial cycle if the solve ended before
a restart (that is a complete GMRES cycle was never achieved).

The number of actual (harmonic) Ritz pairs computed is less than or equal to the restart
parameter for GMRES if a complete cycle has been performed or less or equal to the number of GMRES
iterations.

`KSPComputeEigenvalues()` provides estimates for only the eigenvalues (Ritz values).

For real matrices, the (harmonic) Ritz pairs can be complex-valued. In such a case,
the routine selects the complex (harmonic) Ritz value and its conjugate, and two successive entries of the
vectors S are equal to the real and the imaginary parts of the associated vectors.
When PETSc has been built with complex scalars, the real and imaginary parts of the Ritz
values are still returned in tetar and tetai, as is done in `KSPComputeEigenvalues()`, but
the Ritz vectors S are complex.

The (harmonic) Ritz pairs are given in order of increasing (harmonic) Ritz values in modulus.

The Ritz pairs do not necessarily accurately reflect the eigenvalues and eigenvectors of the operator, consider the
excellent package `SLEPc` if accurate values are required.




## See Also
 [](chapter_ksp), `KSPSetComputeRitz()`, `KSP`, `KSPGMRES`, `KSPComputeEigenvalues()`, `KSPSetComputeSingularValues()`, `KSPMonitorSingularValue()`

## Level
advanced

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/interface/itfunc.c.html#KSPComputeRitz">src/ksp/ksp/interface/itfunc.c</A>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/gmreig.c.html#KSPComputeRitz_GMRES">KSPComputeRitz_GMRES in src/ksp/ksp/impls/gmres/gmreig.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/ksp/ksp/interface/itfunc.c)


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