---
title: "Convert a 3×3 rotation matrix to a quaternion in C#"
url: https://recursiveai.org/onemath/cookbook/convert-rotation-matrix-to-quaternion/
description: "Convert a pure 3×3 rotation matrix to a quaternion with OneMath in C#. Apply both forms to the same vector to check that they rotate it the same way."
version: "1.0.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/convert-rotation-matrix-to-quaternion/convert-rotation-matrix-to-quaternion.zip)

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

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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.

```csharp
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](https://recursiveai.org/onemath/cookbook/convert-rotation-matrix-to-quaternion/convert-rotation-matrix-to-quaternion.zip), 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

```csharp
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`.

```csharp
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)
```

## Related recipes

- [Interpolate quaternion rotations with SLERP](https://recursiveai.org/onemath/cookbook/interpolate-quaternion-with-slerp/)
- [Undo a rotation with an inverse quaternion](https://recursiveai.org/onemath/cookbook/undo-rotation-with-inverse-quaternion/)
- [Compose rigid transforms](https://recursiveai.org/onemath/cookbook/compose-rigid-transforms/)

## 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](https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/rotation.html) 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`](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/QuaternionD.cs#L57-L85) 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](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/QuaternionD.cs#L98-L105) 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.
