vector and matrix functions rewritten in template style