template based flexible matrix class