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