function for multi-dimensional index calculation