Reduce code duplication by templating the Matrix class