COMP: Fix some warnings arising from templates

There are template functions that are hard coded as 3D
but they're instantiated in 2D and 4D as well.

I re-wrote them to take the length of tensor
and the dimensions of matrices into account.