8000 BUG: Fill correct strides for ndmin in array creation by seberg · Pull Request #466 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fill correct strides for ndmin in array creation #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1507,18 +1507,26 @@ PyArray_EquivTypenums(int typenum1, int typenum2)
/*** END C-API FUNCTIONS **/

static PyObject *
_prepend_ones(PyArrayObject *arr, int nd, int ndmin)
_prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order)
{
npy_intp newdims[NPY_MAXDIMS];
npy_intp newstrides[NPY_MAXDIMS];
npy_intp newstride;
int i, k, num;
PyArrayObject *ret;
PyArray_Descr *dtype;

if (order == NPY_FORTRANORDER || PyArray_ISFORTRAN(arr) || PyArray_NDIM(arr) == 0) {
newstride = PyArray_DESCR(arr)->elsize;
}
else {
newstride = PyArray_STRIDES(arr)[0] * PyArray_DIMS(arr)[0];
}

num = ndmin - nd;
for (i = 0; i < num; i++) {
newdims[i] = 1;
newstrides[i] = PyArray_DESCR(arr)->elsize;
newstrides[i] = newstride;
}
for (i = num; i < ndmin; i++) {
k = i - num;
Expand Down Expand Up @@ -1659,7 +1667,7 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws)
* create a new array from the same data with ones in the shape
* steals a reference to ret
*/
return _prepend_ones(ret, nd, ndmin);
return _prepend_ones(ret, nd, ndmin, order);

clean_type:
Py_XDECREF(type);
Expand Down
7 changes: 7 additions & 0 deletions numpy/core/tests/test_regression.py
< 48DD td id="diff-0f2a3660c450cf32c6985c4c56b7078eb113b9cf77136e0c24509879b36367d4L461" data-line-number="461" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ def test_ndmin_float64(self, level=rlevel):
assert_equal(np.array(x,dtype=np.float32,ndmin=2).ndim,2)
assert_equal(np.array(x,dtype=np.float64,ndmin=2).ndim,2)

def test_ndmin_order(self, level=rlevel):
"""Issue #465 and related checks"""
assert_(np.array([1,2], order='C', ndmin=3).flags.c_contiguous)
assert_(np.array([1,2], order='F', ndmin=3).flags.f_contiguous)
assert_(np.array(np.ones((2,2), order='F'), ndmin=3).flags.f_contiguous)
assert_(np.array(np.ones((2,2), order='C'), ndmin=3).flags.c_contiguous)

def test_mem_axis_minimization(self, level=rlevel):
"""Ticket #327"""
data = np.arange(5)
Expand Down
0