Ejemplo n.º 1
0
PyMODINIT_FUNC initnumpy_half(void)
{
    PyObject *m;
    int halfNum;
    PyArray_Descr *descr;

    m = Py_InitModule("numpy_half", HalfMethods);
    if (m == NULL) {
        return;
    }

    /* Make sure NumPy is initialized */
    import_array();

    /* Register the half array scalar type */
#if defined(NPY_PY3K)
    PyHalfArrType_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
#else
    PyHalfArrType_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES;
#endif
    PyHalfArrType_Type.tp_new = half_arrtype_new;
    PyHalfArrType_Type.tp_richcompare = gentype_richcompare;
    PyHalfArrType_Type.tp_hash = halftype_hash;
    PyHalfArrType_Type.tp_repr = halftype_repr;
    PyHalfArrType_Type.tp_str = halftype_str;
    PyHalfArrType_Type.tp_base = &PyFloatingArrType_Type;
    if (PyType_Ready(&PyHalfArrType_Type) < 0) {
        PyErr_Print();
        PyErr_SetString(PyExc_SystemError, "could not initialize PyHalfArrType_Type");
        return;
    }

    /* The array functions */
    PyArray_InitArrFuncs(&_PyHalf_ArrFuncs);
    _PyHalf_ArrFuncs.getitem = (PyArray_GetItemFunc*)HALF_getitem;
    _PyHalf_ArrFuncs.setitem = (PyArray_SetItemFunc*)HALF_setitem;
    /* copy copyswap and copyswapn from uint16 */
    descr = PyArray_DescrFromType(NPY_UINT16);
    _PyHalf_ArrFuncs.copyswap = descr->f->copyswap;
    _PyHalf_ArrFuncs.copyswapn = descr->f->copyswapn;
    Py_DECREF(descr);
    _PyHalf_ArrFuncs.compare = (PyArray_CompareFunc*)HALF_compare;
    _PyHalf_ArrFuncs.argmax = (PyArray_ArgFunc*)HALF_argmax;
    _PyHalf_ArrFuncs.dotfunc = (PyArray_DotFunc*)HALF_dot;
    /*
    _PyHalf_ArrFuncs.scanfunc = (PyArray_ScanFunc*)HALF_scan;
    _PyHalf_ArrFuncs.fromstr = (PyArray_FromStrFunc*)HALF_fromstr;
    */
    _PyHalf_ArrFuncs.nonzero = (PyArray_NonzeroFunc*)HALF_nonzero;
    _PyHalf_ArrFuncs.fill = (PyArray_FillFunc*)HALF_fill;
    _PyHalf_ArrFuncs.fillwithscalar = (PyArray_FillWithScalarFunc*)HALF_fillwithscalar;
    _PyHalf_ArrFuncs.cast[NPY_BOOL] = (PyArray_VectorUnaryFunc*)HALF_to_BOOL;
    _PyHalf_ArrFuncs.cast[NPY_BYTE] = (PyArray_VectorUnaryFunc*)HALF_to_BYTE;
    _PyHalf_ArrFuncs.cast[NPY_UBYTE] = (PyArray_VectorUnaryFunc*)HALF_to_UBYTE;
    _PyHalf_ArrFuncs.cast[NPY_SHORT] = (PyArray_VectorUnaryFunc*)HALF_to_SHORT;
    _PyHalf_ArrFuncs.cast[NPY_USHORT] = (PyArray_VectorUnaryFunc*)HALF_to_USHORT;
    _PyHalf_ArrFuncs.cast[NPY_INT] = (PyArray_VectorUnaryFunc*)HALF_to_INT;
    _PyHalf_ArrFuncs.cast[NPY_UINT] = (PyArray_VectorUnaryFunc*)HALF_to_UINT;
    _PyHalf_ArrFuncs.cast[NPY_LONG] = (PyArray_VectorUnaryFunc*)HALF_to_LONG;
    _PyHalf_ArrFuncs.cast[NPY_ULONG] = (PyArray_VectorUnaryFunc*)HALF_to_ULONG;
    _PyHalf_ArrFuncs.cast[NPY_LONGLONG] = (PyArray_VectorUnaryFunc*)HALF_to_LONGLONG;
    _PyHalf_ArrFuncs.cast[NPY_ULONGLONG] = (PyArray_VectorUnaryFunc*)HALF_to_ULONGLONG;
    _PyHalf_ArrFuncs.cast[NPY_FLOAT] = (PyArray_VectorUnaryFunc*)HALF_to_FLOAT;
    _PyHalf_ArrFuncs.cast[NPY_DOUBLE] = (PyArray_VectorUnaryFunc*)HALF_to_DOUBLE;
    _PyHalf_ArrFuncs.cast[NPY_LONGDOUBLE] = (PyArray_VectorUnaryFunc*)HALF_to_LONGDOUBLE;
    _PyHalf_ArrFuncs.cast[NPY_CFLOAT] = (PyArray_VectorUnaryFunc*)HALF_to_CFLOAT;
    _PyHalf_ArrFuncs.cast[NPY_CDOUBLE] = (PyArray_VectorUnaryFunc*)HALF_to_CDOUBLE;
    _PyHalf_ArrFuncs.cast[NPY_CLONGDOUBLE] = (PyArray_VectorUnaryFunc*)HALF_to_CLONGDOUBLE;

    /* The half array descr */
    half_descr = PyObject_New(PyArray_Descr, &PyArrayDescr_Type);
    half_descr->typeobj = &PyHalfArrType_Type;
    half_descr->kind = 'f';
    half_descr->type = 'j';
    half_descr->byteorder = '=';
    half_descr->type_num = 0; /* assigned at registration */
    half_descr->elsize = 2;
    half_descr->alignment = 2;
    half_descr->subarray = NULL;
    half_descr->fields = NULL;
    half_descr->names = NULL;
    half_descr->f = &_PyHalf_ArrFuncs;


    Py_INCREF(&PyHalfArrType_Type);
    halfNum = PyArray_RegisterDataType(half_descr);

    if (halfNum < 0)
        return;

    register_cast_function(NPY_BOOL, halfNum, (PyArray_VectorUnaryFunc*)BOOL_to_HALF);
    register_cast_function(NPY_BYTE, halfNum, (PyArray_VectorUnaryFunc*)BYTE_to_HALF);
    register_cast_function(NPY_UBYTE, halfNum, (PyArray_VectorUnaryFunc*)UBYTE_to_HALF);
    register_cast_function(NPY_SHORT, halfNum, (PyArray_VectorUnaryFunc*)SHORT_to_HALF);
    register_cast_function(NPY_USHORT, halfNum, (PyArray_VectorUnaryFunc*)USHORT_to_HALF);
    register_cast_function(NPY_INT, halfNum, (PyArray_VectorUnaryFunc*)INT_to_HALF);
    register_cast_function(NPY_UINT, halfNum, (PyArray_VectorUnaryFunc*)UINT_to_HALF);
    register_cast_function(NPY_LONG, halfNum, (PyArray_VectorUnaryFunc*)LONG_to_HALF);
    register_cast_function(NPY_ULONG, halfNum, (PyArray_VectorUnaryFunc*)ULONG_to_HALF);
    register_cast_function(NPY_LONGLONG, halfNum, (PyArray_VectorUnaryFunc*)LONGLONG_to_HALF);
    register_cast_function(NPY_ULONGLONG, halfNum, (PyArray_VectorUnaryFunc*)ULONGLONG_to_HALF);
    register_cast_function(NPY_FLOAT, halfNum, (PyArray_VectorUnaryFunc*)FLOAT_to_HALF);
    register_cast_function(NPY_DOUBLE, halfNum, (PyArray_VectorUnaryFunc*)DOUBLE_to_HALF);
    register_cast_function(NPY_LONGDOUBLE, halfNum, (PyArray_VectorUnaryFunc*)LONGDOUBLE_to_HALF);
    register_cast_function(NPY_CFLOAT, halfNum, (PyArray_VectorUnaryFunc*)CFLOAT_to_HALF);
    register_cast_function(NPY_CDOUBLE, halfNum, (PyArray_VectorUnaryFunc*)CDOUBLE_to_HALF);
    register_cast_function(NPY_CLONGDOUBLE, halfNum, (PyArray_VectorUnaryFunc*)CLONGDOUBLE_to_HALF);

    PyArray_RegisterCanCast(half_descr, NPY_FLOAT, NPY_NOSCALAR);
    PyArray_RegisterCanCast(half_descr, NPY_DOUBLE, NPY_NOSCALAR);
    PyArray_RegisterCanCast(half_descr, NPY_LONGDOUBLE, NPY_NOSCALAR);
    PyArray_RegisterCanCast(half_descr, NPY_CFLOAT, NPY_NOSCALAR);
    PyArray_RegisterCanCast(half_descr, NPY_CDOUBLE, NPY_NOSCALAR);
    PyArray_RegisterCanCast(half_descr, NPY_CLONGDOUBLE, NPY_NOSCALAR);

    PyModule_AddObject(m, "float16", (PyObject *)&PyHalfArrType_Type);
}
Ejemplo n.º 2
0
PyMODINIT_FUNC initnumpy_quaternion(void)
{
    PyObject *m;
    int quaternionNum;
    PyObject* numpy = PyImport_ImportModule("numpy");
    PyObject* numpy_dict = PyModule_GetDict(numpy);
    int arg_types[3];

    m = Py_InitModule("numpy_quaternion", QuaternionMethods);
    if (m == NULL) {
        return;
    }

    /* Make sure NumPy is initialized */
    import_array();
    import_umath();

    /* Register the quaternion array scalar type */
#if defined(NPY_PY3K)
    PyQuaternionArrType_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
#else
    PyQuaternionArrType_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES;
#endif
    PyQuaternionArrType_Type.tp_new = quaternion_arrtype_new;
    PyQuaternionArrType_Type.tp_richcompare = gentype_richcompare;
    PyQuaternionArrType_Type.tp_hash = quaternion_arrtype_hash;
    PyQuaternionArrType_Type.tp_repr = quaternion_arrtype_repr;
    PyQuaternionArrType_Type.tp_str = quaternion_arrtype_str;
    PyQuaternionArrType_Type.tp_base = &PyGenericArrType_Type;
    if (PyType_Ready(&PyQuaternionArrType_Type) < 0) {
        PyErr_Print();
        PyErr_SetString(PyExc_SystemError, "could not initialize PyQuaternionArrType_Type");
        return;
    }

    /* The array functions */
    PyArray_InitArrFuncs(&_PyQuaternion_ArrFuncs);
    _PyQuaternion_ArrFuncs.getitem = (PyArray_GetItemFunc*)QUATERNION_getitem;
    _PyQuaternion_ArrFuncs.setitem = (PyArray_SetItemFunc*)QUATERNION_setitem;
    _PyQuaternion_ArrFuncs.copyswap = (PyArray_CopySwapFunc*)QUATERNION_copyswap;
    _PyQuaternion_ArrFuncs.copyswapn = (PyArray_CopySwapNFunc*)QUATERNION_copyswapn;
    _PyQuaternion_ArrFuncs.compare = (PyArray_CompareFunc*)QUATERNION_compare;
    _PyQuaternion_ArrFuncs.argmax = (PyArray_ArgFunc*)QUATERNION_argmax;
    _PyQuaternion_ArrFuncs.nonzero = (PyArray_NonzeroFunc*)QUATERNION_nonzero;
    _PyQuaternion_ArrFuncs.fillwithscalar = (PyArray_FillWithScalarFunc*)QUATERNION_fillwithscalar;

    /* The quaternion array descr */
    quaternion_descr = PyObject_New(PyArray_Descr, &PyArrayDescr_Type);
    quaternion_descr->typeobj = &PyQuaternionArrType_Type;
    quaternion_descr->kind = 'q';
    quaternion_descr->type = 'j';
    quaternion_descr->byteorder = '=';
    quaternion_descr->type_num = 0; /* assigned at registration */
    quaternion_descr->elsize = 8*4;
    quaternion_descr->alignment = 8;
    quaternion_descr->subarray = NULL;
    quaternion_descr->fields = NULL;
    quaternion_descr->names = NULL;
    quaternion_descr->f = &_PyQuaternion_ArrFuncs;

    Py_INCREF(&PyQuaternionArrType_Type);
    quaternionNum = PyArray_RegisterDataType(quaternion_descr);

    if (quaternionNum < 0)
        return;

    register_cast_function(NPY_BOOL, quaternionNum, (PyArray_VectorUnaryFunc*)BOOL_to_quaternion);
    register_cast_function(NPY_BYTE, quaternionNum, (PyArray_VectorUnaryFunc*)BYTE_to_quaternion);
    register_cast_function(NPY_UBYTE, quaternionNum, (PyArray_VectorUnaryFunc*)UBYTE_to_quaternion);
    register_cast_function(NPY_SHORT, quaternionNum, (PyArray_VectorUnaryFunc*)SHORT_to_quaternion);
    register_cast_function(NPY_USHORT, quaternionNum, (PyArray_VectorUnaryFunc*)USHORT_to_quaternion);
    register_cast_function(NPY_INT, quaternionNum, (PyArray_VectorUnaryFunc*)INT_to_quaternion);
    register_cast_function(NPY_UINT, quaternionNum, (PyArray_VectorUnaryFunc*)UINT_to_quaternion);
    register_cast_function(NPY_LONG, quaternionNum, (PyArray_VectorUnaryFunc*)LONG_to_quaternion);
    register_cast_function(NPY_ULONG, quaternionNum, (PyArray_VectorUnaryFunc*)ULONG_to_quaternion);
    register_cast_function(NPY_LONGLONG, quaternionNum, (PyArray_VectorUnaryFunc*)LONGLONG_to_quaternion);
    register_cast_function(NPY_ULONGLONG, quaternionNum, (PyArray_VectorUnaryFunc*)ULONGLONG_to_quaternion);
    register_cast_function(NPY_FLOAT, quaternionNum, (PyArray_VectorUnaryFunc*)FLOAT_to_quaternion);
    register_cast_function(NPY_DOUBLE, quaternionNum, (PyArray_VectorUnaryFunc*)DOUBLE_to_quaternion);
    register_cast_function(NPY_LONGDOUBLE, quaternionNum, (PyArray_VectorUnaryFunc*)LONGDOUBLE_to_quaternion);
    register_cast_function(NPY_CFLOAT, quaternionNum, (PyArray_VectorUnaryFunc*)CFLOAT_to_quaternion);
    register_cast_function(NPY_CDOUBLE, quaternionNum, (PyArray_VectorUnaryFunc*)CDOUBLE_to_quaternion);
    register_cast_function(NPY_CLONGDOUBLE, quaternionNum, (PyArray_VectorUnaryFunc*)CLONGDOUBLE_to_quaternion);

#define REGISTER_UFUNC(name)\
    PyUFunc_RegisterLoopForType((PyUFuncObject *)PyDict_GetItemString(numpy_dict, #name),\
            quaternion_descr->type_num, quaternion_##name##_ufunc, arg_types, NULL)

#define REGISTER_SCALAR_UFUNC(name)\
    PyUFunc_RegisterLoopForType((PyUFuncObject *)PyDict_GetItemString(numpy_dict, #name),\
            quaternion_descr->type_num, quaternion_##name##_scalar_ufunc, arg_types, NULL)

    /* quat -> bool */
    arg_types[0] = quaternion_descr->type_num;
    arg_types[1] = NPY_BOOL;

    REGISTER_UFUNC(isnan);
    REGISTER_UFUNC(isinf);
    REGISTER_UFUNC(isfinite);
    /* quat -> double */
    arg_types[1] = NPY_DOUBLE;

    REGISTER_UFUNC(absolute);

    /* quat -> quat */
    arg_types[1] = quaternion_descr->type_num;

    REGISTER_UFUNC(log);
    REGISTER_UFUNC(exp);
    REGISTER_UFUNC(negative);
    REGISTER_UFUNC(conjugate);

    /* quat, quat -> bool */

    arg_types[2] = NPY_BOOL;

    REGISTER_UFUNC(equal);
    REGISTER_UFUNC(not_equal);
    REGISTER_UFUNC(less);
    REGISTER_UFUNC(less_equal);

    /* quat, double -> quat */

    arg_types[1] = NPY_DOUBLE;
    arg_types[2] = quaternion_descr->type_num;

    REGISTER_SCALAR_UFUNC(multiply);
    REGISTER_SCALAR_UFUNC(divide);
    REGISTER_SCALAR_UFUNC(power);

    /* quat, quat -> quat */

    arg_types[1] = quaternion_descr->type_num;

    REGISTER_UFUNC(add);
    REGISTER_UFUNC(subtract);
    REGISTER_UFUNC(multiply);
    REGISTER_UFUNC(divide);
    REGISTER_UFUNC(power);
    REGISTER_UFUNC(copysign);

    PyModule_AddObject(m, "quaternion", (PyObject *)&PyQuaternionArrType_Type);
}