function recurse( array, ndims, shape, dim, index ) {
	var stepSize;
	var subArray;
	var out;
	var d;
	var S;
	var i;

	S = shape[ dim ];
	if ( dim === ndims - 1 ) {
		return slice( array, index, index + S );
	}

	d = dim + 1;
	stepSize = 1;
	for ( i = d; i < shape.length; i++ ) {
		stepSize *= shape[ i ];
	}

	out = [];
	for ( i = 0; i < S; i++ ) {
		subArray = recurse( array, ndims, shape, d, index );
		out.push( subArray );
		index += stepSize;
	}
	return out;
}
Recursive reshapes an array.

@private
@param {Array} array - input n-dimensional nested array
@param {NonNegativeInteger} ndims - number of dimensions
@param {NonNegativeIntegerArray} shape - array shape
@param {NonNegativeInteger} dim - dimension index
@param {NonNegativeInteger} index - sub-array index
@returns {Array} output m-dimensional nested array