A few remarks:
0) Very nice practical introduction to quaternions and their application to rotation.
1) Neat didactic "textbook" implementation, but note that it is not production quality (eg potential overflow in the norm function unnecessarily). That was not the aim, either, but just something to bear in mind.
2) As a supplement, a useful practical reference for rotations in 3D (with good clarifications and basically all formulae you'll ever need, but no implementation) is
Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors by James Diebel
https://www.astro.rug.nl/software/kapteyn-beta/_downloads/at...