29 #include "../limits.hpp" 34 #include <type_traits> 47 template<
typename T >
61 PyObject * createNumpyArrayImpl(
void *
const data,
62 std::type_index
const type,
63 bool const dataIsConst,
65 long long const *
const dims,
66 long long const *
const strides );
79 PyObject * createCupyArrayImpl(
void *
const data,
80 std::type_index
const type,
81 bool const dataIsConst,
83 long long const *
const dims,
84 long long const *
const strides );
105 template<
typename T,
typename INDEX_TYPE >
106 std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
110 INDEX_TYPE
const *
const dimsPtr,
111 INDEX_TYPE
const *
const stridesPtr )
113 std::vector< long long > dims( ndim );
114 std::vector< long long > strides( ndim );
116 for(
int i = 0; i < ndim; ++i )
118 dims[ i ] = integerConversion< long long >( dimsPtr[ i ] );
119 strides[ i ] = integerConversion< long long >( stridesPtr[ i ] );
124 return internal::createCupyArrayImpl( const_cast< void * >( static_cast< void const * >( data ) ),
125 std::type_index(
typeid( T ) ),
126 std::is_const< T >::value || !modify,
131 return internal::createNumpyArrayImpl( const_cast< void * >( static_cast< void const * >( data ) ),
132 std::type_index(
typeid( T ) ),
133 std::is_const< T >::value || !modify,
145 template<
typename T >
146 std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
150 long long strides = 1;
152 return internal::createNumpyArrayImpl( const_cast< void * >( static_cast< void const * >( &value ) ),
153 std::type_index(
typeid( T ) ),
154 std::is_const< T >::value,
167 std::tuple< PyObjectRef< PyObject >,
void const *,
long long >
168 parseNumPyArray( PyObject *
const obj, std::type_index
const expectedType );
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(T &value)
Create a NumPy 1D array of length 1 containing the scalar value.
Definition: numpyHelpers.hpp:147
std::type_index getTypeIndexFromNumPy(int const numpyType)
Return the std::type_index corresponding to the NumPy type numpyType.
Definition: numpyHelpers.cpp:276
Forward declarations of Python Objects.
The top level namespace.
Definition: Array.hpp:24
constexpr bool canExportToNumpy
True if T can be represented by a NumPy native data type.
Definition: numpyHelpers.hpp:48
PyObject * getNumPyTypeObject(std::type_index const typeIndex)
Return the NumPy type object corresponding to typeIndex.
Definition: numpyHelpers.cpp:398
bool import_array_wrapper()
Attempt to import the NumPy API if it was not already imported, return true iff successful.
Definition: numpyHelpers.cpp:221
std::tuple< PyObjectRef< PyObject >, void const *, long long > parseNumPyArray(PyObject *const obj, std::type_index const expectedType)
Attempt to parse obj into a NumPy ndarray of type expectedType.
Definition: numpyHelpers.cpp:233
std::string getNumPyTypeName(int const numpyType)
Return the name corresponding to the NumPy type numpyType.
Definition: numpyHelpers.cpp:335
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> createNumPyArray(T *const data, bool const modify, int const ndim, INDEX_TYPE const *const dimsPtr, INDEX_TYPE const *const stridesPtr)
Return a NumPy ndarray view of data.
Definition: numpyHelpers.hpp:107