class FunctionUtils {

  public static int[] fromTensorIndex(final int index, final int[] dimensions) {
    Validate.notNull(dimensions, "dimensions");
    final int dim = dimensions.length;
    final int[] res = new int[dim];

    int product = 1;
    final int[] products = new int[dim - 1];
    for (int i = 0; i < dim - 1; i++) {
      product *= dimensions[i];
      products[i] = product;
    }

    int a = index;
    for (int i = dim - 1; i > 0; i--) {
      res[i] = a / products[i - 1];
      a -= res[i] * products[i - 1];
    }
    res[0] = a;

    return res;
  }

  private  FunctionUtils();

}