def test(self): a = numpy.arange(12, dtype = numpy.double) a.shape = 3,4 ptr = numpyext.getdataptr( a ) wp = bpext.wrap_native_ptr( ptr ) shape = mccomponentsbp.vector_uint( 0 ) for i in a.shape: shape.append( i ) a1 = mccomponentsbp.new_NdArray_dblarr_2( wp, shape ) a1.origin = a indexes = mccomponentsbp.vector_uint( 0 ) indexes.append( 2 ); indexes.append( 1 ) self.assertEqual( a1[ indexes ], 9 ) return
def ndarray( self, npyarr ): '''create boost python instance of NdArray object arguments: npyarr: numpy array. it must be a contiguous array. ''' assert npyarr.dtype == numpy.double, "only work for double array for this time" ptr = numpyext.getdataptr( npyarr ) wp = bpext.wrap_native_ptr( ptr ) shape = b.vector_uint( 0 ) for i in npyarr.shape: shape.append( i ) factory = 'new_NdArray_dblarr_%d' % len(shape) a1 = getattr(b,factory)( wp, shape ) a1.origin = npyarr # keep a reference to avoid seg fault return a1