|
| 1 | +#ifndef ATOM_RIGHT_MATMUL_H |
| 2 | +#define ATOM_RIGHT_MATMUL_H |
| 3 | + |
| 4 | +#include "common.h" |
| 5 | +#include "bivariate.h" |
| 6 | + |
| 7 | +/* Right matrix multiplication: f(x) @ A where A is a constant matrix */ |
| 8 | +static PyObject *py_make_right_matmul(PyObject *self, PyObject *args) |
| 9 | +{ |
| 10 | + PyObject *child_capsule; |
| 11 | + PyObject *data_obj, *indices_obj, *indptr_obj; |
| 12 | + int m, n; |
| 13 | + if (!PyArg_ParseTuple(args, "OOOOii", &child_capsule, &data_obj, &indices_obj, |
| 14 | + &indptr_obj, &m, &n)) |
| 15 | + { |
| 16 | + return NULL; |
| 17 | + } |
| 18 | + |
| 19 | + expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME); |
| 20 | + if (!child) |
| 21 | + { |
| 22 | + PyErr_SetString(PyExc_ValueError, "invalid child capsule"); |
| 23 | + return NULL; |
| 24 | + } |
| 25 | + |
| 26 | + PyArrayObject *data_array = |
| 27 | + (PyArrayObject *) PyArray_FROM_OTF(data_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); |
| 28 | + PyArrayObject *indices_array = (PyArrayObject *) PyArray_FROM_OTF( |
| 29 | + indices_obj, NPY_INT32, NPY_ARRAY_IN_ARRAY); |
| 30 | + PyArrayObject *indptr_array = (PyArrayObject *) PyArray_FROM_OTF( |
| 31 | + indptr_obj, NPY_INT32, NPY_ARRAY_IN_ARRAY); |
| 32 | + |
| 33 | + if (!data_array || !indices_array || !indptr_array) |
| 34 | + { |
| 35 | + Py_XDECREF(data_array); |
| 36 | + Py_XDECREF(indices_array); |
| 37 | + Py_XDECREF(indptr_array); |
| 38 | + return NULL; |
| 39 | + } |
| 40 | + |
| 41 | + int nnz = (int) PyArray_SIZE(data_array); |
| 42 | + CSR_Matrix *A = new_csr_matrix(m, n, nnz); |
| 43 | + memcpy(A->x, PyArray_DATA(data_array), nnz * sizeof(double)); |
| 44 | + memcpy(A->i, PyArray_DATA(indices_array), nnz * sizeof(int)); |
| 45 | + memcpy(A->p, PyArray_DATA(indptr_array), (m + 1) * sizeof(int)); |
| 46 | + |
| 47 | + Py_DECREF(data_array); |
| 48 | + Py_DECREF(indices_array); |
| 49 | + Py_DECREF(indptr_array); |
| 50 | + |
| 51 | + expr *node = new_right_matmul(child, A); |
| 52 | + free_csr_matrix(A); |
| 53 | + |
| 54 | + if (!node) |
| 55 | + { |
| 56 | + PyErr_SetString(PyExc_RuntimeError, "failed to create right_matmul node"); |
| 57 | + return NULL; |
| 58 | + } |
| 59 | + expr_retain(node); /* Capsule owns a reference */ |
| 60 | + return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor); |
| 61 | +} |
| 62 | + |
| 63 | +#endif /* ATOM_RIGHT_MATMUL_H */ |
0 commit comments