private final PyMultiarray indicesToStructure(final PyObject pyIndices) {
        // Convert the pyIndices into an array of PyObjects.
        // RGA PyObject indices[] = (pyIndices instanceof PyTuple) ?
        // ((PyTuple)pyIndices).list : new PyObject[] {pyIndices};
        final PyObject indices[] = (pyIndices instanceof PyTuple) ? ((PyTuple) pyIndices)
                .getArray() : new PyObject[] { pyIndices };
        // First pass: determine the size of the new dimensions.
        int nDimensions = this.dimensions.length, ellipsisLength = 0, axis = 0;
        for (int i = 0; i < indices.length; i++) {
            final PyObject index = indices[i];
            if (index instanceof PyEllipsis) {
                if (ellipsisLength > 0) {
                    continue;
                }
                ellipsisLength = this.dimensions.length
                        - (indices.length - i - 1 + axis);
                for (int j = i + 1; j < indices.length; j++) {
                    if (indices[j] instanceof PyNone) {
                        ellipsisLength++;
                    }
                }
                if (ellipsisLength < 0) { throw Py
                        .IndexError("too many indices"); }
                axis += ellipsisLength;
            }
            else if (index instanceof PyNone) {
                nDimensions++;
            } else if (index instanceof PyInteger) {
                nDimensions--;
                axis++;
            }
            else if (index instanceof PySlice) {
                axis++;
            } else {
                throw Py.ValueError("invalid index");
            }
        }
        if (axis > this.dimensions.length) { throw Py
                .ValueError("invalid index"); }
        // Second pass: now generate the dimensions.
        int newStart = this.start, newAxis = 0, oldAxis = 0;
        final int[] newDimensions = new int[nDimensions], newStrides = new int[nDimensions];
        for (final PyObject index : indices) {
            if (index instanceof PyEllipsis) {
                if (ellipsisLength > 0) {
                    for (int j = 0; j < ellipsisLength; j++) {
                        newDimensions[newAxis + j] = this.dimensions[oldAxis
                                + j];
                        newStrides[newAxis + j] = this.strides[oldAxis + j];
                    }
                    oldAxis += ellipsisLength;
                    newAxis += ellipsisLength;
                    ellipsisLength = 0;
                }
            }
            else if (index instanceof PyNone) {
                newDimensions[newAxis] = 1;
                newStrides[newAxis] = 0;
                newAxis++;
            }
            else if (oldAxis >= this.dimensions.length) {
                throw Py.IndexError("too many dimensions");
            } else if (index instanceof PyInteger) {
                final PyInteger integer = (PyInteger) index;
                newStart += this.fixIndex(integer.getValue(), oldAxis)
                        * this.strides[oldAxis];
                oldAxis++;
            }
            else if (index instanceof PySlice) {
                final PySlice slice = (PySlice) index;
                final int sliceStep = PyMultiarray.getStep1(slice.step);
                final int sliceStart = PyMultiarray.getStart1(
                        slice.start,
                        sliceStep,
                        this.dimensions[oldAxis]);
                final int sliceStop = PyMultiarray.getStop1(
                        slice.stop,
                        sliceStart,
                        sliceStep,
                        this.dimensions[oldAxis]);
                if (sliceStep > 0) {
                    newDimensions[newAxis] = 1 + (sliceStop - sliceStart - 1)
                            / sliceStep;
                } else {
                    newDimensions[newAxis] = 1 - (sliceStart - sliceStop - 1)
                            / sliceStep;
                }
                newStart += sliceStart * this.strides[oldAxis];
                newStrides[newAxis] = sliceStep * this.strides[oldAxis];
                oldAxis++;
                newAxis++;
            } else {
                throw Py.ValueError("illegal index");
            }
        }
        // Tack any extra indices onto the end.
        for (int i = 0; i < nDimensions - newAxis; i++) {
            newDimensions[newAxis + i] = this.dimensions[oldAxis + i];
            newStrides[newAxis + i] = this.strides[oldAxis + i];
        }
        return new PyMultiarray(
                this.data,
                this._typecode,
                newStart,
                newDimensions,
                newStrides);
    }