armanpy.hpp 7.26 KB
Newer Older
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
// Copyright (C) 2012 thomas.natschlaeger@gmail.com
// 
// This file is part of the ArmaNpy library.
// It is provided without any warranty of fitness
// for any purpose. You can redistribute this file
// and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published
// by the Free Software Foundation, either version 3
// of the License or (at your option) any later version.
// (see http://www.opensource.org/licenses for more info)

///////////////////////////////////////////////////////////////////////////////
// Define types (templated) to allow wrapping of armadillo types as Python
// objects such that they can be used as base objects of numpy arrays
//

#define INIT_ARMA_CAPSULE( MatT )  \
    ArmaCapsulePyType< MatT >::object.tp_new = PyType_GenericNew; \
    if (PyType_Ready(&ArmaCapsulePyType< MatT >::object) < 0) return;

template< typename MatT >
struct ArmaCapsule {
    PyObject_HEAD
    MatT *mat;
} ;

template< typename MatT >
static void ArmaMat_dealloc( PyObject *self )
{
    //std::cerr << "ArmaMat_dealloc( " << self << " )"<< std::endl;
    //((ArmaMat_Capsule *)self)->mat->print("mat");
    delete ((ArmaCapsule<MatT> *)self)->mat;
    self->ob_type->tp_free( self );
};

template<typename MatT >
class ArmaCapsulePyType {
public:
    static PyTypeObject object;
private:
    MatT _dummy;
};


template< typename MatT > PyTypeObject ArmaCapsulePyType<MatT>::object = { \
    PyObject_HEAD_INIT(NULL)   \
    0, /*ob_size*/             \
    "ArmaCapsule", /*tp_name*/ \
    sizeof( ArmaCapsule< MatT > ), /*tp_basicsize*/ \
    0, /*tp_itemsize*/ \
    ArmaMat_dealloc< MatT >, /*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, /*tp_flags*/ \
    "Internal armadillo capsulation object", /* tp_doc */ \
    };

///////////////////////////////////////////////////////////////////////////////
// Define types (templated) to allow wrapping of boost::shared_ptr< arma::... >
// types as Python objects such that they can be used as base objects of numpy
// arrays.
// This is mainly used for return values of the type boost::shared_ptr< arma::... >
//

#if defined( ARMANPY_SHARED_PTR )

#define INIT_ARMA_BSPTR_CAPSULE( MatT )  \
    ArmaBsptrCapsulePyType< MatT >::object.tp_new = PyType_GenericNew; \
    if (PyType_Ready(&ArmaBsptrCapsulePyType< MatT >::object) < 0) return;

template< typename MatT >
struct ArmaBsptrCapsule {
    PyObject_HEAD
    boost::shared_ptr< MatT > *mat;
} ;

template< typename MatT >
static void ArmaBsptrMat_dealloc( PyObject *self )
{
    //std::cerr << "ArmaBsptrMat_dealloc( " << self << " )"<< std::endl;
    //(*(((ArmaBsptrCapsule<MatT> *)self)->mat))->print("mat");
    delete ((ArmaBsptrCapsule<MatT> *)self)->mat;
    self->ob_type->tp_free( self );
};

template<typename MatT >
class ArmaBsptrCapsulePyType {
public:
    static PyTypeObject object;
private:
    MatT _dummy;
};


template< typename MatT > PyTypeObject ArmaBsptrCapsulePyType<MatT>::object = { \
    PyObject_HEAD_INIT(NULL)   \
    0, /*ob_size*/             \
    "ArmaBsptrCapsule", /*tp_name*/ \
    sizeof( ArmaBsptrCapsule< MatT > ), /*tp_basicsize*/ \
    0, /*tp_itemsize*/ \
    ArmaBsptrMat_dealloc< MatT >, /*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, /*tp_flags*/ \
    "Internal armadillo capsulation object", /* tp_doc */ \
    };

#endif

///////////////////////////////////////////////////////////////////////////////
// Define a template NumpyType which associates the element types like double,
// float, int, etc. with the accoring numpy enums NPY_DOUBLE, NPY_FLOAT and so
// on.
//
// #include <numpy/ndarraytypes.h>
#include <numpy/arrayobject.h>

template< typename elem_type > struct NumpyType { private: elem_type _d; };

#define ASSOCIATE_NUMPY_TYPE( elem_type, npy ) \
    template<> struct NumpyType< elem_type > { static int val; }; \
    int NumpyType< elem_type >::val=npy;

ASSOCIATE_NUMPY_TYPE( double,   NPY_DOUBLE )
ASSOCIATE_NUMPY_TYPE( float,    NPY_FLOAT )
ASSOCIATE_NUMPY_TYPE( int,      NPY_INT )
ASSOCIATE_NUMPY_TYPE( unsigned, NPY_UINT )
ASSOCIATE_NUMPY_TYPE( unsigned char, NPY_UBYTE )
#if defined(ARMA_64BIT_WORD)
ASSOCIATE_NUMPY_TYPE( arma::sword, NPY_INT64 )
ASSOCIATE_NUMPY_TYPE( arma::uword, NPY_UINT64 )
#endif
ASSOCIATE_NUMPY_TYPE( std::complex< double >, NPY_COMPLEX128 )
ASSOCIATE_NUMPY_TYPE( std::complex< float >,  NPY_COMPLEX64 )

template< typename MatT > struct ArmaTypeInfo { private: MatT _d; };
#define ARMA_TYPE_INFO( MatT, nd ) \
    template<> struct ArmaTypeInfo< MatT > { static int type; static int numdim; }; \
    int ArmaTypeInfo< MatT >::type=NumpyType< MatT::elem_type >::val; \
    int ArmaTypeInfo< MatT >::numdim=nd;

ARMA_TYPE_INFO( arma::vec,          1 )
ARMA_TYPE_INFO( arma::fvec,         1 )
ARMA_TYPE_INFO( arma::ivec,         1 )
ARMA_TYPE_INFO( arma::uvec,         1 )
ARMA_TYPE_INFO( arma::uchar_vec,    1 )
#if defined(ARMA_64BIT_WORD)
ARMA_TYPE_INFO( arma::u32_vec,      1 )
ARMA_TYPE_INFO( arma::s32_vec,      1 )
#endif
ARMA_TYPE_INFO( arma::cx_vec,       1 )
ARMA_TYPE_INFO( arma::cx_fvec,      1 )

ARMA_TYPE_INFO( arma::rowvec,       1 )
ARMA_TYPE_INFO( arma::frowvec,      1 )
ARMA_TYPE_INFO( arma::irowvec,      1 )
ARMA_TYPE_INFO( arma::urowvec,      1 )
ARMA_TYPE_INFO( arma::uchar_rowvec, 1 )
#if defined(ARMA_64BIT_WORD)
ARMA_TYPE_INFO( arma::u32_rowvec,   1 )
ARMA_TYPE_INFO( arma::s32_rowvec,   1 )
#endif
ARMA_TYPE_INFO( arma::cx_rowvec,    1 )
ARMA_TYPE_INFO( arma::cx_frowvec,   1 )

ARMA_TYPE_INFO( arma::mat,       2 )
ARMA_TYPE_INFO( arma::fmat,      2 )
ARMA_TYPE_INFO( arma::imat,      2 )
ARMA_TYPE_INFO( arma::umat,      2 )
ARMA_TYPE_INFO( arma::uchar_mat, 2 )
#if defined(ARMA_64BIT_WORD)
ARMA_TYPE_INFO( arma::u32_mat,   2 )
ARMA_TYPE_INFO( arma::s32_mat,   2 )
#endif
ARMA_TYPE_INFO( arma::cx_mat,    2 )
ARMA_TYPE_INFO( arma::cx_fmat,   2 )

ARMA_TYPE_INFO( arma::cube,       3 )
ARMA_TYPE_INFO( arma::fcube,      3 )
ARMA_TYPE_INFO( arma::icube,      3 )
ARMA_TYPE_INFO( arma::ucube,      3 )
ARMA_TYPE_INFO( arma::uchar_cube, 3 )
#if defined(ARMA_64BIT_WORD)
ARMA_TYPE_INFO( arma::u32_cube,   3 )
ARMA_TYPE_INFO( arma::s32_cube,   3 )
#endif
ARMA_TYPE_INFO( arma::cx_cube,    3 )
ARMA_TYPE_INFO( arma::cx_fcube,   3 )

///////////////////////////////////////////////////////////////////////////////
// Function to modify conversion and warning behaviour.
//

static bool armanpy_allow_conversion_flag   = false;
static bool armanpy_warn_on_conversion_flag = true;

/*
void armanpy_conversion( bool allowed =false, bool warnings = true ) {
    armanpy_allow_conversion_flag   = allowed;
    armanpy_warn_on_conversion_flag = warnings;
};
*/

///////////////////////////////////////////////////////////////////////////////
// Code for typechecks, typemaps etc.
//

//#include "armanpy_1d.hpp"