# Matrix4 from Quaternion

Hi, hopefully this is an easy question!

I’m converting some code from cgmath to nalgebra, and I can’t find a matching use case.

I’m trying to convert a quaternion to a mat4.

In cgmath, there is this implementation of From<> here:

I can’t find matching functionality in the docs or the code for nalgebra, does this functionality exist under a function I can’t find? Basically, I can’t figure out how to (using the library, I could do it by hand) convert a quat -> mat4.

Anyone know how I would do that using this library? Thanks a bunch!!

A little sad, I just tried to implement this for myself but it seems the coherence rules prevent me. I’m still pretty new so I hope this doesn’t turn out to be too easy!

Not every quaternion can be used as a rotation, you must use a `UnitQuaternionBase`. Some methods are only implemented for it, including the one you want: `to_rotation_matrix`. This method can be found here. You can get a `UnitQuaternionBase` by calling `Unit::new_normalize` on a quaternion.

@bjadamson In addition to what @pengowen123 said, the method `.to_rotation_matrix()` will return a `Rotation3`, i.e., a 3x3 rotation matrix. If you want a 4x4 matrix, you have to call `.to_homogeneous()` instead (because a 4x4 rotation matrix is actually the 3D rotation expressed in homogeneous coordinates). For example:

``````use na::UnitQuaternion;

fn main() {
let q = UnitQuaternion::<f64>::identity();
let mat4 = q.to_homogeneous();
}
``````

Also note that the type `UnitQuaternion` should be used instead of `UnitQuaternionBase`. The latter which will be removed in a future release.