Improving performance of matrix math templates