Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Optional out parameter for np.dot() #14

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

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ENH: Have non-BLAS dot also support out parameter
  • Loading branch information
Luis Pedro Coelho committed Jan 8, 2011
commit 82e9dadaeca3136b574b69e84c3e81c1a1f1659d
36 changes: 28 additions & 8 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ PyArray_CanCoerceScalar(int thistype, int neededtype,
* priority of ap1 and ap2 into account.
*/
static PyArrayObject *
new_array_for_sum(PyArrayObject *ap1, PyArrayObject *ap2,
new_array_for_sum(PyArrayObject* out, PyArrayObject *ap1, PyArrayObject *ap2,
int nd, intp dimensions[], int typenum)
{
PyArrayObject *ret;
Expand All @@ -597,6 +597,26 @@ new_array_for_sum(PyArrayObject *ap1, PyArrayObject *ap2,
prior1 = prior2 = 0.0;
subtype = Py_TYPE(ap1);
}
if (out) {
if (Py_TYPE(out) != subtype ||
PyArray_NDIM(out) != nd ||
PyArray_TYPE(out) != typenum ||
!PyArray_ISCARRAY(out)) {
PyErr_SetString(PyExc_ValueError,
"output array is not acceptable "
"(must have the right type, nr dimensions, and be a C-Array)");
return 0;
}
int d;
for (d = 0; d != nd; ++d) {
if (dimensions[d] != PyArray_DIM(out, d)) {
PyErr_SetString(PyExc_ValueError,
"output array has wrong dimensions");
return 0;
}
}
return out;
}

ret = (PyArrayObject *)PyArray_New(subtype, nd, dimensions,
typenum, NULL, NULL, 0, 0,
Expand Down Expand Up @@ -666,7 +686,7 @@ PyArray_InnerProduct(PyObject *op1, PyObject *op2)
* Need to choose an output array that can hold a sum
* -- use priority to determine which subtype.
*/
ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum);
ret = new_array_for_sum(NULL, ap1, ap2, nd, dimensions, typenum);
if (ret == NULL) {
goto fail;
}
Expand Down Expand Up @@ -719,7 +739,7 @@ PyArray_InnerProduct(PyObject *op1, PyObject *op2)
* just like inner product but does the swapaxes stuff on the fly
*/
NPY_NO_EXPORT PyObject *
PyArray_MatrixProduct(PyObject *op1, PyObject *op2)
PyArray_MatrixProduct(PyObject *op1, PyObject *op2, PyObject* out)
{
PyArrayObject *ap1, *ap2, *ret = NULL;
PyArrayIterObject *it1, *it2;
Expand Down Expand Up @@ -788,7 +808,7 @@ PyArray_MatrixProduct(PyObject *op1, PyObject *op2)

is1 = ap1->strides[ap1->nd-1]; is2 = ap2->strides[matchDim];
/* Choose which subtype to return */
ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum);
ret = new_array_for_sum(out, ap1, ap2, nd, dimensions, typenum);
if (ret == NULL) {
goto fail;
}
Expand Down Expand Up @@ -968,7 +988,7 @@ _pyarray_correlate(PyArrayObject *ap1, PyArrayObject *ap2, int typenum,
* Need to choose an output array that can hold a sum
* -- use priority to determine which subtype.
*/
ret = new_array_for_sum(ap1, ap2, 1, &length, typenum);
ret = new_array_for_sum(NULL, ap1, ap2, 1, &length, typenum);
if (ret == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1852,12 +1872,12 @@ array_innerproduct(PyObject *NPY_UNUSED(dummy), PyObject *args)
static PyObject *
array_matrixproduct(PyObject *NPY_UNUSED(dummy), PyObject *args)
{
PyObject *v, *a;
PyObject *v, *a, *o = NULL;

if (!PyArg_ParseTuple(args, "OO", &a, &v)) {
if (!PyArg_ParseTuple(args, "OO|O", &a, &v, &o)) {
return NULL;
}
return _ARET(PyArray_MatrixProduct(a, v));
return _ARET(PyArray_MatrixProduct(a, v, o));
}

static PyObject *
Expand Down