Recursive AI

Convert a 3×3 rotation matrix to a quaternion in C#

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

Use CreateFromRotationMatrix on a pure rotation, with no scale, shear, or reflection.

Terminal
dotnet add package RecursiveAI.OneMath

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

Matrix3x3D takes entries in row order and multiplies column vectors. If your source uses row vectors, transpose the matrix before converting.

C#
using RecursiveAI.OneMath;
 
var matrix = new Matrix3x3D(
    0, -1, 0,
    1,  0, 0,
    0,  0, 1);
var q = QuaternionD
    .CreateFromRotationMatrix(matrix);
Console.WriteLine(FormattableString.Invariant(
    $"({q.X:G4}, {q.Y:G4}, {q.Z:G4}, {q.W:G4})"));

Unzip the example, then dotnet run.

Prints the quaternion in (X, Y, Z, W) order, rounded for display.

Text
(0, 0, 0.7071, 0.7071)

The full program

C#
using RecursiveAI.OneMath;
 
var matrix = new Matrix3x3D(
    0, -1, 0,
    1,  0, 0,
    0,  0, 1);
var q = QuaternionD
    .CreateFromRotationMatrix(matrix);
Console.WriteLine(FormattableString.Invariant(
    $"({q.X:G4}, {q.Y:G4}, {q.Z:G4}, {q.W:G4})"));
var input = new Vector3D(1, 0, 0);
var byMatrix = matrix.Transform(input);
var byQuaternion = q.Rotate(input);
Console.WriteLine($"Matrix: {byMatrix}");
Console.WriteLine($"Quaternion: {byQuaternion}");
var agree = Vector3D.Distance(byMatrix, byQuaternion) < 1e-12;
Console.WriteLine($"Agree within 1e-12: {agree}");
Text
(0, 0, 0.7071, 0.7071)
Matrix: (0, 1, 0)
Quaternion: (2.220446049250313E-16, 1, 0)
Agree within 1e-12: True

The conversion assumes a valid rotation matrix; it doesn’t validate or repair the input. The quaternion’s rotated X coordinate, 2.220446049250313E-16, is floating-point rounding error: it’s effectively zero. Both forms turn (1, 0, 0) into (0, 1, 0) within the printed tolerance. The check uses the unrounded quaternion.

Variations

Convert a half turn

Append this to convert 180° about Z. The resulting quaternion has W = 0.

C#
var halfTurn = new Matrix3x3D(
    -1,  0, 0,
     0, -1, 0,
     0,  0, 1);
var halfTurnQ = QuaternionD.CreateFromRotationMatrix(halfTurn);
Console.WriteLine(halfTurnQ);
Text
(0, 0, 1, 0)

The short answer

Pass a pure rotation Matrix3x3D to QuaternionD.CreateFromRotationMatrix. In the example, the first column is (0, 1, 0), so applying the matrix to the positive X axis lands on the positive Y axis. The full program applies the quaternion to the same vector and compares the results with a tolerance.

How do you determine if a matrix is a rotation matrix?

Its three columns must be mutually perpendicular unit vectors, and its determinant must be +1. That’s what orthonormal with determinant +1 means: the axes keep their lengths, their right angles, and their orientation. A reflection can pass the perpendicularity and length checks but has determinant −1. Scale can change column lengths, and shear can change the angles between columns. None of those transforms is a pure rotation.

CreateFromRotationMatrix assumes those conditions; it isn’t a matrix decomposition. If your input comes from a transform that also scales or shears, supply its rotation separately rather than passing that entire 3×3 block. Normalizing the output quaternion doesn’t recover a missing rotation or remove a reflection from the input.

How do you convert a rotation matrix to a quaternion at 180 degrees?

Use the same call. The converter branches on the trace (the sum of the diagonal entries) and the diagonal entries themselves. In the quarter-turn example the trace is 1; in the half-turn variation it’s −1, so the conversion takes a diagonal branch. This matters because at 180° the scalar component W is zero, and a formula that always recovers the other components by dividing by W would break. The sample half turn produces (0, 0, 1, 0).

Why use quaternions over rotation matrices?

A quaternion stores four components instead of nine and lets you interpolate between orientations with QuaternionD.Slerp. A matrix may be the form you already have for applying a transform to vectors. Neither choice changes the intended rotation. One comparison trap is that a unit quaternion and the quaternion with all four signs reversed represent the same rotation, even though component-wise equality says they’re different. For a convention check, compare how both representations rotate basis vectors, rather than requiring every component to have a particular sign. If the result turns X toward −Y instead of +Y, check whether the source uses row vectors and needs to be transposed.

All onemath recipes