Group theory and Unit testing
While an advanced math degree isn’t absolutely required to be a programmer, it definitely helps one in designing smart unit tests. Here are two examples where I used mathematical properties to design unit tests. Most of my functions that take one of these objects as an argument runs these tests as a pre-condition. Of course, this is switched off in release builds.
SPD Matrices
A type of MRI modality known as Diffusion Tensor MRI can be used to measure the diffusion properties of molecules in tissues. The diffusion is because of the Brownian motion of the molecules. There is a direct connection between the diffusion constant and the local structure in tissues. DT-MRI is pretty much the only non-invasive imaging system for the white matter in the brain.
By making measurements of the diffusion in different directions, we can represent the local diffusion at a voxel by a tensor. This rank-2 tensor is a symmetric positive definite (SPD) matrix. Normal matrices can be thought of as points in a nine dimensional space, but symmetric matrices only have six degrees of freedom. So, instead of being a linear space, the manifold is that of a hyperboloid.
Therefore, it makes sense that any operation done on these matrices should also be a SPD matrix. For example, the group is not closed under the subtraction operation. Literature has many examples of distance definitions for SPD matrices. The log-Euclidean metric for example will fail if your matrix is not a SPD (log of a negative real number is undefined.)
Some properties of SPD matrices:
- Eigenvalues are positive
- Determinant is positive
- Matrix inverse is positive definite
Special Orthogonal/Euclidean
The class of purely rotational matrices fall under Special Orthogonal, while matrices that combine rotations and translations fall under Special Euclidean. They’re both subclasses of general linear transformations. Almost every physics engine will have routines to generate transformations given an angle and a translation vector. Arbitrary transformations can be generated by the composition of transformations along the basis vectors for that space.
Some important properties of these groups:
- Rows are orthogonal to each other
- Columns are orthogonal to each other
- Matrix transpose equals matrix inverse
- Matrix times its inverse/transpose returns the identity matrix
- Determinant is equal to +1
- Columns and rows are normalized
These tests are particularly useful in making sure a multi-disciplinary group is on the same page. Physicists and almost everybody else uses a right hand coordinate system, while Chemists are trained to use the left hand coordinate system. The confusion possibly arises from the definitions of cis- and trans-, I’m not exactly sure. In fact, a quick look at the bible Computer Simulation of Liquids defines rotation matrices as the inverse of the “standard.”
June 25th, 2007 at 12:22 pm
[...] really easy to do Common Subexpression Elimination on analytical expressions. I had previously written about the special case of diffusion tensors. A particular framework for dealing with such objects [...]
February 2nd, 2008 at 6:49 am
[...] necessary to keep the overall quality of ones code high. I’ve written about Unit Tests before, so I’ll continue on that [...]