Python matrix calculator for all matrix dimensions