| 1 |
import pytest |
| 2 |
import os |
| 3 |
import sys |
| 4 |
from pypy.interpreter.error import OperationError |
| 5 |
from pypy.module.cpyext.pyobject import make_ref, decref |
| 6 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 7 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 8 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 9 |
from pypy.module.micronumpy.ndarray import W_NDimArray |
| 10 |
from pypy.module.micronumpy.descriptor import get_dtype_cache |
| 11 |
import pypy.module.micronumpy.constants as NPY |
| 12 |
from pypy.module.cpyext.ndarrayobject import ( |
| 13 |
_PyArray_FromAny, _PyArray_FromObject) |
| 14 |
|
| 15 |
pytest.skip("Micronumpy not yet supported on py3k.") |
| 16 |
|
| 17 |
def scalar(space): |
| 18 |
dtype = get_dtype_cache(space).w_float64dtype |
| 19 |
return W_NDimArray.new_scalar(space, dtype, space.wrap(10.)) |
| 20 |
|
| 21 |
def array(space, shape, order=NPY.CORDER): |
| 22 |
dtype = get_dtype_cache(space).w_float64dtype |
| 23 |
return W_NDimArray.from_shape(space, shape, dtype, order=order) |
| 24 |
|
| 25 |
def iarray(space, shape, order=NPY.CORDER): |
| 26 |
dtype = get_dtype_cache(space).w_int64dtype |
| 27 |
return W_NDimArray.from_shape(space, shape, dtype, order=order) |
| 28 |
|
| 29 |
|
| 30 |
NULL = lltype.nullptr(rffi.VOIDP.TO) |
| 31 |
|
| 32 |
class TestNDArrayObject(BaseApiTest): |
| 33 |
spaceconfig = AppTestCpythonExtensionBase.spaceconfig.copy() |
| 34 |
spaceconfig['usemodules'].append('micronumpy') |
| 35 |
|
| 36 |
def test_Check(self, space, api): |
| 37 |
a = array(space, [10, 5, 3]) |
| 38 |
x = space.wrap(10.) |
| 39 |
assert api._PyArray_Check(a) |
| 40 |
assert api._PyArray_CheckExact(a) |
| 41 |
assert not api._PyArray_Check(x) |
| 42 |
assert not api._PyArray_CheckExact(x) |
| 43 |
|
| 44 |
def test_FLAGS(self, space, api): |
| 45 |
s = array(space, [10]) |
| 46 |
c = array(space, [10, 5, 3], order=NPY.CORDER) |
| 47 |
f = array(space, [10, 5, 3], order=NPY.FORTRANORDER) |
| 48 |
assert api._PyArray_FLAGS(s) & 0x0001 |
| 49 |
assert api._PyArray_FLAGS(s) & 0x0002 |
| 50 |
assert api._PyArray_FLAGS(c) & 0x0001 |
| 51 |
assert api._PyArray_FLAGS(f) & 0x0002 |
| 52 |
assert not api._PyArray_FLAGS(c) & 0x0002 |
| 53 |
assert not api._PyArray_FLAGS(f) & 0x0001 |
| 54 |
|
| 55 |
def test_NDIM(self, space, api): |
| 56 |
a = array(space, [10, 5, 3]) |
| 57 |
assert api._PyArray_NDIM(a) == 3 |
| 58 |
|
| 59 |
def test_DIM(self, space, api): |
| 60 |
a = array(space, [10, 5, 3]) |
| 61 |
assert api._PyArray_DIM(a, 1) == 5 |
| 62 |
|
| 63 |
def test_STRIDE(self, space, api): |
| 64 |
a = array(space, [10, 5, 3], ) |
| 65 |
assert api._PyArray_STRIDE(a, 1) == a.implementation.get_strides()[1] |
| 66 |
|
| 67 |
def test_SIZE(self, space, api): |
| 68 |
a = array(space, [10, 5, 3]) |
| 69 |
assert api._PyArray_SIZE(a) == 150 |
| 70 |
|
| 71 |
def test_ITEMSIZE(self, space, api): |
| 72 |
a = array(space, [10, 5, 3]) |
| 73 |
assert api._PyArray_ITEMSIZE(a) == 8 |
| 74 |
|
| 75 |
def test_NBYTES(self, space, api): |
| 76 |
a = array(space, [10, 5, 3]) |
| 77 |
assert api._PyArray_NBYTES(a) == 1200 |
| 78 |
|
| 79 |
def test_TYPE(self, space, api): |
| 80 |
a = array(space, [10, 5, 3]) |
| 81 |
assert api._PyArray_TYPE(a) == 12 |
| 82 |
|
| 83 |
def test_DATA(self, space, api): |
| 84 |
a = array(space, [10, 5, 3]) |
| 85 |
addr = api._PyArray_DATA(a) |
| 86 |
addr2 = rffi.cast(rffi.VOIDP, a.implementation.storage) |
| 87 |
assert addr == addr2 |
| 88 |
|
| 89 |
def test_FromAny_scalar(self, space, api): |
| 90 |
a0 = scalar(space) |
| 91 |
assert a0.get_scalar_value().value == 10. |
| 92 |
|
| 93 |
a = api._PyArray_FromAny(a0, None, 0, 0, 0, NULL) |
| 94 |
assert api._PyArray_NDIM(a) == 0 |
| 95 |
|
| 96 |
ptr = rffi.cast(rffi.DOUBLEP, api._PyArray_DATA(a)) |
| 97 |
assert ptr[0] == 10. |
| 98 |
|
| 99 |
def test_FromAny(self, space): |
| 100 |
a = array(space, [10, 5, 3]) |
| 101 |
assert _PyArray_FromAny(space, a, None, 0, 0, 0, NULL) is a |
| 102 |
assert _PyArray_FromAny(space, a, None, 1, 4, 0, NULL) is a |
| 103 |
with pytest.raises(OperationError) as excinfo: |
| 104 |
_PyArray_FromAny(space, a, None, 4, 5, 0, NULL) |
| 105 |
|
| 106 |
def test_FromObject(self, space): |
| 107 |
a = array(space, [10, 5, 3]) |
| 108 |
assert _PyArray_FromObject(space, a, a.get_dtype().num, 0, 0) is a |
| 109 |
with pytest.raises(OperationError) as excinfo: |
| 110 |
_PyArray_FromObject(space, a, 11, 4, 5) |
| 111 |
assert excinfo.value.errorstr(space).find('desired') >= 0 |
| 112 |
|
| 113 |
def test_list_from_fixedptr(self, space, api): |
| 114 |
A = lltype.GcArray(lltype.Float) |
| 115 |
ptr = lltype.malloc(A, 3) |
| 116 |
assert isinstance(ptr, lltype._ptr) |
| 117 |
ptr[0] = 10. |
| 118 |
ptr[1] = 5. |
| 119 |
ptr[2] = 3. |
| 120 |
l = list(ptr) |
| 121 |
assert l == [10., 5., 3.] |
| 122 |
|
| 123 |
def test_list_from_openptr(self, space, api): |
| 124 |
nd = 3 |
| 125 |
a = array(space, [nd]) |
| 126 |
ptr = rffi.cast(rffi.DOUBLEP, api._PyArray_DATA(a)) |
| 127 |
ptr[0] = 10. |
| 128 |
ptr[1] = 5. |
| 129 |
ptr[2] = 3. |
| 130 |
l = [] |
| 131 |
for i in range(nd): |
| 132 |
l.append(ptr[i]) |
| 133 |
assert l == [10., 5., 3.] |
| 134 |
|
| 135 |
def test_SimpleNew_scalar(self, space, api): |
| 136 |
ptr_s = lltype.nullptr(rffi.LONGP.TO) |
| 137 |
a = api._PyArray_SimpleNew(0, ptr_s, 12) |
| 138 |
|
| 139 |
dtype = get_dtype_cache(space).w_float64dtype |
| 140 |
|
| 141 |
a.set_scalar_value(dtype.itemtype.box(10.)) |
| 142 |
assert a.get_scalar_value().value == 10. |
| 143 |
|
| 144 |
def test_SimpleNewFromData_scalar(self, space, api): |
| 145 |
a = array(space, [1]) |
| 146 |
num = api._PyArray_TYPE(a) |
| 147 |
ptr_a = api._PyArray_DATA(a) |
| 148 |
|
| 149 |
x = rffi.cast(rffi.DOUBLEP, ptr_a) |
| 150 |
x[0] = float(10.) |
| 151 |
|
| 152 |
ptr_s = lltype.nullptr(rffi.LONGP.TO) |
| 153 |
|
| 154 |
res = api._PyArray_SimpleNewFromData(0, ptr_s, num, ptr_a) |
| 155 |
assert res.is_scalar() |
| 156 |
assert res.get_scalar_value().value == 10. |
| 157 |
|
| 158 |
def test_SimpleNew(self, space, api): |
| 159 |
shape = [10, 5, 3] |
| 160 |
nd = len(shape) |
| 161 |
|
| 162 |
s = iarray(space, [nd]) |
| 163 |
ptr_s = rffi.cast(rffi.LONGP, api._PyArray_DATA(s)) |
| 164 |
ptr_s[0] = 10 |
| 165 |
ptr_s[1] = 5 |
| 166 |
ptr_s[2] = 3 |
| 167 |
|
| 168 |
a = api._PyArray_SimpleNew(nd, ptr_s, 12) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
ptr_a = api._PyArray_DATA(a) |
| 173 |
|
| 174 |
x = rffi.cast(rffi.DOUBLEP, ptr_a) |
| 175 |
for i in range(150): |
| 176 |
x[i] = float(i) |
| 177 |
|
| 178 |
for i in range(150): |
| 179 |
assert x[i] == float(i) |
| 180 |
|
| 181 |
def test_SimpleNewFromData(self, space, api): |
| 182 |
shape = [10, 5, 3] |
| 183 |
nd = len(shape) |
| 184 |
|
| 185 |
s = iarray(space, [nd]) |
| 186 |
ptr_s = rffi.cast(rffi.LONGP, api._PyArray_DATA(s)) |
| 187 |
ptr_s[0] = 10 |
| 188 |
ptr_s[1] = 5 |
| 189 |
ptr_s[2] = 3 |
| 190 |
|
| 191 |
a = array(space, shape) |
| 192 |
num = api._PyArray_TYPE(a) |
| 193 |
ptr_a = api._PyArray_DATA(a) |
| 194 |
|
| 195 |
x = rffi.cast(rffi.DOUBLEP, ptr_a) |
| 196 |
for i in range(150): |
| 197 |
x[i] = float(i) |
| 198 |
|
| 199 |
res = api._PyArray_SimpleNewFromData(nd, ptr_s, num, ptr_a) |
| 200 |
assert api._PyArray_TYPE(res) == num |
| 201 |
assert api._PyArray_DATA(res) == ptr_a |
| 202 |
for i in range(nd): |
| 203 |
assert api._PyArray_DIM(res, i) == shape[i] |
| 204 |
ptr_r = rffi.cast(rffi.DOUBLEP, api._PyArray_DATA(res)) |
| 205 |
for i in range(150): |
| 206 |
assert ptr_r[i] == float(i) |
| 207 |
res = api._PyArray_SimpleNewFromDataOwning(nd, ptr_s, num, ptr_a) |
| 208 |
x = rffi.cast(rffi.DOUBLEP, ptr_a) |
| 209 |
ptr_r = rffi.cast(rffi.DOUBLEP, api._PyArray_DATA(res)) |
| 210 |
x[20] = -100. |
| 211 |
assert ptr_r[20] == -100. |
| 212 |
|
| 213 |
def test_SimpleNewFromData_complex(self, space, api): |
| 214 |
a = array(space, [2]) |
| 215 |
ptr_a = api._PyArray_DATA(a) |
| 216 |
|
| 217 |
x = rffi.cast(rffi.DOUBLEP, ptr_a) |
| 218 |
x[0] = 3. |
| 219 |
x[1] = 4. |
| 220 |
|
| 221 |
ptr_s = lltype.nullptr(rffi.LONGP.TO) |
| 222 |
|
| 223 |
res = api._PyArray_SimpleNewFromData(0, ptr_s, 15, ptr_a) |
| 224 |
assert res.get_scalar_value().real == 3. |
| 225 |
assert res.get_scalar_value().imag == 4. |
| 226 |
|
| 227 |
def _test_Ufunc_FromFuncAndDataAndSignature(self, space, api): |
| 228 |
pytest.skip('preliminary non-translated test') |
| 229 |
''' |
| 230 |
PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2}; |
| 231 |
char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT }; |
| 232 |
void *array_data[] = {NULL, NULL}; |
| 233 |
ufunc = api.PyUFunc_FromFuncAndDataAndSignature(space, funcs, data, |
| 234 |
types, ntypes, nin, nout, identity, doc, check_return, |
| 235 |
signature) |
| 236 |
''' |
| 237 |
|
| 238 |
def test_ndarray_ref(self, space, api): |
| 239 |
w_obj = space.appexec([], """(): |
| 240 |
import _numpypy |
| 241 |
return _numpypy.multiarray.dtype('int64').type(2)""") |
| 242 |
ref = make_ref(space, w_obj) |
| 243 |
decref(space, ref) |
| 244 |
|
| 245 |
class AppTestNDArray(AppTestCpythonExtensionBase): |
| 246 |
|
| 247 |
def setup_class(cls): |
| 248 |
AppTestCpythonExtensionBase.setup_class.im_func(cls) |
| 249 |
if cls.runappdirect: |
| 250 |
try: |
| 251 |
import numpy |
| 252 |
except ImportError: |
| 253 |
skip('numpy not importable') |
| 254 |
cls.w_numpy_include = [numpy.get_include()] |
| 255 |
else: |
| 256 |
if '__pypy__' in sys.builtin_module_names: |
| 257 |
skip('cannot run tests untranslated on top of PyPy') |
| 258 |
numpy_incl = os.path.abspath(os.path.dirname(__file__) + |
| 259 |
'/../include/_numpypy') |
| 260 |
assert os.path.exists(numpy_incl) |
| 261 |
cls.w_numpy_include = cls.space.wrap([numpy_incl]) |
| 262 |
|
| 263 |
def test_ndarray_object_c(self): |
| 264 |
mod = self.import_extension('foo', [ |
| 265 |
("test_simplenew", "METH_NOARGS", |
| 266 |
''' |
| 267 |
npy_intp dims[2] ={2, 3}; |
| 268 |
PyObject * obj = PyArray_SimpleNew(2, dims, 11); |
| 269 |
return obj; |
| 270 |
''' |
| 271 |
), |
| 272 |
("test_fill", "METH_NOARGS", |
| 273 |
''' |
| 274 |
npy_intp dims[2] ={2, 3}; |
| 275 |
PyObject * obj = PyArray_SimpleNew(2, dims, 1); |
| 276 |
PyArray_FILLWBYTE((PyArrayObject*)obj, 42); |
| 277 |
return obj; |
| 278 |
''' |
| 279 |
), |
| 280 |
("test_copy", "METH_NOARGS", |
| 281 |
''' |
| 282 |
npy_intp dims1[2] ={2, 3}; |
| 283 |
npy_intp dims2[2] ={3, 2}; |
| 284 |
int ok; |
| 285 |
PyObject * obj1 = PyArray_ZEROS(2, dims1, 11, 0); |
| 286 |
PyObject * obj2 = PyArray_ZEROS(2, dims2, 11, 0); |
| 287 |
PyArray_FILLWBYTE((PyArrayObject*)obj2, 42); |
| 288 |
ok = PyArray_CopyInto((PyArrayObject*)obj2, (PyArrayObject*)obj1); |
| 289 |
Py_DECREF(obj2); |
| 290 |
if (ok < 0) |
| 291 |
{ |
| 292 |
/* Should have failed */ |
| 293 |
Py_DECREF(obj1); |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
return obj1; |
| 297 |
''' |
| 298 |
), |
| 299 |
("test_FromAny", "METH_NOARGS", |
| 300 |
''' |
| 301 |
npy_intp dims[2] ={2, 3}; |
| 302 |
PyObject * obj2, * obj1 = PyArray_SimpleNew(2, dims, 1); |
| 303 |
PyArray_FILLWBYTE((PyArrayObject*)obj1, 42); |
| 304 |
obj2 = PyArray_FromAny(obj1, NULL, 0, 0, 0, NULL); |
| 305 |
Py_DECREF(obj1); |
| 306 |
return obj2; |
| 307 |
''' |
| 308 |
), |
| 309 |
("test_FromObject", "METH_NOARGS", |
| 310 |
''' |
| 311 |
npy_intp dims[2] ={2, 3}; |
| 312 |
PyObject * obj2, * obj1 = PyArray_SimpleNew(2, dims, 1); |
| 313 |
PyArray_FILLWBYTE((PyArrayObject*)obj1, 42); |
| 314 |
obj2 = PyArray_FromObject(obj1, 12, 0, 0); |
| 315 |
Py_DECREF(obj1); |
| 316 |
return obj2; |
| 317 |
''' |
| 318 |
), |
| 319 |
("test_DescrFromType", "METH_O", |
| 320 |
""" |
| 321 |
long typenum = PyInt_AsLong(args); |
| 322 |
return PyArray_DescrFromType(typenum); |
| 323 |
""" |
| 324 |
), |
| 325 |
], include_dirs=self.numpy_include, |
| 326 |
prologue=''' |
| 327 |
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
| 328 |
#include <numpy/arrayobject.h> |
| 329 |
#ifdef PYPY_VERSION |
| 330 |
#define PyArray_FromObject _PyArray_FromObject |
| 331 |
#define PyArray_FromAny _PyArray_FromAny |
| 332 |
#endif |
| 333 |
''', |
| 334 |
more_init = ''' |
| 335 |
#ifndef PYPY_VERSION |
| 336 |
import_array(); |
| 337 |
#endif |
| 338 |
''') |
| 339 |
arr = mod.test_simplenew() |
| 340 |
assert arr.shape == (2, 3) |
| 341 |
assert arr.dtype.num == 11 |
| 342 |
arr = mod.test_fill() |
| 343 |
assert arr.shape == (2, 3) |
| 344 |
assert arr.dtype.num == 1 |
| 345 |
assert (arr == 42).all() |
| 346 |
raises(ValueError, mod.test_copy) |
| 347 |
|
| 348 |
arr = mod.test_FromAny() |
| 349 |
arr = mod.test_FromObject() |
| 350 |
dt = mod.test_DescrFromType(11) |
| 351 |
assert dt.num == 11 |
| 352 |
|
| 353 |
def test_pass_ndarray_object_to_c(self): |
| 354 |
if self.runappdirect: |
| 355 |
from numpy import ndarray |
| 356 |
else: |
| 357 |
from _numpypy.multiarray import ndarray |
| 358 |
mod = self.import_extension('foo', [ |
| 359 |
("check_array", "METH_VARARGS", |
| 360 |
''' |
| 361 |
PyObject* obj; |
| 362 |
if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &obj)) |
| 363 |
return NULL; |
| 364 |
Py_INCREF(obj); |
| 365 |
return obj; |
| 366 |
'''), |
| 367 |
], include_dirs=self.numpy_include, |
| 368 |
prologue=''' |
| 369 |
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
| 370 |
#include <numpy/arrayobject.h> |
| 371 |
''', |
| 372 |
more_init = ''' |
| 373 |
#ifndef PYPY_VERSION |
| 374 |
import_array(); |
| 375 |
#endif |
| 376 |
''') |
| 377 |
array = ndarray((3, 4), dtype='d') |
| 378 |
assert mod.check_array(array) is array |
| 379 |
raises(TypeError, "mod.check_array(42)") |
| 380 |
|
| 381 |
def test_ufunc(self): |
| 382 |
if self.runappdirect: |
| 383 |
from numpy import arange |
| 384 |
pytest.xfail('segfaults on cpython: PyUFunc_API == NULL?') |
| 385 |
else: |
| 386 |
from _numpypy.multiarray import arange |
| 387 |
mod = self.import_extension('foo', [ |
| 388 |
("create_ufunc_basic", "METH_NOARGS", |
| 389 |
""" |
| 390 |
PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2}; |
| 391 |
char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT }; |
| 392 |
void *array_data[] = {NULL, NULL}; |
| 393 |
PyObject * retval; |
| 394 |
retval = PyUFunc_FromFuncAndData(funcs, |
| 395 |
array_data, types, 2, 1, 1, PyUFunc_None, |
| 396 |
"times2", "times2_docstring", 0); |
| 397 |
return retval; |
| 398 |
""" |
| 399 |
), |
| 400 |
("create_ufunc_signature", "METH_NOARGS", |
| 401 |
""" |
| 402 |
PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2}; |
| 403 |
char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT }; |
| 404 |
void *array_data[] = {NULL, NULL}; |
| 405 |
PyObject * retval; |
| 406 |
retval = PyUFunc_FromFuncAndDataAndSignature(funcs, |
| 407 |
array_data, types, 2, 1, 1, PyUFunc_None, |
| 408 |
"times2", "times2_docstring", 0, "()->()"); |
| 409 |
return retval; |
| 410 |
"""), |
| 411 |
("create_float_ufunc_3x3", "METH_NOARGS", |
| 412 |
""" |
| 413 |
PyUFuncGenericFunction funcs[] = {&float_func_with_sig_3x3}; |
| 414 |
char types[] = { NPY_FLOAT,NPY_FLOAT}; |
| 415 |
void *array_data[] = {NULL, NULL}; |
| 416 |
return PyUFunc_FromFuncAndDataAndSignature(funcs, |
| 417 |
array_data, types, 1, 1, 1, PyUFunc_None, |
| 418 |
"float_3x3", |
| 419 |
"a ufunc that tests a more complicated signature", |
| 420 |
0, "(m,m)->(m,m)"); |
| 421 |
"""), |
| 422 |
], include_dirs=self.numpy_include, |
| 423 |
prologue=''' |
| 424 |
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
| 425 |
#include <numpy/arrayobject.h> |
| 426 |
#ifndef PYPY_VERSION |
| 427 |
#include <numpy/ufuncobject.h> /*generated by numpy setup.py*/ |
| 428 |
#endif |
| 429 |
typedef void (*PyUFuncGenericFunction) |
| 430 |
(char **args, |
| 431 |
npy_intp *dimensions, |
| 432 |
npy_intp *strides, |
| 433 |
void *innerloopdata); |
| 434 |
#define PyUFunc_None -1 |
| 435 |
void double_times2(char **args, npy_intp *dimensions, |
| 436 |
npy_intp* steps, void* data) |
| 437 |
{ |
| 438 |
npy_intp i; |
| 439 |
npy_intp n; |
| 440 |
char *in, *out; |
| 441 |
npy_intp in_step, out_step; |
| 442 |
double tmp; |
| 443 |
n = dimensions[0]; |
| 444 |
in = args[0]; out=args[1]; |
| 445 |
in_step = steps[0]; out_step = steps[1]; |
| 446 |
|
| 447 |
for (i = 0; i < n; i++) { |
| 448 |
/*BEGIN main ufunc computation*/ |
| 449 |
tmp = *(double *)in; |
| 450 |
tmp *=2.0; |
| 451 |
*((double *)out) = tmp; |
| 452 |
/*END main ufunc computation*/ |
| 453 |
|
| 454 |
in += in_step; |
| 455 |
out += out_step; |
| 456 |
}; |
| 457 |
}; |
| 458 |
void int_times2(char **args, npy_intp *dimensions, |
| 459 |
npy_intp* steps, void* data) |
| 460 |
{ |
| 461 |
npy_intp i; |
| 462 |
npy_intp n = dimensions[0]; |
| 463 |
char *in = args[0], *out=args[1]; |
| 464 |
npy_intp in_step = steps[0], out_step = steps[1]; |
| 465 |
int tmp; |
| 466 |
for (i = 0; i < n; i++) { |
| 467 |
/*BEGIN main ufunc computation*/ |
| 468 |
tmp = *(int *)in; |
| 469 |
tmp *=2.0; |
| 470 |
*((int *)out) = tmp; |
| 471 |
/*END main ufunc computation*/ |
| 472 |
|
| 473 |
in += in_step; |
| 474 |
out += out_step; |
| 475 |
}; |
| 476 |
}; |
| 477 |
void float_func_with_sig_3x3(char ** args, npy_intp * dimensions, |
| 478 |
npy_intp* steps, void* data) |
| 479 |
{ |
| 480 |
int target_dims[] = {1, 3}; |
| 481 |
int target_steps[] = {0, 0, 12, 4, 12, 4}; |
| 482 |
int res = 0; |
| 483 |
int i; |
| 484 |
for (i=0; i<sizeof(target_dims)/sizeof(int); i++) |
| 485 |
if (dimensions[i] != target_dims[i]) |
| 486 |
res += 1; |
| 487 |
for (i=0; i<sizeof(target_steps)/sizeof(int); i++) |
| 488 |
if (steps[i] != target_steps[i]) |
| 489 |
res += +10; |
| 490 |
*((float *)args[1]) = res; |
| 491 |
}; |
| 492 |
|
| 493 |
''', more_init = ''' |
| 494 |
#ifndef PYPY_VERSION |
| 495 |
import_array(); |
| 496 |
#endif |
| 497 |
''') |
| 498 |
sq = arange(18, dtype="float32").reshape(2,3,3) |
| 499 |
float_ufunc = mod.create_float_ufunc_3x3() |
| 500 |
out = float_ufunc(sq) |
| 501 |
assert out[0, 0, 0] == 0 |
| 502 |
|
| 503 |
times2 = mod.create_ufunc_basic() |
| 504 |
arr = arange(12, dtype='i').reshape(3, 4) |
| 505 |
out = times2(arr, extobj=[0, 0, None]) |
| 506 |
assert (out == arr * 2).all() |
| 507 |
|
| 508 |
times2prime = mod.create_ufunc_signature() |
| 509 |
out = times2prime(arr, sig='d->d', extobj=[0, 0, None]) |
| 510 |
assert (out == arr * 2).all() |