@@ -198,6 +198,91 @@ struct cast<Scalar, NewScalar, EigenBase, false> {
198198 mat, NumpyMap<MatType, NewScalar>::map( \
199199 pyArray, details::check_swap(pyArray, mat)))
200200
201+ // Define specific cast for Windows and Mac
202+ #if defined _WIN32 || defined __CYGWIN__
203+ // Manage NPY_INT on Windows (NPY_INT32 is NPY_LONG).
204+ // See https://github.com/stack-of-tasks/eigenpy/pull/455
205+ #define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC ( \
206+ MatType, Scalar, pyArray, mat, CAST_MACRO) \
207+ case NPY_INT: \
208+ CAST_MACRO (MatType, int32_t , Scalar, pyArray, mat); \
209+ break ; \
210+ case NPY_UINT: \
211+ CAST_MACRO (MatType, uint32_t , Scalar, pyArray, mat); \
212+ break ;
213+ #elif defined __APPLE__
214+ // Manage NPY_LONGLONG on Mac (NPY_INT64 is NPY_LONG).
215+ // long long and long are both the same type
216+ // but NPY_LONGLONG and NPY_LONG are different dtype.
217+ // See https://github.com/stack-of-tasks/eigenpy/pull/455
218+ #define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC ( \
219+ MatType, Scalar, pyArray, mat, CAST_MACRO) \
220+ case NPY_LONGLONG: \
221+ CAST_MACRO (MatType, int64_t , Scalar, pyArray, mat); \
222+ break ; \
223+ case NPY_ULONGLONG: \
224+ CAST_MACRO (MatType, uint64_t , Scalar, pyArray, mat); \
225+ break ;
226+ #else
227+ #define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC ( \
228+ MatType, Scalar, pyArray, mat, CAST_MACRO)
229+ #endif
230+
231+ // / Define casting between Numpy matrix type to Eigen type.
232+ #define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH ( \
233+ pyArray_type_code, MatType, Scalar, pyArray, mat, CAST_MACRO) \
234+ switch (pyArray_type_code) { \
235+ case NPY_BOOL: \
236+ CAST_MACRO (MatType, bool , Scalar, pyArray, mat); \
237+ break ; \
238+ case NPY_INT8: \
239+ CAST_MACRO (MatType, int8_t , Scalar, pyArray, mat); \
240+ break ; \
241+ case NPY_INT16: \
242+ CAST_MACRO (MatType, int16_t , Scalar, pyArray, mat); \
243+ break ; \
244+ case NPY_INT32: \
245+ CAST_MACRO (MatType, int32_t , Scalar, pyArray, mat); \
246+ break ; \
247+ case NPY_INT64: \
248+ CAST_MACRO (MatType, int64_t , Scalar, pyArray, mat); \
249+ break ; \
250+ case NPY_UINT8: \
251+ CAST_MACRO (MatType, uint8_t , Scalar, pyArray, mat); \
252+ break ; \
253+ case NPY_UINT16: \
254+ CAST_MACRO (MatType, uint16_t , Scalar, pyArray, mat); \
255+ break ; \
256+ case NPY_UINT32: \
257+ CAST_MACRO (MatType, uint32_t , Scalar, pyArray, mat); \
258+ break ; \
259+ case NPY_UINT64: \
260+ CAST_MACRO (MatType, uint64_t , Scalar, pyArray, mat); \
261+ break ; \
262+ case NPY_FLOAT: \
263+ CAST_MACRO (MatType, float , Scalar, pyArray, mat); \
264+ break ; \
265+ case NPY_CFLOAT: \
266+ CAST_MACRO (MatType, std::complex <float >, Scalar, pyArray, mat); \
267+ break ; \
268+ case NPY_DOUBLE: \
269+ CAST_MACRO (MatType, double , Scalar, pyArray, mat); \
270+ break ; \
271+ case NPY_CDOUBLE: \
272+ CAST_MACRO (MatType, std::complex <double >, Scalar, pyArray, mat); \
273+ break ; \
274+ case NPY_LONGDOUBLE: \
275+ CAST_MACRO (MatType, long double , Scalar, pyArray, mat); \
276+ break ; \
277+ case NPY_CLONGDOUBLE: \
278+ CAST_MACRO (MatType, std::complex <long double >, Scalar, pyArray, mat); \
279+ break ; \
280+ EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC ( \
281+ MatType, Scalar, pyArray, mat, CAST_MACRO) \
282+ default : \
283+ throw Exception (" You asked for a conversion which is not implemented." ); \
284+ }
285+
201286template <typename EigenType>
202287struct EigenAllocator ;
203288
@@ -247,43 +332,9 @@ struct eigen_allocator_impl_matrix {
247332 pyArray, details::check_swap (pyArray, mat)); // avoid useless cast
248333 return ;
249334 }
250-
251- switch (pyArray_type_code) {
252- case NPY_INT:
253- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, int , Scalar, pyArray,
254- mat);
255- break ;
256- case NPY_LONG:
257- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, long , Scalar,
258- pyArray, mat);
259- break ;
260- case NPY_FLOAT:
261- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, float , Scalar,
262- pyArray, mat);
263- break ;
264- case NPY_CFLOAT:
265- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, std::complex <float >,
266- Scalar, pyArray, mat);
267- break ;
268- case NPY_DOUBLE:
269- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, double , Scalar,
270- pyArray, mat);
271- break ;
272- case NPY_CDOUBLE:
273- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, std::complex <double >,
274- Scalar, pyArray, mat);
275- break ;
276- case NPY_LONGDOUBLE:
277- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (MatType, long double , Scalar,
278- pyArray, mat);
279- break ;
280- case NPY_CLONGDOUBLE:
281- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX (
282- MatType, std::complex <long double >, Scalar, pyArray, mat);
283- break ;
284- default :
285- throw Exception (" You asked for a conversion which is not implemented." );
286- }
335+ EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH (
336+ pyArray_type_code, MatType, Scalar, pyArray, mat,
337+ EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX);
287338 }
288339
289340 // / \brief Copy mat into the Python array using Eigen::Map
@@ -301,43 +352,8 @@ struct eigen_allocator_impl_matrix {
301352 details::check_swap (pyArray, mat)) = mat;
302353 return ;
303354 }
304-
305- switch (pyArray_type_code) {
306- case NPY_INT:
307- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (MatType, Scalar, int , mat,
308- pyArray);
309- break ;
310- case NPY_LONG:
311- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (MatType, Scalar, long , mat,
312- pyArray);
313- break ;
314- case NPY_FLOAT:
315- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (MatType, Scalar, float , mat,
316- pyArray);
317- break ;
318- case NPY_CFLOAT:
319- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (
320- MatType, Scalar, std::complex <float >, mat, pyArray);
321- break ;
322- case NPY_DOUBLE:
323- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (MatType, Scalar, double , mat,
324- pyArray);
325- break ;
326- case NPY_CDOUBLE:
327- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (
328- MatType, Scalar, std::complex <double >, mat, pyArray);
329- break ;
330- case NPY_LONGDOUBLE:
331- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (MatType, Scalar, long double ,
332- mat, pyArray);
333- break ;
334- case NPY_CLONGDOUBLE:
335- EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY (
336- MatType, Scalar, std::complex <long double >, mat, pyArray);
337- break ;
338- default :
339- throw Exception (" You asked for a conversion which is not implemented." );
340- }
355+ throw Exception (
356+ " Scalar conversion from Eigen to Numpy is not implemented." );
341357 }
342358};
343359
@@ -394,42 +410,9 @@ struct eigen_allocator_impl_tensor {
394410 return ;
395411 }
396412
397- switch (pyArray_type_code) {
398- case NPY_INT:
399- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (TensorType, int , Scalar,
400- pyArray, tensor);
401- break ;
402- case NPY_LONG:
403- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (TensorType, long , Scalar,
404- pyArray, tensor);
405- break ;
406- case NPY_FLOAT:
407- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (TensorType, float , Scalar,
408- pyArray, tensor);
409- break ;
410- case NPY_CFLOAT:
411- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (
412- TensorType, std::complex <float >, Scalar, pyArray, tensor);
413- break ;
414- case NPY_DOUBLE:
415- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (TensorType, double , Scalar,
416- pyArray, tensor);
417- break ;
418- case NPY_CDOUBLE:
419- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (
420- TensorType, std::complex <double >, Scalar, pyArray, tensor);
421- break ;
422- case NPY_LONGDOUBLE:
423- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (TensorType, long double ,
424- Scalar, pyArray, tensor);
425- break ;
426- case NPY_CLONGDOUBLE:
427- EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR (
428- TensorType, std::complex <long double >, Scalar, pyArray, tensor);
429- break ;
430- default :
431- throw Exception (" You asked for a conversion which is not implemented." );
432- }
413+ EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH (
414+ pyArray_type_code, TensorType, Scalar, pyArray, tensor,
415+ EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR);
433416 }
434417
435418#define EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (TensorType, Scalar, \
@@ -454,42 +437,8 @@ struct eigen_allocator_impl_tensor {
454437 return ;
455438 }
456439
457- switch (pyArray_type_code) {
458- case NPY_INT:
459- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (TensorType, Scalar, int ,
460- tensor, pyArray);
461- break ;
462- case NPY_LONG:
463- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (TensorType, Scalar, long ,
464- tensor, pyArray);
465- break ;
466- case NPY_FLOAT:
467- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (TensorType, Scalar, float ,
468- tensor, pyArray);
469- break ;
470- case NPY_CFLOAT:
471- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (
472- TensorType, Scalar, std::complex <float >, tensor, pyArray);
473- break ;
474- case NPY_DOUBLE:
475- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (TensorType, Scalar, double ,
476- tensor, pyArray);
477- break ;
478- case NPY_CDOUBLE:
479- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (
480- TensorType, Scalar, std::complex <double >, tensor, pyArray);
481- break ;
482- case NPY_LONGDOUBLE:
483- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (TensorType, Scalar,
484- long double , tensor, pyArray);
485- break ;
486- case NPY_CLONGDOUBLE:
487- EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY (
488- TensorType, Scalar, std::complex <long double >, tensor, pyArray);
489- break ;
490- default :
491- throw Exception (" You asked for a conversion which is not implemented." );
492- }
440+ throw Exception (
441+ " Scalar conversion from Eigen to Numpy is not implemented." );
493442 }
494443};
495444#endif
0 commit comments