Recursive AI

Decompose a symmetric 3×3 matrix in C#

OneMath is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Find eigenvalues and unit eigenvectors. Only symmetric 3×3 matrices are supported.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OneMath.Physics;
 
var matrix = new Matrix3x3D(
    2, 1, 0, 1, 2, 0, 0, 0, 4);
var eigen = SymmetricEigenD.Decompose(matrix);
Console.WriteLine(eigen.Values);

Computes ascending eigenvalues; eigenvectors are in eigen.Vectors as columns.

Text
(1, 3, 4)

Unzip the example, then dotnet run.

Full program

C#
using RecursiveAI.OneMath;
using RecursiveAI.OneMath.Physics;
 
var matrix = new Matrix3x3D(
    2, 1, 0,
    1, 2, 0,
    0, 0, 4);
var eigen = SymmetricEigenD.Decompose(matrix);
Console.WriteLine($"Eigenvalues: {eigen.Values}");
var vectors = eigen.Vectors;
var rebuilt = vectors * Matrix3x3D.CreateScale(eigen.Values)
    * vectors.Transposed();
var gram = vectors.Transposed() * vectors;
double MaxError(Matrix3x3D a, Matrix3x3D b)
{
    double error = 0;
    for (int row = 0; row < 3; row++)
    {
        var delta = a.Row(row) - b.Row(row);
        error = Math.Max(error, Math.Abs(delta.X));
        error = Math.Max(error, Math.Abs(delta.Y));
        error = Math.Max(error, Math.Abs(delta.Z));
    }
    return error;
}
Console.WriteLine($"Reconstruction within 1e-14: {MaxError(rebuilt, matrix) < 1e-14}");
Console.WriteLine($"Orthonormal within 1e-14: {MaxError(gram, Matrix3x3D.Identity) < 1e-14}");
Console.WriteLine($"Right-handed: {vectors.Determinant() > 0}");
Text
Eigenvalues: (1, 3, 4)
Reconstruction within 1e-14: True
Orthonormal within 1e-14: True
Right-handed: True

Nonfinite entries throw ArgumentOutOfRangeException; a matrix outside the symmetry tolerance throws ArgumentException.

Variations

Single precision and repeated eigenvalues

C#
using RecursiveAI.OneMath;
using RecursiveAI.OneMath.Physics;
 
var matrix = new Matrix3x3F(
    2, 0, 0, 0, 2, 0, 0, 0, 5);
var eigen = SymmetricEigenF.Decompose(matrix);
Console.WriteLine(eigen.Values);
Console.WriteLine(eigen.Vectors == Matrix3x3F.Identity);
Text
(2, 2, 5)
True

The short answer

Use SymmetricEigenD.Decompose for a double-precision symmetric 3×3 matrix, or SymmetricEigenF.Decompose for single precision. The result contains three ascending real eigenvalues in Values and the corresponding eigenvectors as columns of Vectors. It is not an API for arbitrary-size or nonsymmetric matrices.

The example uses a matrix with rows (2, 1, 0), (1, 2, 0) and (0, 0, 4). Its eigenvalues are 1, 3 and 4. The full program reconstructs the matrix and checks whether the eigenvector columns are orthonormal. Those checks describe this input, not a guarantee of exact equality after floating-point multiplication.

How do I compute a symmetric matrix eigen decomposition in C#?

Construct Matrix3x3D with all nine entries, in row order, then call the method shown at the top. A symmetric matrix has matching entries across the diagonal: M12 matches M21, M13 matches M31, and M23 matches M32. The input contract requires every entry to be finite and mirrored pairs to agree within 64 * eps * max|entry|. Here eps is 2⁻⁵² for double and 2⁻²³ for float.

The calculation reads the upper triangle after checking symmetry. Do not fill the lower half with zeros unless those zeros are actually the mirrored values. For an input with small disagreement inside the allowed tolerance, the upper half defines the symmetric matrix being decomposed.

Read Values.X, Values.Y and Values.Z in ascending order. Pair them with columns 0, 1 and 2 respectively, not with rows. This is important when passing the frame to code with a different matrix convention. OneMath uses column vectors; its conversion guide covers the boundary with the framework’s numerics types.

How do I reconstruct a symmetric matrix from eigenvalues and eigenvectors?

The formula is A = V * diag(values) * V.Transposed(). The full program implements it with Matrix3x3D.CreateScale(eigen.Values). It also computes V.Transposed() * V, which should be close to the identity, and checks that the determinant is positive.

The documented accuracy bounds are relative to the largest input entry. For normal-range eigenvalues, maximum reconstruction error is at most 16 u max|A|, maximum orthonormality error is at most 10 u, and determinant error from +1 is at most 10 u. Here u is 2⁻⁵³ for double or 2⁻²⁴ for float. Subnormal results add quantization steps to the reconstruction bound. Eigenvalues can overflow even when the individual input entries are finite.

Use an application-appropriate tolerance rather than exact equality when comparing the rebuilt matrix with the input. A tolerance appropriate for this small integer example is not a universal threshold for matrices of any scale.

What happens when a symmetric matrix has repeated eigenvalues?

Equal eigenvalues retain the iteration’s order. A diagonal input with repeated values keeps the original axis order, as the variation demonstrates. The zero matrix returns zero eigenvalues and the identity frame.

OneMath also fixes signs: the largest-magnitude component of each of the first two columns is positive, with the lowest component index winning ties. The third column is chosen to make the frame right-handed. Its largest component need not be positive. These are the documented conventions, not a reason to reorder columns independently of the eigenvalues.

For repeated values, consume the returned frame as a frame rather than assigning a unique geometric meaning to each axis. When processing inertia tensors, the principal-axis API supplies the moments and rotation together.

All onemath recipes