Advanced maths

Nearest positive semidefinite matrix calculator

Project a real matrix onto the symmetric PSD cone in Frobenius norm, with explicit symmetrization and eigenvalue clipping.

Your numbers

One matrix row per line; commas or spaces separate columns. Use integers, decimals with up to six places, or fractions. Entries and fraction components are bounded by 1,000,000.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A positive semidefinite matrix has no negative quadratic directions. When an entered symmetric matrix is indefinite, replacing negative eigenvalues by zero produces its nearest positive semidefinite matrix in Frobenius norm. This page reconstructs that projection and reports its distance from the original input. Nonsymmetric input is accepted only when you explicitly choose symmetrization, so the treatment of asymmetric entries is visible rather than silently assumed.

THE FORMULAFor S=Q diag(λ)Qᵀ, the nearest PSD matrix is Q diag(max(λ,0))Qᵀ in Frobenius norm.

A worked example

A=[[1,2],[2,1]] has eigenvalues three and minus one. Clipping minus one to zero gives [[1.5,1.5],[1.5,1.5]]. Its Frobenius distance from A is one. The projected matrix is singular and positive semidefinite, not strictly positive definite; its zero eigenvalue is an expected boundary result.

Good to know

Supports real square matrices up to 6×6. Spectral decomposition and reconstruction are numerical, so tiny boundary roundoff can remain. The Frobenius norm is the declared objective. This does not preserve the original diagonal, require strictly positive eigenvalues, enforce sparsity or solve the nearest correlation-matrix problem. A projection result is different from a positive-definiteness test.

The distance norm defines which matrix is nearest

The Frobenius norm takes the square root of the sum of squared entry differences. Under orthogonal changes of basis that norm is unchanged, so the symmetric spectral decomposition turns the projection into independent scalar eigenvalue problems. A nonnegative eigenvalue is kept, and a negative one is moved to the closest allowed value, zero. The original eigenvectors are retained.

The default example changes every entry by magnitude one half. Four squared changes sum to one, giving distance one. Changing the distance norm or requiring a minimum strictly positive eigenvalue would change the optimization problem and its interpretation. The result table lets that distance be checked directly without accepting the eigenvalue interpretation on trust. Already-PSD input should return itself up to numerical roundoff, while an entirely negative spectrum projects to zero.

Clip the negative eigenvalue minus one to zero while retaining eigenvalue threeEigenvalue −1Eigenvalue 3Replace by 0Keep 3Reconstruct: every entry is 1.5
The symmetric matrix [[1,2],[2,1]] has eigenvalues −1 and 3. Its Frobenius-nearest PSD matrix is [[1.5,1.5],[1.5,1.5]], at distance one.

The Principal angles between subspaces calculator also uses an orthogonal basis comparison, but it measures subspace separation rather than projecting a matrix onto a cone.

Semidefinite projection need not preserve a covariance interpretation

A covariance or correlation estimate may require additional constraints. In particular, a correlation matrix normally has unit diagonal. Clipping eigenvalues can change those diagonal entries, so this calculator must not be called a nearest correlation-matrix solver. Adding diagonal preservation changes the feasible set and requires another algorithm.

When symmetrization is enabled, the skew-symmetric part is removed before the PSD spectral step. The reported Frobenius distance still compares the final matrix with the original full input. Without that option, an asymmetric matrix is rejected. The existing Sylvester criterion checks whether a symmetric matrix is positive definite; it does not construct a nearest admissible replacement. Positive semidefinite projection permits zero eigenvalues and can therefore produce a singular matrix even when the input was nonsingular.

A few questions, answered

Is a projected PSD matrix always invertible?

No. Clipping negative eigenvalues to zero commonly creates a singular matrix. PSD and positive definite are different requirements.

Can the diagonal change?

Yes. This is an unconstrained symmetric PSD projection in Frobenius norm. Fixing diagonal entries is a different optimization problem.

Find your calculator

Start typing to explore the library.