function reshape (array, sizes) {
  const flatArray = flatten(array, true) // since it has rectangular
  const currentLength = flatArray.length

  if (!Array.isArray(array) || !Array.isArray(sizes)) {
    throw new TypeError('Array expected')
  }

  if (sizes.length === 0) {
    throw new DimensionError(0, currentLength, '!=')
  }

  sizes = processSizesWildcard(sizes, currentLength)
  const newLength = product(sizes)
  if (currentLength !== newLength) {
    throw new DimensionError(
      newLength,
      currentLength,
      '!='
    )
  }

  try {
    return _reshape(flatArray, sizes)
  } catch (e) {
    if (e instanceof DimensionError) {
      throw new DimensionError(
        newLength,
        currentLength,
        '!='
      )
    }
    throw e
  }
}
Re-shape a multi dimensional array to fit the specified dimensions
@param {Array} array           Array to be reshaped
@param {number[]} sizes        List of sizes for each dimension
@returns {Array}               Array whose data has been formatted to fit the
                               specified dimensions

@throws {DimensionError}       If the product of the new dimension sizes does
                               not equal that of the old ones