Oh, by the way, it looks like what you are implementing is the Einstein convention for summation, and if so you might find that there are better ways of doing your calculations that have been already programmed in the tensor packages.