diff options
author | alex <argriffi@ncsu.edu> | 2015-10-21 17:32:51 -0400 |
---|---|---|
committer | alex <argriffi@ncsu.edu> | 2015-10-23 13:27:29 -0400 |
commit | b94546d7426627c33686ddbf72bd69b8ddd4afed (patch) | |
tree | 52f2c3af68dfa4a4ac9defc18ba8e91b4b8acdf3 | |
parent | 626eb3748d067ea6e4c4dd308252bfad97df186f (diff) | |
download | numpy-b94546d7426627c33686ddbf72bd69b8ddd4afed.tar.gz |
BUG: fix inner() by copying if needed to enforce contiguity
-rw-r--r-- | numpy/core/src/multiarray/cblasfuncs.c | 22 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 16 |
2 files changed, 36 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/cblasfuncs.c b/numpy/core/src/multiarray/cblasfuncs.c index 9c7c26725..67f325ba1 100644 --- a/numpy/core/src/multiarray/cblasfuncs.c +++ b/numpy/core/src/multiarray/cblasfuncs.c @@ -674,8 +674,8 @@ fail: * * This is for use by PyArray_InnerProduct. It is assumed on entry that the * arrays ap1 and ap2 have a common data type given by typenum that is - * float, double, cfloat, or cdouble and have dimension <= 2, and have the - * contiguous flag set. The * __numpy_ufunc__ nonsense is also assumed to + * float, double, cfloat, or cdouble and have dimension <= 2. + * The * __numpy_ufunc__ nonsense is also assumed to * have been taken care of. */ @@ -689,6 +689,24 @@ cblas_innerproduct(int typenum, PyArrayObject *ap1, PyArrayObject *ap2) npy_intp dimensions[NPY_MAXDIMS]; PyTypeObject *subtype; + /* assure contiguous arrays */ + if (!PyArray_IS_C_CONTIGUOUS(ap1)) { + PyObject *op1 = PyArray_NewCopy(ap1, NPY_CORDER); + Py_DECREF(ap1); + ap1 = (PyArrayObject *)op1; + if (ap1 == NULL) { + goto fail; + } + } + if (!PyArray_IS_C_CONTIGUOUS(ap2)) { + PyObject *op2 = PyArray_NewCopy(ap2, NPY_CORDER); + Py_DECREF(ap2); + ap2 = (PyArrayObject *)op2; + if (ap2 == NULL) { + goto fail; + } + } + if (PyArray_NDIM(ap1) == 0 || PyArray_NDIM(ap2) == 0) { /* One of ap1 or ap2 is a scalar */ if (PyArray_NDIM(ap1) == 0) { diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 85b0e5519..41b54a18b 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -4728,6 +4728,22 @@ class TestInner(TestCase): p = np.inner(a, a) assert_almost_equal(p, 0, decimal=14) + def test_inner_product_with_various_contiguities(self): + # github issue 6532 + for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?': + # check an inner product involving a matrix transpose + A = np.array([[1, 2], [3, 4]], dtype=dt) + B = np.array([[1, 3], [2, 4]], dtype=dt) + C = np.array([1, 1], dtype=dt) + desired = np.array([4, 6], dtype=dt) + assert_equal(np.inner(A.T, C), desired) + assert_equal(np.inner(B, C), desired) + # check an inner product involving an aliased and reversed view + a = np.arange(5).astype(dt) + b = a[::-1] + desired = np.array(10, dtype=dt).item() + assert_equal(np.inner(b, a), desired) + class TestSummarization(TestCase): def test_1d(self): |