Skip to content

Commit f331df8

Browse files
committed
adds many bindings to test more problems in DNLP
1 parent f8c57a9 commit f331df8

16 files changed

Lines changed: 593 additions & 0 deletions

File tree

python/atoms/asinh.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ATOM_ASINH_H
2+
#define ATOM_ASINH_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_asinh(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
if (!PyArg_ParseTuple(args, "O", &child_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
15+
if (!child)
16+
{
17+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
18+
return NULL;
19+
}
20+
21+
expr *node = new_asinh(child);
22+
if (!node)
23+
{
24+
PyErr_SetString(PyExc_RuntimeError, "failed to create asinh node");
25+
return NULL;
26+
}
27+
expr_retain(node); /* Capsule owns a reference */
28+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
29+
}
30+
31+
#endif /* ATOM_ASINH_H */

python/atoms/atanh.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ATOM_ATANH_H
2+
#define ATOM_ATANH_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_atanh(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
if (!PyArg_ParseTuple(args, "O", &child_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
15+
if (!child)
16+
{
17+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
18+
return NULL;
19+
}
20+
21+
expr *node = new_atanh(child);
22+
if (!node)
23+
{
24+
PyErr_SetString(PyExc_RuntimeError, "failed to create atanh node");
25+
return NULL;
26+
}
27+
expr_retain(node); /* Capsule owns a reference */
28+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
29+
}
30+
31+
#endif /* ATOM_ATANH_H */

python/atoms/cos.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ATOM_COS_H
2+
#define ATOM_COS_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_cos(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
if (!PyArg_ParseTuple(args, "O", &child_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
15+
if (!child)
16+
{
17+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
18+
return NULL;
19+
}
20+
21+
expr *node = new_cos(child);
22+
if (!node)
23+
{
24+
PyErr_SetString(PyExc_RuntimeError, "failed to create cos node");
25+
return NULL;
26+
}
27+
expr_retain(node); /* Capsule owns a reference */
28+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
29+
}
30+
31+
#endif /* ATOM_COS_H */

python/atoms/entr.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ATOM_ENTR_H
2+
#define ATOM_ENTR_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_entr(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
if (!PyArg_ParseTuple(args, "O", &child_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
15+
if (!child)
16+
{
17+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
18+
return NULL;
19+
}
20+
21+
expr *node = new_entr(child);
22+
if (!node)
23+
{
24+
PyErr_SetString(PyExc_RuntimeError, "failed to create entr node");
25+
return NULL;
26+
}
27+
expr_retain(node); /* Capsule owns a reference */
28+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
29+
}
30+
31+
#endif /* ATOM_ENTR_H */

python/atoms/left_matmul.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef ATOM_LEFT_MATMUL_H
2+
#define ATOM_LEFT_MATMUL_H
3+
4+
#include "common.h"
5+
#include "bivariate.h"
6+
7+
/* Left matrix multiplication: A @ f(x) where A is a constant matrix */
8+
static PyObject *py_make_left_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_left_matmul(child, A);
52+
free_csr_matrix(A);
53+
54+
if (!node)
55+
{
56+
PyErr_SetString(PyExc_RuntimeError, "failed to create left_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_LEFT_MATMUL_H */

python/atoms/logistic.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ATOM_LOGISTIC_H
2+
#define ATOM_LOGISTIC_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_logistic(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
if (!PyArg_ParseTuple(args, "O", &child_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
15+
if (!child)
16+
{
17+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
18+
return NULL;
19+
}
20+
21+
expr *node = new_logistic(child);
22+
if (!node)
23+
{
24+
PyErr_SetString(PyExc_RuntimeError, "failed to create logistic node");
25+
return NULL;
26+
}
27+
expr_retain(node); /* Capsule owns a reference */
28+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
29+
}
30+
31+
#endif /* ATOM_LOGISTIC_H */

python/atoms/multiply.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef ATOM_MULTIPLY_H
2+
#define ATOM_MULTIPLY_H
3+
4+
#include "common.h"
5+
#include "bivariate.h"
6+
7+
static PyObject *py_make_multiply(PyObject *self, PyObject *args)
8+
{
9+
PyObject *left_capsule, *right_capsule;
10+
if (!PyArg_ParseTuple(args, "OO", &left_capsule, &right_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *left = (expr *) PyCapsule_GetPointer(left_capsule, EXPR_CAPSULE_NAME);
15+
expr *right = (expr *) PyCapsule_GetPointer(right_capsule, EXPR_CAPSULE_NAME);
16+
if (!left || !right)
17+
{
18+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
19+
return NULL;
20+
}
21+
22+
expr *node = new_elementwise_mult(left, right);
23+
if (!node)
24+
{
25+
PyErr_SetString(PyExc_RuntimeError, "failed to create multiply node");
26+
return NULL;
27+
}
28+
expr_retain(node); /* Capsule owns a reference */
29+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
30+
}
31+
32+
#endif /* ATOM_MULTIPLY_H */

python/atoms/power.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef ATOM_POWER_H
2+
#define ATOM_POWER_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_power(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
double p;
11+
if (!PyArg_ParseTuple(args, "Od", &child_capsule, &p))
12+
{
13+
return NULL;
14+
}
15+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
16+
if (!child)
17+
{
18+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
19+
return NULL;
20+
}
21+
22+
expr *node = new_power(child, p);
23+
if (!node)
24+
{
25+
PyErr_SetString(PyExc_RuntimeError, "failed to create power node");
26+
return NULL;
27+
}
28+
expr_retain(node); /* Capsule owns a reference */
29+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
30+
}
31+
32+
#endif /* ATOM_POWER_H */

python/atoms/right_matmul.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 */

python/atoms/sin.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ATOM_SIN_H
2+
#define ATOM_SIN_H
3+
4+
#include "common.h"
5+
#include "elementwise_univariate.h"
6+
7+
static PyObject *py_make_sin(PyObject *self, PyObject *args)
8+
{
9+
PyObject *child_capsule;
10+
if (!PyArg_ParseTuple(args, "O", &child_capsule))
11+
{
12+
return NULL;
13+
}
14+
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
15+
if (!child)
16+
{
17+
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
18+
return NULL;
19+
}
20+
21+
expr *node = new_sin(child);
22+
if (!node)
23+
{
24+
PyErr_SetString(PyExc_RuntimeError, "failed to create sin node");
25+
return NULL;
26+
}
27+
expr_retain(node); /* Capsule owns a reference */
28+
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
29+
}
30+
31+
#endif /* ATOM_SIN_H */

0 commit comments

Comments
 (0)