# How can I estimate the condition number of a large sparse matrix using PETSc?

I have a PETSc Mat and would like to estimate its condition number.

For small matrices, the condition number can be reliably computed using the singular value decomposition. Do a KSPSolve() with the matrix and run with -pc_type svd -pc_svd_monitor.
-ksp_monitor_singular_value -ksp_type gmres -ksp_gmres_restart 1000 -pc_type none

These options say to estimate the extremal singular values on each Krylov iteration. GMRES is used to compute the Krylov space (you could also use CG) with a huge restart. At restarts, GMRES discards the current Krylov space, so all the progress on singular value estimates is lost in a restart. The final option -pc_type none says to perform this iteration on the unpreconditioned matrix. By default, the preconditioned operator ($P^{-1} A$ or $A P^{-1}$) would be used, so you would end up with an estimate for the preconditioned operator.
This will generally be accurate for the largest singular values, but may overestimate the smallest singular value unless the method has converged. If you have a solver for the matrix (e.g. using KSPSolve()), then you can estimate the smallest singular value of $A$ using the same procedure applied to $A^{-1}$.