/
sobol_obj.c
163 lines (138 loc) · 4.38 KB
/
sobol_obj.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <sobol_obj.h>
#include <limits.h>
static void
SobolSampler_dealloc(SobolSampler* self)
{
gsl_qrng_free(self->_rng);
self->ob_type->tp_free((PyObject*)self);
}
static const char SobolSampler_dims__doc__[] =
"Number of dimensions";
static PyObject*
SobolSampler_dims(PyObject* self)
{
SobolSampler* s = (SobolSampler *)self;
return PyInt_FromSsize_t(s->_dims);
}
static const char SobolSampler_size__doc__[] =
"Size of the sampler";
static PyObject*
SobolSampler_size(PyObject* self)
{
SobolSampler* s = (SobolSampler *)self;
if (s->_is_inf)
{
Py_INCREF(Py_None);
return Py_None;
}
else
return PyInt_FromSsize_t(s->_size);
}
static PyObject*
SobolSampler_iter(PyObject *self)
{
Py_INCREF(self);
return self;
}
static PyObject*
SobolSampler_iternext(PyObject *self)
{
SobolSampler* s = (SobolSampler *)self;
if (s->_size == 0)
{
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
int arr_dims[1] = {s->_dims};
PyArrayObject* arr = (PyArrayObject *)PyArray_SimpleNew(1 /*nd*/, arr_dims, NPY_DOUBLE);
if (!arr) return NULL;
gsl_qrng_get(s->_rng, (double *)PyArray_DATA(arr));
if (!s->_is_inf) --s->_size;
return PyArray_Return(arr);
}
static PyMemberDef SobolSampler_members[] = {
{NULL} /* Sentinel */
};
static PyMethodDef SobolSampler_methods[] = {
{"dims", (PyCFunction)SobolSampler_dims, METH_NOARGS, SobolSampler_dims__doc__},
{"size", (PyCFunction)SobolSampler_size, METH_NOARGS, SobolSampler_size__doc__},
{NULL} /* Sentinel */
};
static PyTypeObject SobolSamplerType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"sobol._SoolSampler", /*tp_name*/
sizeof(SobolSampler), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)SobolSampler_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
"Sobol sampler", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
SobolSampler_iter, /* tp_iter */
SobolSampler_iternext, /* tp_iternext */
SobolSampler_methods, /* tp_methods */
SobolSampler_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
int SobolSampler_Register(PyObject* m)
{
SobolSamplerType.tp_new = PyType_GenericNew;
int ret = 0;
if ((ret = PyType_Ready(&SobolSamplerType)) < 0)
return ret;
Py_INCREF(&SobolSamplerType);
PyModule_AddObject(m, "_SobolSampler", (PyObject *)&SobolSamplerType);
return 0;
}
SobolSampler* SobolSampler_New(size_t dims, PyObject* size)
{
size_t _size = 0;
size_t _is_inf = 0;
if (size == Py_None)
{
_size = 1;
_is_inf = 1;
}
else if (PyInt_Check(size) || PyLong_Check(size))
{
_size = PyInt_AsSsize_t(size);
_is_inf = 0;
}
else
{
PyErr_SetString(PyExc_TypeError, "size must be an integer, or None");
return NULL;
}
SobolSampler* s = PyObject_New(SobolSampler, &SobolSamplerType);
if (!s) return NULL;
s->_size = _size;
s->_is_inf = _is_inf;
s->_dims = dims;
s->_rng = gsl_qrng_alloc(gsl_qrng_sobol, dims);
return s;
}