Change Matrix to template class.

Signed-off-by: CyberZHG <CyberZHG@gmail.com>