From 9a886dc3651a36fb1a8c6052b6136c08153b74f5 Mon Sep 17 00:00:00 2001 From: Maki4748 <68695780+maki4748@users.noreply.github.com> Date: Sat, 30 Nov 2024 00:35:38 +0100 Subject: [PATCH 1/7] Changed up unspecific int types to the specific bit implementation --- tests/eigency_tests/eigency_tests.pyx | 89 +++++++++--------- tests/eigency_tests/eigency_tests_cpp.cpp | 46 ++++------ tests/eigency_tests/eigency_tests_cpp.h | 23 +++-- tests/run_tests.py | 104 ++++++++++------------ 4 files changed, 115 insertions(+), 147 deletions(-) diff --git a/tests/eigency_tests/eigency_tests.pyx b/tests/eigency_tests/eigency_tests.pyx index f8ce32d..d596228 100644 --- a/tests/eigency_tests/eigency_tests.pyx +++ b/tests/eigency_tests/eigency_tests.pyx @@ -1,6 +1,7 @@ # distutils: language = c++ # distutils: sources = eigency_tests/eigency_tests_cpp.cpp from eigency.core cimport * +from libc.stdint cimport int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t # cimport eigency.conversions # from eigency_tests.eigency cimport * @@ -43,20 +44,18 @@ cdef extern from "eigency_tests/eigency_tests_cpp.h": cdef PlainObjectBase _function_filter3 "function_filter3" (FlattenedMapWithStride[Array, double, Dynamic, Dynamic, ColMajor, Unaligned, _1, Dynamic] &) - cdef PlainObjectBase _function_type_double "function_type_double" (Map[ArrayXXd] &) cdef PlainObjectBase _function_type_float "function_type_float" (Map[ArrayXXf] &) - cdef PlainObjectBase _function_type_long "function_type_long" (FlattenedMap[Array, long, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_long_long "function_type_long_long" (FlattenedMap[Array, long long, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_ulong "function_type_ulong" (FlattenedMap[Array, unsigned long, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_ulong_long "function_type_ulong_long" (FlattenedMap[Array, unsigned long long, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_int "function_type_int" (Map[ArrayXXi] &) - cdef PlainObjectBase _function_type_uint "function_type_uint" (FlattenedMap[Array, unsigned int, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_short "function_type_short" (FlattenedMap[Array, short, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_ushort "function_type_ushort" (FlattenedMap[Array, ushort, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_signed_char "function_type_signed_char" (FlattenedMap[Array, signed char, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_unsigned_char "function_type_unsigned_char" (FlattenedMap[Array, unsigned char, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_complex_double "function_type_complex_double" (Map[ArrayXXcd] &) + cdef PlainObjectBase _function_type_double "function_type_double" (Map[ArrayXXd] &) + cdef PlainObjectBase _function_type_int8 "function_type_int8" (FlattenedMap[Array, int8_t, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_uint8 "function_type_uint8" (FlattenedMap[Array, uint8_t, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_int16 "function_type_int16" (FlattenedMap[Array, int16_t, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_uint16 "function_type_uint16" (FlattenedMap[Array, uint16_t, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_int32 "function_type_int32" (Map[ArrayXXi] &) + cdef PlainObjectBase _function_type_uint32 "function_type_uint32" (FlattenedMap[Array, uint32_t, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_int64 "function_type_int64" (FlattenedMap[Array, int64_t, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_uint64 "function_type_uint64" (FlattenedMap[Array, uint64_t, Dynamic, Dynamic] &) cdef PlainObjectBase _function_type_complex_float "function_type_complex_float" (Map[ArrayXXcf] &) + cdef PlainObjectBase _function_type_complex_double "function_type_complex_double" (Map[ArrayXXcd] &) cdef PlainObjectBase _function_single_col_matrix "function_single_col_matrix" (Map[ArrayXXd] &) @@ -141,62 +140,54 @@ def function_filter2(np.ndarray[np.float64_t, ndim=2] array): def function_filter3(np.ndarray[np.float64_t, ndim=2] array): return ndarray(_function_filter3(FlattenedMapWithStride[Array, double, Dynamic, Dynamic, ColMajor, Unaligned, _1, Dynamic](array))) -# Functions with different matrix types: float64 -def function_type_float64(np.ndarray[np.float64_t, ndim=2] array): - return ndarray(_function_type_double(Map[ArrayXXd](array))) - # Functions with different matrix types: float32 def function_type_float32(np.ndarray[np.float32_t, ndim=2] array): return ndarray(_function_type_float(Map[ArrayXXf](array))) -# Functions with different matrix types: long -def function_type_long(np.ndarray[long, ndim=2] array): - return ndarray(_function_type_long(FlattenedMap[Array, long, Dynamic, Dynamic](array))) - -# Functions with different matrix types: long long -def function_type_long_long(np.ndarray[long long, ndim=2] array): - return ndarray(_function_type_long_long(FlattenedMap[Array, longlong, Dynamic, Dynamic](array))) +# Functions with different matrix types: float64 +def function_type_float64(np.ndarray[np.float64_t, ndim=2] array): + return ndarray(_function_type_double(Map[ArrayXXd](array))) -# Functions with different matrix types: ulong -def function_type_ulong(np.ndarray[unsigned long, ndim=2] array): - return ndarray(_function_type_ulong(FlattenedMap[Array, ulong, Dynamic, Dynamic](array))) +# Functions with different matrix types: signed char +def function_type_int8(np.ndarray[np.int8_t, ndim=2] array): + return ndarray(_function_type_int8(FlattenedMap[Array, int8_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: ulong long -def function_type_ulong_long(np.ndarray[unsigned long long, ndim=2] array): - return ndarray(_function_type_ulong_long(FlattenedMap[Array, ulonglong, Dynamic, Dynamic](array))) +# Functions with different matrix types: unsigned char +def function_type_uint8(np.ndarray[np.uint8_t, ndim=2] array): + return ndarray(_function_type_uint8(FlattenedMap[Array, uint8_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: int -def function_type_intc(np.ndarray[np.int32_t, ndim=2] array): - return ndarray(_function_type_int(Map[ArrayXXi](array))) +# Functions with different matrix types: signed char +def function_type_int16(np.ndarray[np.int16_t, ndim=2] array): + return ndarray(_function_type_int16(FlattenedMap[Array, int16_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: uint -def function_type_uintc(np.ndarray[np.uint32_t, ndim=2] array): - return ndarray(_function_type_uint(FlattenedMap[Array, uint, Dynamic, Dynamic](array))) +# Functions with different matrix types: unsigned char +def function_type_uint16(np.ndarray[np.uint16_t, ndim=2] array): + return ndarray(_function_type_uint16(FlattenedMap[Array, uint16_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: short -def function_type_short(np.ndarray[np.int16_t, ndim=2] array): - return ndarray(_function_type_short(FlattenedMap[Array, short, Dynamic, Dynamic](array))) +# Functions with different matrix types: signed char +def function_type_int32(np.ndarray[np.int32_t, ndim=2] array): + return ndarray(_function_type_int32(Map[ArrayXXi](array))) -# Functions with different matrix types: ushort -def function_type_ushort(np.ndarray[np.uint16_t, ndim=2] array): - return ndarray(_function_type_ushort(FlattenedMap[Array, ushort, Dynamic, Dynamic](array))) +# Functions with different matrix types: unsigned char +def function_type_uint32(np.ndarray[np.uint32_t, ndim=2] array): + return ndarray(_function_type_uint32(FlattenedMap[Array, uint32_t, Dynamic, Dynamic](array))) # Functions with different matrix types: signed char -def function_type_int8(np.ndarray[np.int8_t, ndim=2] array): - return ndarray(_function_type_signed_char(FlattenedMap[Array, schar, Dynamic, Dynamic](array))) +def function_type_int64(np.ndarray[np.int64_t, ndim=2] array): + return ndarray(_function_type_int64(FlattenedMap[Array, int64_t, Dynamic, Dynamic](array))) # Functions with different matrix types: unsigned char -def function_type_uint8(np.ndarray[np.uint8_t, ndim=2] array): - return ndarray(_function_type_unsigned_char(FlattenedMap[Array, uchar, Dynamic, Dynamic](array))) - -# Functions with different matrix types: complex128 -def function_type_complex128(np.ndarray[np.complex128_t, ndim=2] array): - return ndarray(_function_type_complex_double(Map[ArrayXXcd](array))) +def function_type_uint64(np.ndarray[np.uint64_t, ndim=2] array): + return ndarray(_function_type_uint64(FlattenedMap[Array, uint64_t, Dynamic, Dynamic](array))) # Functions with different matrix types: complex64 def function_type_complex64(np.ndarray[np.complex64_t, ndim=2] array): return ndarray(_function_type_complex_float(Map[ArrayXXcf](array))) +# Functions with different matrix types: complex128 +def function_type_complex128(np.ndarray[np.complex128_t, ndim=2] array): + return ndarray(_function_type_complex_double(Map[ArrayXXcd](array))) + # Functions testing a matrix with only one column def function_single_col_matrix(np.ndarray[np.float64_t, ndim=2] array): return ndarray(_function_single_col_matrix(Map[ArrayXXd](array))) diff --git a/tests/eigency_tests/eigency_tests_cpp.cpp b/tests/eigency_tests/eigency_tests_cpp.cpp index ad5a2b2..2178222 100644 --- a/tests/eigency_tests/eigency_tests_cpp.cpp +++ b/tests/eigency_tests/eigency_tests_cpp.cpp @@ -73,64 +73,54 @@ Eigen::Map > return mat; } -Eigen::ArrayXXd function_type_double(Eigen::Map &mat) { - Eigen::ArrayXXd output = mat; - return output; -} - Eigen::ArrayXXf function_type_float(Eigen::Map &mat) { Eigen::ArrayXXf output = mat; return output; } -Eigen::Array function_type_long(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::ArrayXXd function_type_double(Eigen::Map &mat) { + Eigen::ArrayXXd output = mat; return output; } -Eigen::Array function_type_long_long(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::Array function_type_int8(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } -Eigen::Array function_type_ulong(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::Array function_type_uint8(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } -Eigen::Array function_type_ulong_long(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::Array function_type_int16(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } - -Eigen::ArrayXXi function_type_int(Eigen::Map &mat) { - Eigen::ArrayXXi output = mat; +Eigen::Array function_type_uint16(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } -Eigen::Array function_type_uint(Eigen::Map > &mat) { - Eigen::Array output = mat; - return output; -} -Eigen::Array function_type_short(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::ArrayXXi function_type_int32(Eigen::Map &mat) { + Eigen::ArrayXXi output = mat; return output; } -Eigen::Array function_type_ushort(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::Array function_type_uint32(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } -Eigen::Array function_type_signed_char(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::Array function_type_int64(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } -Eigen::Array function_type_unsigned_char(Eigen::Map > &mat) { - Eigen::Array output = mat; +Eigen::Array function_type_uint64(Eigen::Map > &mat) { + Eigen::Array output = mat; return output; } diff --git a/tests/eigency_tests/eigency_tests_cpp.h b/tests/eigency_tests/eigency_tests_cpp.h index ea6a5fd..d87dc4b 100644 --- a/tests/eigency_tests/eigency_tests_cpp.h +++ b/tests/eigency_tests/eigency_tests_cpp.h @@ -3,6 +3,7 @@ #include "Eigen/Core" #include "eigency.h" +#include long function_w_vec_arg(Eigen::Map &vec); @@ -38,20 +39,18 @@ RowMajorArrayMap &function_filter2(RowMajorArrayMap &); typedef Eigen::Map > CustomStrideMap; CustomStrideMap &function_filter3(CustomStrideMap &); -Eigen::ArrayXXd function_type_double(Eigen::Map &array); Eigen::ArrayXXf function_type_float(Eigen::Map &array); -Eigen::Array function_type_long(Eigen::Map > &mat); -Eigen::Array function_type_long_long(Eigen::Map > &mat); -Eigen::Array function_type_ulong(Eigen::Map > &mat); -Eigen::Array function_type_ulong_long(Eigen::Map > &mat); -Eigen::ArrayXXi function_type_int(Eigen::Map &array); -Eigen::Array function_type_uint(Eigen::Map > &array); -Eigen::Array function_type_short(Eigen::Map > &mat); -Eigen::Array function_type_ushort(Eigen::Map > &mat); -Eigen::Array function_type_signed_char(Eigen::Map > &mat); -Eigen::Array function_type_unsigned_char(Eigen::Map > &mat); -Eigen::ArrayXXcd function_type_complex_double(Eigen::Map &array); +Eigen::ArrayXXd function_type_double(Eigen::Map &array); +Eigen::Array function_type_int8(Eigen::Map > &mat); +Eigen::Array function_type_uint8(Eigen::Map > &mat); +Eigen::Array function_type_int16(Eigen::Map > &mat); +Eigen::Array function_type_uint16(Eigen::Map > &mat); +Eigen::ArrayXXi function_type_int32(Eigen::Map &array); +Eigen::Array function_type_uint32(Eigen::Map > &array); +Eigen::Array function_type_int64(Eigen::Map > &mat); +Eigen::Array function_type_uint64(Eigen::Map > &mat); Eigen::ArrayXXcf function_type_complex_float(Eigen::Map &array); +Eigen::ArrayXXcd function_type_complex_double(Eigen::Map &array); Eigen::Map function_single_col_matrix(Eigen::Map &array); diff --git a/tests/run_tests.py b/tests/run_tests.py index 5b69d10..20d2ceb 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -1,5 +1,7 @@ import unittest +import cython + import eigency_tests import numpy as np from numpy.testing import assert_array_equal @@ -147,83 +149,69 @@ def test_mat_ref_retval_array_row_major(self): y = my_object.get_array_copy() assert_array_equal(x, y) - def test_function_type_float64(self): - # C++ double - mat_in = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]], order="F", dtype=np.float64) - mat_out = eigency_tests.function_type_float64(mat_in) - assert_array_equal(mat_in, mat_out) - def test_function_type_float32(self): # C++ float mat_in = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]], order="F", dtype=np.float32) mat_out = eigency_tests.function_type_float32(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_int(self): - # C++ long - Note that this is the standard Python integer - # with numpy 1 int is 32 in numpy 2 int is 64, so we need to be more specific - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F") - if mat_in.dtype == np.dtype("long"): - mat_out = eigency_tests.function_type_long(mat_in) - else: - mat_out = eigency_tests.function_type_long_long(mat_in) + def test_function_type_float64(self): + # C++ double + mat_in = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]], order="F", dtype=np.float64) + mat_out = eigency_tests.function_type_float64(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_long(self): - # C++ long - Note that this is the standard Python integer - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=int) - if mat_in.dtype == np.dtype("long"): - mat_out = eigency_tests.function_type_long(mat_in) - else: - mat_out = eigency_tests.function_type_long_long(mat_in) + def test_function_type_int8(self): + # C++ char + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int8) + mat_out = eigency_tests.function_type_int8(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_ulong(self): - # C++ ulong - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint) - if mat_in.dtype == np.dtype("ulong"): - mat_out = eigency_tests.function_type_ulong(mat_in) - else: - mat_out = eigency_tests.function_type_ulong_long(mat_in) + def test_function_type_uint8(self): + # C++ unsigned char + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint8) + mat_out = eigency_tests.function_type_uint8(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_intc(self): - # C++ int - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.intc) - mat_out = eigency_tests.function_type_intc(mat_in) + def test_function_type_int16(self): + # C++ long - Note that this is the standard Python integer + # with numpy 1 int is 32 in numpy 2 int is 64, so we need to be more specific + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int16) + mat_out = eigency_tests.function_type_int16(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_uintc(self): - # C++ uint - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uintc) - mat_out = eigency_tests.function_type_uintc(mat_in) + def test_function_type_uint16(self): + # C++ long - Note that this is the standard Python integer + # with numpy 1 int is 32 in numpy 2 int is 64, so we need to be more specific + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint16) + mat_out = eigency_tests.function_type_uint16(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_short(self): - # C++ short - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.short) - mat_out = eigency_tests.function_type_short(mat_in) + def test_function_type_int32(self): + # C++ long - Note that this is the standard Python integer + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.long) + mat_out = eigency_tests.function_type_int32(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_unsigned_short(self): - # C++ ushort - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.ushort) - mat_out = eigency_tests.function_type_ushort(mat_in) + def test_function_type_uint32(self): + # C++ ulong + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.ulong) + mat_out = eigency_tests.function_type_uint32(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_signed_char(self): - # C++ char - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int8) - mat_out = eigency_tests.function_type_int8(mat_in) + def test_function_type_int64(self): + # C++ int + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int64) + mat_out = eigency_tests.function_type_int64(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_unsigned_char(self): - # C++ unsigned char - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint8) - mat_out = eigency_tests.function_type_uint8(mat_in) + def test_function_type_uint64(self): + # C++ uint + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint64) + mat_out = eigency_tests.function_type_uint64(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_complex128(self): + def test_function_type_complex64(self): # C++ char mat_in = np.array( [ @@ -231,12 +219,12 @@ def test_function_type_complex128(self): [5.0 + 5.0j, 6.0 + 6.0j, 7.0 + 7.0j, 8.0 + 8.0j], ], order="F", - dtype=np.complex128, + dtype=np.complex64, ) - mat_out = eigency_tests.function_type_complex128(mat_in) + mat_out = eigency_tests.function_type_complex64(mat_in) assert_array_equal(mat_in, mat_out) - def test_function_type_complex64(self): + def test_function_type_complex128(self): # C++ char mat_in = np.array( [ @@ -244,9 +232,9 @@ def test_function_type_complex64(self): [5.0 + 5.0j, 6.0 + 6.0j, 7.0 + 7.0j, 8.0 + 8.0j], ], order="F", - dtype=np.complex64, + dtype=np.complex128, ) - mat_out = eigency_tests.function_type_complex64(mat_in) + mat_out = eigency_tests.function_type_complex128(mat_in) assert_array_equal(mat_in, mat_out) def test_function_single_col_matrix(self): From 702e26d093662049eb60bcb632e4a0cf35b3da3d Mon Sep 17 00:00:00 2001 From: Lina Krings <68695780+maki4748@users.noreply.github.com> Date: Sat, 30 Nov 2024 02:46:27 +0100 Subject: [PATCH 2/7] Made the handling of types by eigency more implicit through cython and reduced overhead in conversions.pyx and conversions.pxd --- eigency/conversions.pxd | 119 ++---- eigency/conversions.pyx | 578 +------------------------- eigency/core.pxd | 19 +- eigency/eigency_cpp.h | 78 ++-- tests/eigency_tests/eigency_tests.pyx | 3 +- tests/run_tests.py | 4 +- 6 files changed, 95 insertions(+), 706 deletions(-) diff --git a/eigency/conversions.pxd b/eigency/conversions.pxd index 213c488..f98a495 100644 --- a/eigency/conversions.pxd +++ b/eigency/conversions.pxd @@ -1,99 +1,30 @@ cimport numpy as np -# Array with limit 2D -cdef api np.ndarray[long double, ndim=2] ndarray_long_double() -cdef api np.ndarray[long double, ndim=2] ndarray_long_double_C(long double *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long double, ndim=2] ndarray_long_double_F(long double *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long double, ndim=2] ndarray_copy_long_double_C(const long double *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long double, ndim=2] ndarray_copy_long_double_F(const long double *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[double, ndim=2] ndarray_double() -cdef api np.ndarray[double, ndim=2] ndarray_double_C(double *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[double, ndim=2] ndarray_double_F(double *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[double, ndim=2] ndarray_copy_double_C(const double *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[double, ndim=2] ndarray_copy_double_F(const double *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[float, ndim=2] ndarray_float() -cdef api np.ndarray[float, ndim=2] ndarray_float_C(float *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[float, ndim=2] ndarray_float_F(float *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[float, ndim=2] ndarray_copy_float_C(const float *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[float, ndim=2] ndarray_copy_float_F(const float *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[long long, ndim=2] ndarray_long_long() -cdef api np.ndarray[long long, ndim=2] ndarray_long_long_C(long long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long long, ndim=2] ndarray_long_long_F(long long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long long, ndim=2] ndarray_copy_long_long_C(const long long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long long, ndim=2] ndarray_copy_long_long_F(const long long *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[long, ndim=2] ndarray_long() -cdef api np.ndarray[long, ndim=2] ndarray_long_C(long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long, ndim=2] ndarray_long_F(long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long, ndim=2] ndarray_copy_long_C(const long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long, ndim=2] ndarray_copy_long_F(const long *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[unsigned long long, ndim=2] ndarray_ulong_long() -cdef api np.ndarray[unsigned long long, ndim=2] ndarray_ulong_long_C(unsigned long long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned long long, ndim=2] ndarray_ulong_long_F(unsigned long long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned long long, ndim=2] ndarray_copy_ulong_long_C(const unsigned long long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned long long, ndim=2] ndarray_copy_ulong_long_F(const unsigned long long *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[unsigned long, ndim=2] ndarray_ulong() -cdef api np.ndarray[unsigned long, ndim=2] ndarray_ulong_C(unsigned long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned long, ndim=2] ndarray_ulong_F(unsigned long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned long, ndim=2] ndarray_copy_ulong_C(const unsigned long *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned long, ndim=2] ndarray_copy_ulong_F(const unsigned long *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[int, ndim=2] ndarray_int() -cdef api np.ndarray[int, ndim=2] ndarray_int_C(int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[int, ndim=2] ndarray_int_F(int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[int, ndim=2] ndarray_copy_int_C(const int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[int, ndim=2] ndarray_copy_int_F(const int *data, long rows, long cols, long outer_stride, long inner_stride) +# If this is changed, array_type_t in core.pxd needs to be changed too +# Also, the handling of the types needs to be changed in eigency_cpp.h for NDAC and NDAV +ctypedef fused array_type_t: + signed char + unsigned char + short + unsigned short + int + unsigned int + long + unsigned long + long long + unsigned long long + float + double + long double + float complex + double complex + long double complex -cdef api np.ndarray[unsigned int, ndim=2] ndarray_uint() -cdef api np.ndarray[unsigned int, ndim=2] ndarray_uint_C(unsigned int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned int, ndim=2] ndarray_uint_F(unsigned int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned int, ndim=2] ndarray_copy_uint_C(const unsigned int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned int, ndim=2] ndarray_copy_uint_F(const unsigned int *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[short, ndim=2] ndarray_short() -cdef api np.ndarray[short, ndim=2] ndarray_short_C(short *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[short, ndim=2] ndarray_short_F(short *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[short, ndim=2] ndarray_copy_short_C(const short *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[short, ndim=2] ndarray_copy_short_F(const short *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[unsigned short, ndim=2] ndarray_ushort() -cdef api np.ndarray[unsigned short, ndim=2] ndarray_ushort_C(unsigned short *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned short, ndim=2] ndarray_ushort_F(unsigned short *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned short, ndim=2] ndarray_copy_ushort_C(const unsigned short *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned short, ndim=2] ndarray_copy_ushort_F(const unsigned short *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[signed char, ndim=2] ndarray_schar() -cdef api np.ndarray[signed char, ndim=2] ndarray_schar_C(signed char *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[signed char, ndim=2] ndarray_schar_F(signed char *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[signed char, ndim=2] ndarray_copy_schar_C(const signed char *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[signed char, ndim=2] ndarray_copy_schar_F(const signed char *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[unsigned char, ndim=2] ndarray_uchar() -cdef api np.ndarray[unsigned char, ndim=2] ndarray_uchar_C(unsigned char *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned char, ndim=2] ndarray_uchar_F(unsigned char *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned char, ndim=2] ndarray_copy_uchar_C(const unsigned char *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[unsigned char, ndim=2] ndarray_copy_uchar_F(const unsigned char *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[long double complex, ndim=2] ndarray_complex_long_double() -cdef api np.ndarray[long double complex, ndim=2] ndarray_complex_long_double_C(long double complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long double complex, ndim=2] ndarray_complex_long_double_F(long double complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long double complex, ndim=2] ndarray_copy_complex_long_double_C(const long double complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[long double complex, ndim=2] ndarray_copy_complex_long_double_F(const long double complex *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[double complex, ndim=2] ndarray_complex_double() -cdef api np.ndarray[double complex, ndim=2] ndarray_complex_double_C(double complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[double complex, ndim=2] ndarray_complex_double_F(double complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[double complex, ndim=2] ndarray_copy_complex_double_C(const double complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[double complex, ndim=2] ndarray_copy_complex_double_F(const double complex *data, long rows, long cols, long outer_stride, long inner_stride) - -cdef api np.ndarray[float complex, ndim=2] ndarray_complex_float() -cdef api np.ndarray[float complex, ndim=2] ndarray_complex_float_C(float complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[float complex, ndim=2] ndarray_complex_float_F(float complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[float complex, ndim=2] ndarray_copy_complex_float_C(const float complex *data, long rows, long cols, long outer_stride, long inner_stride) -cdef api np.ndarray[float complex, ndim=2] ndarray_copy_complex_float_F(const float complex *data, long rows, long cols, long outer_stride, long inner_stride) +# Array with limit 2D +cdef api np.ndarray[array_type_t, ndim=2] ndarray(const array_type_t *data) +cdef api np.ndarray[array_type_t, ndim=2] ndarray_C(array_type_t *data, long rows, long cols, long outer_stride, long inner_stride) +cdef api np.ndarray[array_type_t, ndim=2] ndarray_F(array_type_t *data, long rows, long cols, long outer_stride, long inner_stride) +cdef api np.ndarray[array_type_t, ndim=2] ndarray_copy_C(const array_type_t *data, long rows, long cols, long outer_stride, long inner_stride) +cdef api np.ndarray[array_type_t, ndim=2] ndarray_copy_F(const array_type_t *data, long rows, long cols, long outer_stride, long inner_stride) diff --git a/eigency/conversions.pyx b/eigency/conversions.pyx index 8435469..0b766b0 100644 --- a/eigency/conversions.pyx +++ b/eigency/conversions.pyx @@ -1,580 +1,30 @@ cimport cython cimport numpy as np + import numpy as np from numpy.lib.stride_tricks import as_strided -# Array with limit 2D -# -# double -# - -@cython.boundscheck(False) -cdef np.ndarray[double, ndim=2] ndarray_double(): - return np.empty((0,0), dtype='double') - -@cython.boundscheck(False) -cdef np.ndarray[double, ndim=2] ndarray_double_C(double *data, long rows, long cols, long row_stride, long col_stride): - cdef double[:,:] mem_view = data - dtype = 'double' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[double, ndim=2] ndarray_double_F(double *data, long rows, long cols, long row_stride, long col_stride): - cdef double[::1,:] mem_view = data - dtype = 'double' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[double, ndim=2] ndarray_copy_double_C(const double *data, long rows, long cols, long row_stride, long col_stride): - cdef double[:,:] mem_view = data - dtype = 'double' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[double, ndim=2] ndarray_copy_double_F(const double *data, long rows, long cols, long row_stride, long col_stride): - cdef double[::1,:] mem_view = data - dtype = 'double' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# long double -# - -@cython.boundscheck(False) -cdef np.ndarray[long double, ndim=2] ndarray_long_double(): - return np.empty((0,0), dtype='longdouble') - -@cython.boundscheck(False) -cdef np.ndarray[long double, ndim=2] ndarray_long_double_C(long double *data, long rows, long cols, long row_stride, long col_stride): - cdef long double[:,:] mem_view = data - dtype = 'longdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[long double, ndim=2] ndarray_long_double_F(long double *data, long rows, long cols, long row_stride, long col_stride): - cdef long double[::1,:] mem_view = data - dtype = 'longdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[long double, ndim=2] ndarray_copy_long_double_C(const long double *data, long rows, long cols, long row_stride, long col_stride): - cdef long double[:,:] mem_view = data - dtype = 'longdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[long double, ndim=2] ndarray_copy_long_double_F(const long double *data, long rows, long cols, long row_stride, long col_stride): - cdef long double[::1,:] mem_view = data - dtype = 'longdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# float -# - -@cython.boundscheck(False) -cdef np.ndarray[float, ndim=2] ndarray_float(): - return np.empty((0,0), dtype='float') - -@cython.boundscheck(False) -cdef np.ndarray[float, ndim=2] ndarray_float_C(float *data, long rows, long cols, long row_stride, long col_stride): - cdef float[:,:] mem_view = data - dtype = 'float' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[float, ndim=2] ndarray_float_F(float *data, long rows, long cols, long row_stride, long col_stride): - cdef float[::1,:] mem_view = data - dtype = 'float' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[float, ndim=2] ndarray_copy_float_C(const float *data, long rows, long cols, long row_stride, long col_stride): - cdef float[:,:] mem_view = data - dtype = 'float' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[float, ndim=2] ndarray_copy_float_F(const float *data, long rows, long cols, long row_stride, long col_stride): - cdef float[::1,:] mem_view = data - dtype = 'float' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) -# -# long long -# - -@cython.boundscheck(False) -cdef np.ndarray[ longlong, ndim=2] ndarray_long_long(): - return np.empty((0,0), dtype='longlong') - -@cython.boundscheck(False) -cdef np.ndarray[longlong, ndim=2] ndarray_long_long_C(long long *data, long rows, long cols, long row_stride, long col_stride): - cdef long long[:,:] mem_view = data - dtype = 'longlong' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[longlong, ndim=2] ndarray_long_long_F(long long *data, long rows, long cols, long row_stride, long col_stride): - cdef long long[::1,:] mem_view = data - dtype = 'longlong' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[longlong, ndim=2] ndarray_copy_long_long_C(const long long *data, long rows, long cols, long row_stride, long col_stride): - cdef long long[:,:] mem_view = data - dtype = 'longlong' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[longlong, ndim=2] ndarray_copy_long_long_F(const long long *data, long rows, long cols, long row_stride, long col_stride): - cdef long long[::1,:] mem_view = data - dtype = 'longlong' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# long -# - -@cython.boundscheck(False) -cdef np.ndarray[long, ndim=2] ndarray_long(): - return np.empty((0,0), dtype='long') - -@cython.boundscheck(False) -cdef np.ndarray[long, ndim=2] ndarray_long_C(long *data, long rows, long cols, long row_stride, long col_stride): - cdef long[:,:] mem_view = data - dtype = 'long' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[long, ndim=2] ndarray_long_F(long *data, long rows, long cols, long row_stride, long col_stride): - cdef long[::1,:] mem_view = data - dtype = 'long' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[long, ndim=2] ndarray_copy_long_C(const long *data, long rows, long cols, long row_stride, long col_stride): - cdef long[:,:] mem_view = data - dtype = 'long' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[long, ndim=2] ndarray_copy_long_F(const long *data, long rows, long cols, long row_stride, long col_stride): - cdef long[::1,:] mem_view = data - dtype = 'long' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# unsigned long long -# -@cython.boundscheck(False) -cdef np.ndarray[unsigned long long, ndim=2] ndarray_ulong_long(): - return np.empty((0,0), dtype='ulong') - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long long, ndim=2] ndarray_ulong_long_C(unsigned long long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long long[:,:] mem_view = data - dtype = 'ulonglong' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long long, ndim=2] ndarray_ulong_long_F(unsigned long long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long long[::1,:] mem_view = data - dtype = 'ulonglong' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long long, ndim=2] ndarray_copy_ulong_long_C(const unsigned long long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long long[:,:] mem_view = data - dtype = 'ulonglong' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long long, ndim=2] ndarray_copy_ulong_long_F(const unsigned long long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long long[::1,:] mem_view = data - dtype = 'ulonglong' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# unsigned long -# - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long, ndim=2] ndarray_ulong(): - return np.empty((0,0), dtype='ulong') - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long, ndim=2] ndarray_ulong_C(unsigned long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long[:,:] mem_view = data - dtype = 'ulong' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long, ndim=2] ndarray_ulong_F(unsigned long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long[::1,:] mem_view = data - dtype = 'ulong' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long, ndim=2] ndarray_copy_ulong_C(const unsigned long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long[:,:] mem_view = data - dtype = 'ulong' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned long, ndim=2] ndarray_copy_ulong_F(const unsigned long *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned long[::1,:] mem_view = data - dtype = 'ulong' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# int -# - -@cython.boundscheck(False) -cdef np.ndarray[int, ndim=2] ndarray_int(): - return np.empty((0,0), dtype='int') - -@cython.boundscheck(False) -cdef np.ndarray[int, ndim=2] ndarray_int_C(int *data, long rows, long cols, long row_stride, long col_stride): - cdef int[:,:] mem_view = data - dtype = 'int' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[int, ndim=2] ndarray_int_F(int *data, long rows, long cols, long row_stride, long col_stride): - cdef int[::1,:] mem_view = data - dtype = 'int' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[int, ndim=2] ndarray_copy_int_C(const int *data, long rows, long cols, long row_stride, long col_stride): - cdef int[:,:] mem_view = data - dtype = 'int' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[int, ndim=2] ndarray_copy_int_F(const int *data, long rows, long cols, long row_stride, long col_stride): - cdef int[::1,:] mem_view = data - dtype = 'int' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# unsigned int -# - -@cython.boundscheck(False) -cdef np.ndarray[unsigned int, ndim=2] ndarray_uint(): - return np.empty((0,0), dtype='uint') - -@cython.boundscheck(False) -cdef np.ndarray[unsigned int, ndim=2] ndarray_uint_C(unsigned int *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned int[:,:] mem_view = data - dtype = 'uint' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned int, ndim=2] ndarray_uint_F(unsigned int *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned int[::1,:] mem_view = data - dtype = 'uint' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned int, ndim=2] ndarray_copy_uint_C(const unsigned int *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned int[:,:] mem_view = data - dtype = 'uint' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned int, ndim=2] ndarray_copy_uint_F(const unsigned int *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned int[::1,:] mem_view = data - dtype = 'uint' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# short -# - -@cython.boundscheck(False) -cdef np.ndarray[short, ndim=2] ndarray_short(): - return np.empty((0,0), dtype='short') - -@cython.boundscheck(False) -cdef np.ndarray[short, ndim=2] ndarray_short_C(short *data, long rows, long cols, long row_stride, long col_stride): - cdef short[:,:] mem_view = data - dtype = 'short' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[short, ndim=2] ndarray_short_F(short *data, long rows, long cols, long row_stride, long col_stride): - cdef short[::1,:] mem_view = data - dtype = 'short' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[short, ndim=2] ndarray_copy_short_C(const short *data, long rows, long cols, long row_stride, long col_stride): - cdef short[:,:] mem_view = data - dtype = 'short' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[short, ndim=2] ndarray_copy_short_F(const short *data, long rows, long cols, long row_stride, long col_stride): - cdef short[::1,:] mem_view = data - dtype = 'short' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# unsigned short -# - -@cython.boundscheck(False) -cdef np.ndarray[unsigned short, ndim=2] ndarray_ushort(): - return np.empty((0,0), dtype='ushort') - -@cython.boundscheck(False) -cdef np.ndarray[unsigned short, ndim=2] ndarray_ushort_C(unsigned short *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned short[:,:] mem_view = data - dtype = 'ushort' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned short, ndim=2] ndarray_ushort_F(unsigned short *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned short[::1,:] mem_view = data - dtype = 'ushort' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned short, ndim=2] ndarray_copy_ushort_C(const unsigned short *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned short[:,:] mem_view = data - dtype = 'ushort' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned short, ndim=2] ndarray_copy_ushort_F(const unsigned short *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned short[::1,:] mem_view = data - dtype = 'ushort' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# signed char -# - -@cython.boundscheck(False) -cdef np.ndarray[signed char, ndim=2] ndarray_schar(): - return np.empty((0,0), dtype='int8') - -@cython.boundscheck(False) -cdef np.ndarray[signed char, ndim=2] ndarray_schar_C(signed char *data, long rows, long cols, long row_stride, long col_stride): - cdef signed char[:,:] mem_view = data - dtype = 'int8' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[signed char, ndim=2] ndarray_schar_F(signed char *data, long rows, long cols, long row_stride, long col_stride): - cdef signed char[::1,:] mem_view = data - dtype = 'int8' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[signed char, ndim=2] ndarray_copy_schar_C(const signed char *data, long rows, long cols, long row_stride, long col_stride): - cdef signed char[:,:] mem_view = data - dtype = 'int8' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[signed char, ndim=2] ndarray_copy_schar_F(const signed char *data, long rows, long cols, long row_stride, long col_stride): - cdef signed char[::1,:] mem_view = data - dtype = 'int8' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# unsigned char -# - -@cython.boundscheck(False) -cdef np.ndarray[unsigned char, ndim=2] ndarray_uchar(): - return np.empty((0,0), dtype='uint8') - -@cython.boundscheck(False) -cdef np.ndarray[unsigned char, ndim=2] ndarray_uchar_C(unsigned char *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned char[:,:] mem_view = data - dtype = 'uint8' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned char, ndim=2] ndarray_uchar_F(unsigned char *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned char[::1,:] mem_view = data - dtype = 'uint8' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned char, ndim=2] ndarray_copy_uchar_C(const unsigned char *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned char[:,:] mem_view = data - dtype = 'uint8' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[unsigned char, ndim=2] ndarray_copy_uchar_F(const unsigned char *data, long rows, long cols, long row_stride, long col_stride): - cdef unsigned char[::1,:] mem_view = data - dtype = 'uint8' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# complex double -# - -@cython.boundscheck(False) -cdef np.ndarray[double complex, ndim=2] ndarray_complex_double(): - return np.empty((0,0), dtype='complex128') - -@cython.boundscheck(False) -cdef np.ndarray[double complex, ndim=2] ndarray_complex_double_C(double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef double complex[:,:] mem_view = data - dtype = 'complex128' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[double complex, ndim=2] ndarray_complex_double_F(double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef double complex[::1,:] mem_view = data - dtype = 'complex128' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[double complex, ndim=2] ndarray_copy_complex_double_C(const double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef double complex[:,:] mem_view = data - dtype = 'complex128' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[double complex, ndim=2] ndarray_copy_complex_double_F(const double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef double complex[::1,:] mem_view = data - dtype = 'complex128' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# complex long double -# - -@cython.boundscheck(False) -cdef np.ndarray[long double complex, ndim=2] ndarray_complex_long_double(): - return np.empty((0,0), dtype='clongdouble') - -@cython.boundscheck(False) -cdef np.ndarray[long double complex, ndim=2] ndarray_complex_long_double_C(long double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef long double complex[:,:] mem_view = data - dtype = 'clongdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[long double complex, ndim=2] ndarray_complex_long_double_F(long double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef long double complex[::1,:] mem_view = data - dtype = 'clongdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) - -@cython.boundscheck(False) -cdef np.ndarray[long double complex, ndim=2] ndarray_copy_complex_long_double_C(const long double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef long double complex[:,:] mem_view = data - dtype = 'clongdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) - -@cython.boundscheck(False) -cdef np.ndarray[long double complex, ndim=2] ndarray_copy_complex_long_double_F(const long double complex *data, long rows, long cols, long row_stride, long col_stride): - cdef long double complex[::1,:] mem_view = data - dtype = 'clongdouble' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) - -# -# complex float -# +# Array with limit 2D +# While data isn't used it is what causes cython to create a function for every type in array_type_t and gives us a defined data type to use with the numpy array @cython.boundscheck(False) -cdef np.ndarray[float complex, ndim=2] ndarray_complex_float(): - return np.empty((0,0), dtype='complex64') +cdef np.ndarray[array_type_t, ndim=2] ndarray(const array_type_t *data): + cdef array_type_t tmp # Yes, this line is necessary, otherwise we can't initialize the cython array + return np.empty_like((&tmp), shape=(0, 0)) @cython.boundscheck(False) -cdef np.ndarray[float complex, ndim=2] ndarray_complex_float_C(float complex *data, long rows, long cols, long row_stride, long col_stride): - cdef float complex[:,:] mem_view = data - dtype = 'complex64' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize]) +cdef np.ndarray[array_type_t, ndim=2] ndarray_C(array_type_t *data, long rows, long cols, long row_stride, long col_stride): + return as_strided(np.asarray(data, order="C"), strides=[row_stride, col_stride]) @cython.boundscheck(False) -cdef np.ndarray[float complex, ndim=2] ndarray_complex_float_F(float complex *data, long rows, long cols, long row_stride, long col_stride): - cdef float complex[::1,:] mem_view = data - dtype = 'complex64' - cdef int itemsize = np.dtype(dtype).itemsize - return as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize]) +cdef np.ndarray[array_type_t, ndim=2] ndarray_F(array_type_t *data, long rows, long cols, long row_stride, long col_stride): + return as_strided(np.asarray(data, order="F"), strides=[row_stride, col_stride]) @cython.boundscheck(False) -cdef np.ndarray[float complex, ndim=2] ndarray_copy_complex_float_C(const float complex *data, long rows, long cols, long row_stride, long col_stride): - cdef float complex[:,:] mem_view = data - dtype = 'complex64' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="C"), strides=[row_stride*itemsize, col_stride*itemsize])) +cdef np.ndarray[array_type_t, ndim=2] ndarray_copy_C(const array_type_t *data, long rows, long cols, long row_stride, long col_stride): + return np.copy(as_strided(np.asarray(data, order="C"), strides=[row_stride, col_stride])) @cython.boundscheck(False) -cdef np.ndarray[float complex, ndim=2] ndarray_copy_complex_float_F(const float complex *data, long rows, long cols, long row_stride, long col_stride): - cdef float complex[::1,:] mem_view = data - dtype = 'complex64' - cdef int itemsize = np.dtype(dtype).itemsize - return np.copy(as_strided(np.asarray(mem_view, dtype=dtype, order="F"), strides=[row_stride*itemsize, col_stride*itemsize])) +cdef np.ndarray[array_type_t, ndim=2] ndarray_copy_F(const array_type_t *data, long rows, long cols, long row_stride, long col_stride): + return np.copy(as_strided(np.asarray(data, order="F"), strides=[row_stride, col_stride])) diff --git a/eigency/core.pxd b/eigency/core.pxd index d54df04..3ecaede 100644 --- a/eigency/core.pxd +++ b/eigency/core.pxd @@ -1,19 +1,24 @@ -cimport cython cimport numpy as np -ctypedef signed char schar; -ctypedef unsigned char uchar; -ctypedef long double long_double; +# If this is changed, array_type_t in conversions.pxd needs to be changed too ctypedef fused dtype: - uchar - schar + signed char + unsigned char short + unsigned short int + unsigned int long + unsigned long + long long + unsigned long long float double - long_double + long double + float complex + double complex + long double complex ctypedef fused DenseType: Matrix diff --git a/eigency/eigency_cpp.h b/eigency/eigency_cpp.h index cc8069c..84d9c56 100644 --- a/eigency/eigency_cpp.h +++ b/eigency/eigency_cpp.h @@ -20,6 +20,7 @@ typedef ::std::complex< long double > __pyx_t_long_double_complex; namespace eigency { +// Eigen -> Numpy (2D) template inline PyArrayObject* _ndarray_view(Scalar *, long rows, long cols, bool is_row_major, long outer_stride=0, long inner_stride=0); template @@ -37,61 +38,62 @@ inline PyArrayObject* _ndarray_copy(const Scalar *, long rows, long cols, bool i #define _NDAV(TYPE, FUNC_NAME_E, FUNC_NAME_C, FUNC_NAME_F) template<> \ inline PyArrayObject* _ndarray_view< TYPE >(TYPE *data, long rows, long cols, bool is_row_major, long outer_stride, long inner_stride) { \ if (data == nullptr) { \ - return FUNC_NAME_E(); \ + return FUNC_NAME_E(data); \ } else if (is_row_major) { \ /* Eigen row-major mode: row_stride=outer_stride, and col_stride=inner_stride */ \ /* If no stride is given, the row_stride is set to the number of columns. */ \ - return FUNC_NAME_C(data, rows, cols, outer_stride>0?outer_stride:cols, inner_stride>0?inner_stride:1); \ + return FUNC_NAME_C(data, rows, cols, (outer_stride>0?outer_stride:cols)*sizeof(TYPE), (inner_stride>0?inner_stride:1)*sizeof(TYPE));\ } else { \ /* Eigen column-major mode: row_stride=outer_stride, and col_stride=inner_stride */ \ /* If no stride is given, the cow_stride is set to the number of rows. */ \ - return FUNC_NAME_F(data, rows, cols, inner_stride>0?inner_stride:1, outer_stride>0?outer_stride:rows); \ + return FUNC_NAME_F(data, rows, cols, (inner_stride>0?inner_stride:1)*sizeof(TYPE), (outer_stride>0?outer_stride:rows)*sizeof(TYPE));\ } \ } #define _NDAC(TYPE, FUNC_NAME_E, FUNC_NAME_C, FUNC_NAME_F) template<> \ inline PyArrayObject* _ndarray_copy< TYPE >(const TYPE *data, long rows, long cols, bool is_row_major, long outer_stride, long inner_stride) { \ if (data == nullptr) { \ - return FUNC_NAME_E(); \ + return FUNC_NAME_E(data); \ } else if (is_row_major) { \ - return FUNC_NAME_C(data, rows, cols, outer_stride>0?outer_stride:cols, inner_stride>0?inner_stride:1); \ + return FUNC_NAME_C(data, rows, cols, (outer_stride>0?outer_stride:cols)*sizeof(TYPE), (inner_stride>0?inner_stride:1)*sizeof(TYPE)); \ } else { \ - return FUNC_NAME_F(data, rows, cols, inner_stride>0?inner_stride:1, outer_stride>0?outer_stride:rows); \ + return FUNC_NAME_F(data, rows, cols, (inner_stride>0?inner_stride:1)*sizeof(TYPE), (outer_stride>0?outer_stride:rows)*sizeof(TYPE)); \ } \ } -_NDAV(long double, ndarray_long_double, ndarray_long_double_C, ndarray_long_double_F) -_NDAC(long double, ndarray_long_double, ndarray_copy_long_double_C, ndarray_copy_long_double_F) -_NDAV(double, ndarray_double, ndarray_double_C, ndarray_double_F) -_NDAC(double, ndarray_double, ndarray_copy_double_C, ndarray_copy_double_F) -_NDAV(float, ndarray_float, ndarray_float_C, ndarray_float_F) -_NDAC(float, ndarray_float, ndarray_copy_float_C, ndarray_copy_float_F) -_NDAV(long, ndarray_long, ndarray_long_C, ndarray_long_F) -_NDAC(long, ndarray_long, ndarray_copy_long_C, ndarray_copy_long_F) -_NDAV(long long, ndarray_long_long, ndarray_long_long_C, ndarray_long_long_F) -_NDAC(long long, ndarray_long_long, ndarray_copy_long_long_C, ndarray_copy_long_long_F) -_NDAV(unsigned long, ndarray_ulong, ndarray_ulong_C, ndarray_ulong_F) -_NDAC(unsigned long, ndarray_ulong, ndarray_copy_ulong_C, ndarray_copy_ulong_F) -_NDAV(unsigned long long, ndarray_ulong_long, ndarray_ulong_long_C, ndarray_ulong_long_F) -_NDAC(unsigned long long, ndarray_ulong_long, ndarray_copy_ulong_long_C, ndarray_copy_ulong_long_F) -_NDAV(int, ndarray_int, ndarray_int_C, ndarray_int_F) -_NDAC(int, ndarray_int, ndarray_copy_int_C, ndarray_copy_int_F) -_NDAV(unsigned int, ndarray_uint, ndarray_uint_C, ndarray_uint_F) -_NDAC(unsigned int, ndarray_uint, ndarray_copy_uint_C, ndarray_copy_uint_F) -_NDAV(short, ndarray_short, ndarray_short_C, ndarray_short_F) -_NDAC(short, ndarray_short, ndarray_copy_short_C, ndarray_copy_short_F) -_NDAV(unsigned short, ndarray_ushort, ndarray_ushort_C, ndarray_ushort_F) -_NDAC(unsigned short, ndarray_ushort, ndarray_copy_ushort_C, ndarray_copy_ushort_F) -_NDAV(signed char, ndarray_schar, ndarray_schar_C, ndarray_schar_F) -_NDAC(signed char, ndarray_schar, ndarray_copy_schar_C, ndarray_copy_schar_F) -_NDAV(unsigned char, ndarray_uchar, ndarray_uchar_C, ndarray_uchar_F) -_NDAC(unsigned char, ndarray_uchar, ndarray_copy_uchar_C, ndarray_copy_uchar_F) -_NDAV(std::complex, ndarray_complex_long_double, ndarray_complex_long_double_C, ndarray_complex_long_double_F) -_NDAC(std::complex, ndarray_complex_long_double, ndarray_copy_complex_long_double_C, ndarray_copy_complex_long_double_F) -_NDAV(std::complex, ndarray_complex_double, ndarray_complex_double_C, ndarray_complex_double_F) -_NDAC(std::complex, ndarray_complex_double, ndarray_copy_complex_double_C, ndarray_copy_complex_double_F) -_NDAV(std::complex, ndarray_complex_float, ndarray_complex_float_C, ndarray_complex_float_F) -_NDAC(std::complex, ndarray_complex_float, ndarray_copy_complex_float_C, ndarray_copy_complex_float_F) +// This is the currently best option for this mess, tho if I can figure out a better one, I will banish this thing back to the place from whence it came +_NDAV(signed char, __pyx_fuse_0ndarray, __pyx_fuse_0ndarray_C, __pyx_fuse_0ndarray_F) +_NDAC(signed char, __pyx_fuse_0ndarray, __pyx_fuse_0ndarray_copy_C, __pyx_fuse_0ndarray_copy_F) +_NDAV(unsigned char, __pyx_fuse_1ndarray, __pyx_fuse_1ndarray_C, __pyx_fuse_1ndarray_F) +_NDAC(unsigned char, __pyx_fuse_1ndarray, __pyx_fuse_1ndarray_copy_C, __pyx_fuse_1ndarray_copy_F) +_NDAV(short, __pyx_fuse_2ndarray, __pyx_fuse_2ndarray_C, __pyx_fuse_2ndarray_F) +_NDAC(short, __pyx_fuse_2ndarray, __pyx_fuse_2ndarray_copy_C, __pyx_fuse_2ndarray_copy_F) +_NDAV(unsigned short, __pyx_fuse_3ndarray, __pyx_fuse_3ndarray_C, __pyx_fuse_3ndarray_F) +_NDAC(unsigned short, __pyx_fuse_3ndarray, __pyx_fuse_3ndarray_copy_C, __pyx_fuse_3ndarray_copy_F) +_NDAV(int, __pyx_fuse_4ndarray, __pyx_fuse_4ndarray_C, __pyx_fuse_4ndarray_F) +_NDAC(int, __pyx_fuse_4ndarray, __pyx_fuse_4ndarray_copy_C, __pyx_fuse_4ndarray_copy_F) +_NDAV(unsigned int, __pyx_fuse_5ndarray, __pyx_fuse_5ndarray_C, __pyx_fuse_5ndarray_F) +_NDAC(unsigned int, __pyx_fuse_5ndarray, __pyx_fuse_5ndarray_copy_C, __pyx_fuse_5ndarray_copy_F) +_NDAV(long, __pyx_fuse_6ndarray, __pyx_fuse_6ndarray_C, __pyx_fuse_6ndarray_F) +_NDAC(long, __pyx_fuse_6ndarray, __pyx_fuse_6ndarray_copy_C, __pyx_fuse_6ndarray_copy_F) +_NDAV(unsigned long, __pyx_fuse_7ndarray, __pyx_fuse_7ndarray_C, __pyx_fuse_7ndarray_F) +_NDAC(unsigned long, __pyx_fuse_7ndarray, __pyx_fuse_7ndarray_copy_C, __pyx_fuse_7ndarray_copy_F) +_NDAV(long long, __pyx_fuse_8ndarray, __pyx_fuse_8ndarray_C, __pyx_fuse_8ndarray_F) +_NDAC(long long, __pyx_fuse_8ndarray, __pyx_fuse_8ndarray_copy_C, __pyx_fuse_8ndarray_copy_F) +_NDAV(unsigned long long, __pyx_fuse_9ndarray, __pyx_fuse_9ndarray_C, __pyx_fuse_9ndarray_F) +_NDAC(unsigned long long, __pyx_fuse_9ndarray, __pyx_fuse_9ndarray_copy_C, __pyx_fuse_9ndarray_copy_F) +_NDAV(float, __pyx_fuse_10ndarray, __pyx_fuse_10ndarray_C, __pyx_fuse_10ndarray_F) +_NDAC(float, __pyx_fuse_10ndarray, __pyx_fuse_10ndarray_copy_C, __pyx_fuse_10ndarray_copy_F) +_NDAV(double, __pyx_fuse_11ndarray, __pyx_fuse_11ndarray_C, __pyx_fuse_11ndarray_F) +_NDAC(double, __pyx_fuse_11ndarray, __pyx_fuse_11ndarray_copy_C, __pyx_fuse_11ndarray_copy_F) +_NDAV(long double, __pyx_fuse_12ndarray, __pyx_fuse_12ndarray_C, __pyx_fuse_12ndarray_F) +_NDAC(long double, __pyx_fuse_12ndarray, __pyx_fuse_12ndarray_copy_C, __pyx_fuse_12ndarray_copy_F) +_NDAV(std::complex, __pyx_fuse_13ndarray, __pyx_fuse_13ndarray_C, __pyx_fuse_13ndarray_F) +_NDAC(std::complex, __pyx_fuse_13ndarray, __pyx_fuse_13ndarray_copy_C, __pyx_fuse_13ndarray_copy_F) +_NDAV(std::complex, __pyx_fuse_14ndarray, __pyx_fuse_14ndarray_C, __pyx_fuse_14ndarray_F) +_NDAC(std::complex, __pyx_fuse_14ndarray, __pyx_fuse_14ndarray_copy_C, __pyx_fuse_14ndarray_copy_F) +_NDAV(std::complex, __pyx_fuse_15ndarray, __pyx_fuse_15ndarray_C, __pyx_fuse_15ndarray_F) +_NDAC(std::complex, __pyx_fuse_15ndarray, __pyx_fuse_15ndarray_copy_C, __pyx_fuse_15ndarray_copy_F) #undef _NDAV #undef _NDAC diff --git a/tests/eigency_tests/eigency_tests.pyx b/tests/eigency_tests/eigency_tests.pyx index d596228..ef0e9e9 100644 --- a/tests/eigency_tests/eigency_tests.pyx +++ b/tests/eigency_tests/eigency_tests.pyx @@ -1,7 +1,8 @@ # distutils: language = c++ # distutils: sources = eigency_tests/eigency_tests_cpp.cpp +import numpy as np from eigency.core cimport * -from libc.stdint cimport int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t +from libc.stdint cimport int8_t, uint8_t, int16_t, uint16_t, uint32_t, int64_t, uint64_t # cimport eigency.conversions # from eigency_tests.eigency cimport * diff --git a/tests/run_tests.py b/tests/run_tests.py index 20d2ceb..ba7020b 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -189,13 +189,13 @@ def test_function_type_uint16(self): def test_function_type_int32(self): # C++ long - Note that this is the standard Python integer - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.long) + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int32) mat_out = eigency_tests.function_type_int32(mat_in) assert_array_equal(mat_in, mat_out) def test_function_type_uint32(self): # C++ ulong - mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.ulong) + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint32) mat_out = eigency_tests.function_type_uint32(mat_in) assert_array_equal(mat_in, mat_out) From eb59a3b14bc5c3a4065ff9fbd87b8af39f1e5012 Mon Sep 17 00:00:00 2001 From: Lina Krings <68695780+maki4748@users.noreply.github.com> Date: Sat, 30 Nov 2024 03:25:42 +0100 Subject: [PATCH 3/7] Fixed up the comments Signed-off-by: Maki4748 <68695780+maki4748@users.noreply.github.com> --- tests/eigency_tests/eigency_tests.pyx | 12 ++++++------ tests/run_tests.py | 16 +++++++--------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/tests/eigency_tests/eigency_tests.pyx b/tests/eigency_tests/eigency_tests.pyx index ef0e9e9..72aa2ad 100644 --- a/tests/eigency_tests/eigency_tests.pyx +++ b/tests/eigency_tests/eigency_tests.pyx @@ -157,27 +157,27 @@ def function_type_int8(np.ndarray[np.int8_t, ndim=2] array): def function_type_uint8(np.ndarray[np.uint8_t, ndim=2] array): return ndarray(_function_type_uint8(FlattenedMap[Array, uint8_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: signed char +# Functions with different matrix types: short/int def function_type_int16(np.ndarray[np.int16_t, ndim=2] array): return ndarray(_function_type_int16(FlattenedMap[Array, int16_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: unsigned char +# Functions with different matrix types: unsigned short/unsigned int def function_type_uint16(np.ndarray[np.uint16_t, ndim=2] array): return ndarray(_function_type_uint16(FlattenedMap[Array, uint16_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: signed char +# Functions with different matrix types: long def function_type_int32(np.ndarray[np.int32_t, ndim=2] array): return ndarray(_function_type_int32(Map[ArrayXXi](array))) -# Functions with different matrix types: unsigned char +# Functions with different matrix types: unsigned long def function_type_uint32(np.ndarray[np.uint32_t, ndim=2] array): return ndarray(_function_type_uint32(FlattenedMap[Array, uint32_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: signed char +# Functions with different matrix types: long long def function_type_int64(np.ndarray[np.int64_t, ndim=2] array): return ndarray(_function_type_int64(FlattenedMap[Array, int64_t, Dynamic, Dynamic](array))) -# Functions with different matrix types: unsigned char +# Functions with different matrix types: unsigned long long def function_type_uint64(np.ndarray[np.uint64_t, ndim=2] array): return ndarray(_function_type_uint64(FlattenedMap[Array, uint64_t, Dynamic, Dynamic](array))) diff --git a/tests/run_tests.py b/tests/run_tests.py index ba7020b..6df33e0 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -162,7 +162,7 @@ def test_function_type_float64(self): assert_array_equal(mat_in, mat_out) def test_function_type_int8(self): - # C++ char + # C++ signed char mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int8) mat_out = eigency_tests.function_type_int8(mat_in) assert_array_equal(mat_in, mat_out) @@ -174,39 +174,37 @@ def test_function_type_uint8(self): assert_array_equal(mat_in, mat_out) def test_function_type_int16(self): - # C++ long - Note that this is the standard Python integer - # with numpy 1 int is 32 in numpy 2 int is 64, so we need to be more specific + # C++ short/int mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int16) mat_out = eigency_tests.function_type_int16(mat_in) assert_array_equal(mat_in, mat_out) def test_function_type_uint16(self): - # C++ long - Note that this is the standard Python integer - # with numpy 1 int is 32 in numpy 2 int is 64, so we need to be more specific + # C++ unsigned short/unsigned int mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint16) mat_out = eigency_tests.function_type_uint16(mat_in) assert_array_equal(mat_in, mat_out) def test_function_type_int32(self): - # C++ long - Note that this is the standard Python integer + # C++ long mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int32) mat_out = eigency_tests.function_type_int32(mat_in) assert_array_equal(mat_in, mat_out) def test_function_type_uint32(self): - # C++ ulong + # C++ unsigned long mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint32) mat_out = eigency_tests.function_type_uint32(mat_in) assert_array_equal(mat_in, mat_out) def test_function_type_int64(self): - # C++ int + # C++ long long mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.int64) mat_out = eigency_tests.function_type_int64(mat_in) assert_array_equal(mat_in, mat_out) def test_function_type_uint64(self): - # C++ uint + # C++ unsigned long long mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.uint64) mat_out = eigency_tests.function_type_uint64(mat_in) assert_array_equal(mat_in, mat_out) From 9c9533a27d394aaae606d56cd70e2e245152c0cd Mon Sep 17 00:00:00 2001 From: Maki4748 <68695780+maki4748@users.noreply.github.com> Date: Sat, 30 Nov 2024 03:58:50 +0100 Subject: [PATCH 4/7] Removed unused import Signed-off-by: Maki4748 <68695780+maki4748@users.noreply.github.com> --- eigency/conversions.pxd | 1 - eigency/core.pxd | 1 - tests/eigency_tests/eigency_tests.pyx | 5 ++++- tests/run_tests.py | 2 -- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/eigency/conversions.pxd b/eigency/conversions.pxd index f98a495..da73a87 100644 --- a/eigency/conversions.pxd +++ b/eigency/conversions.pxd @@ -1,6 +1,5 @@ cimport numpy as np - # If this is changed, array_type_t in core.pxd needs to be changed too # Also, the handling of the types needs to be changed in eigency_cpp.h for NDAC and NDAV ctypedef fused array_type_t: diff --git a/eigency/core.pxd b/eigency/core.pxd index 3ecaede..1432e9d 100644 --- a/eigency/core.pxd +++ b/eigency/core.pxd @@ -1,6 +1,5 @@ cimport numpy as np - # If this is changed, array_type_t in conversions.pxd needs to be changed too ctypedef fused dtype: signed char diff --git a/tests/eigency_tests/eigency_tests.pyx b/tests/eigency_tests/eigency_tests.pyx index 72aa2ad..59518cc 100644 --- a/tests/eigency_tests/eigency_tests.pyx +++ b/tests/eigency_tests/eigency_tests.pyx @@ -1,8 +1,11 @@ # distutils: language = c++ # distutils: sources = eigency_tests/eigency_tests_cpp.cpp import numpy as np + +from libc.stdint cimport (int8_t, int16_t, int64_t, uint8_t, uint16_t, + uint32_t, uint64_t) + from eigency.core cimport * -from libc.stdint cimport int8_t, uint8_t, int16_t, uint16_t, uint32_t, int64_t, uint64_t # cimport eigency.conversions # from eigency_tests.eigency cimport * diff --git a/tests/run_tests.py b/tests/run_tests.py index 6df33e0..a973afb 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -1,7 +1,5 @@ import unittest -import cython - import eigency_tests import numpy as np from numpy.testing import assert_array_equal From 955470209f066b5fa290ca71347b4206af5c7407 Mon Sep 17 00:00:00 2001 From: Lina Krings <68695780+maki4748@users.noreply.github.com> Date: Sat, 30 Nov 2024 14:59:19 +0100 Subject: [PATCH 5/7] Added variable width type checks Signed-off-by: Maki4748 <68695780+maki4748@users.noreply.github.com> --- eigency/core.pxd | 13 +++++++---- tests/eigency_tests/eigency_tests.pyx | 24 ++++++++++++++++++-- tests/eigency_tests/eigency_tests_cpp.cpp | 25 +++++++++++++++++++++ tests/eigency_tests/eigency_tests_cpp.h | 6 +++++ tests/run_tests.py | 27 +++++++++++++++++++++++ 5 files changed, 89 insertions(+), 6 deletions(-) diff --git a/eigency/core.pxd b/eigency/core.pxd index 1432e9d..a6c4b51 100644 --- a/eigency/core.pxd +++ b/eigency/core.pxd @@ -1,5 +1,10 @@ cimport numpy as np +ctypedef long long long_long; +ctypedef unsigned long long u_long_long; +ctypedef long double long_double; +ctypedef long double complex c_long_double; + # If this is changed, array_type_t in conversions.pxd needs to be changed too ctypedef fused dtype: signed char @@ -10,14 +15,14 @@ ctypedef fused dtype: unsigned int long unsigned long - long long - unsigned long long + long_long + u_long_long float double - long double + long_double float complex double complex - long double complex + c_long_double ctypedef fused DenseType: Matrix diff --git a/tests/eigency_tests/eigency_tests.pyx b/tests/eigency_tests/eigency_tests.pyx index 59518cc..66ecfae 100644 --- a/tests/eigency_tests/eigency_tests.pyx +++ b/tests/eigency_tests/eigency_tests.pyx @@ -4,6 +4,7 @@ import numpy as np from libc.stdint cimport (int8_t, int16_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t) +from numpy.lib._version import NumpyVersion from eigency.core cimport * @@ -58,8 +59,15 @@ cdef extern from "eigency_tests/eigency_tests_cpp.h": cdef PlainObjectBase _function_type_uint32 "function_type_uint32" (FlattenedMap[Array, uint32_t, Dynamic, Dynamic] &) cdef PlainObjectBase _function_type_int64 "function_type_int64" (FlattenedMap[Array, int64_t, Dynamic, Dynamic] &) cdef PlainObjectBase _function_type_uint64 "function_type_uint64" (FlattenedMap[Array, uint64_t, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_complex_float "function_type_complex_float" (Map[ArrayXXcf] &) - cdef PlainObjectBase _function_type_complex_double "function_type_complex_double" (Map[ArrayXXcd] &) + cdef PlainObjectBase _function_type_complex_float "function_type_complex_float"(Map[ArrayXXcf] &) + cdef PlainObjectBase _function_type_complex_double "function_type_complex_double"(Map[ArrayXXcd] &) + + cdef PlainObjectBase _function_type_longdouble "function_type_longdouble"(FlattenedMap[Array, long double, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_long "function_type_long"(FlattenedMap[Array, long, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_ulong "function_type_ulong"(FlattenedMap[Array, unsigned long, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_longlong "function_type_longlong"(FlattenedMap[Array, long long, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_ulonglong "function_type_ulonglong"(FlattenedMap[Array, unsigned long long, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_clongdouble "function_type_clongdouble"(FlattenedMap[Array, long double complex, Dynamic, Dynamic] &) cdef PlainObjectBase _function_single_col_matrix "function_single_col_matrix" (Map[ArrayXXd] &) @@ -192,6 +200,18 @@ def function_type_complex64(np.ndarray[np.complex64_t, ndim=2] array): def function_type_complex128(np.ndarray[np.complex128_t, ndim=2] array): return ndarray(_function_type_complex_double(Map[ArrayXXcd](array))) +# Functions with different matrix types: long long +def function_type_longlong(np.ndarray[np.longlong_t, ndim=2] array): + return ndarray(_function_type_longlong(FlattenedMap[Array, long_long, Dynamic, Dynamic](array))) + +# Functions with different matrix types: unsigned long long +def function_type_ulonglong(np.ndarray[np.ulonglong_t, ndim=2] array): + return ndarray(_function_type_ulonglong(FlattenedMap[Array, u_long_long, Dynamic, Dynamic](array))) + +# Functions with different matrix types: long double complex +def function_type_clongdouble(np.ndarray[np.clongdouble_t, ndim=2] array): + return ndarray(_function_type_clongdouble(FlattenedMap[Array, c_long_double, Dynamic, Dynamic](array))) + # Functions testing a matrix with only one column def function_single_col_matrix(np.ndarray[np.float64_t, ndim=2] array): return ndarray(_function_single_col_matrix(Map[ArrayXXd](array))) diff --git a/tests/eigency_tests/eigency_tests_cpp.cpp b/tests/eigency_tests/eigency_tests_cpp.cpp index 2178222..7931bea 100644 --- a/tests/eigency_tests/eigency_tests_cpp.cpp +++ b/tests/eigency_tests/eigency_tests_cpp.cpp @@ -124,6 +124,31 @@ Eigen::Array function_type_uint64(Eige return output; } +Eigen::Array function_type_long(Eigen::Map > &mat) { + Eigen::Array output = mat; + return output; +} + +Eigen::Array function_type_ulong(Eigen::Map > &mat) { + Eigen::Array output = mat; + return output; +} + +Eigen::Array function_type_longlong(Eigen::Map > &mat) { + Eigen::Array output = mat; + return output; +} + +Eigen::Array function_type_ulonglong(Eigen::Map > &mat) { + Eigen::Array output = mat; + return output; +} + +Eigen::Array<::std::complex< long double >, Eigen::Dynamic, Eigen::Dynamic> function_type_clongdouble(Eigen::Map, Eigen::Dynamic, Eigen::Dynamic> > &mat) { + Eigen::Array<::std::complex< long double >, Eigen::Dynamic, Eigen::Dynamic> output = mat; + return output; +} + Eigen::ArrayXXcd function_type_complex_double(Eigen::Map &mat) { Eigen::ArrayXXcd output = mat; return output; diff --git a/tests/eigency_tests/eigency_tests_cpp.h b/tests/eigency_tests/eigency_tests_cpp.h index d87dc4b..c6f200c 100644 --- a/tests/eigency_tests/eigency_tests_cpp.h +++ b/tests/eigency_tests/eigency_tests_cpp.h @@ -52,6 +52,12 @@ Eigen::Array function_type_uint64(Eige Eigen::ArrayXXcf function_type_complex_float(Eigen::Map &array); Eigen::ArrayXXcd function_type_complex_double(Eigen::Map &array); +Eigen::Array function_type_long(Eigen::Map > &mat); +Eigen::Array function_type_ulong(Eigen::Map > &mat); +Eigen::Array function_type_longlong(Eigen::Map > &mat); +Eigen::Array function_type_ulonglong(Eigen::Map > &mat); +Eigen::Array<::std::complex< long double >, Eigen::Dynamic, Eigen::Dynamic> function_type_clongdouble(Eigen::Map, Eigen::Dynamic, Eigen::Dynamic> > &mat); + Eigen::Map function_single_col_matrix(Eigen::Map &array); diff --git a/tests/run_tests.py b/tests/run_tests.py index a973afb..e18bc6b 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -147,6 +147,7 @@ def test_mat_ref_retval_array_row_major(self): y = my_object.get_array_copy() assert_array_equal(x, y) + # Fixed width types def test_function_type_float32(self): # C++ float mat_in = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]], order="F", dtype=np.float32) @@ -233,6 +234,32 @@ def test_function_type_complex128(self): mat_out = eigency_tests.function_type_complex128(mat_in) assert_array_equal(mat_in, mat_out) + def test_function_type_longlong(self): + # C++ long long + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.longlong) + mat_out = eigency_tests.function_type_longlong(mat_in) + assert_array_equal(mat_in, mat_out) + + def test_function_type_ulonglong(self): + # C++ unsigned long long + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.ulonglong) + mat_out = eigency_tests.function_type_ulonglong(mat_in) + assert_array_equal(mat_in, mat_out) + + def test_function_type_clongdouble(self): + # C++ long double complex + mat_in = np.array( + [ + [1.0 + 1.0j, 2.0 + 2.0j, 3.0 + 3.0j, 4.0 + 4.0j], + [5.0 + 5.0j, 6.0 + 6.0j, 7.0 + 7.0j, 8.0 + 8.0j], + ], + order="F", + dtype=np.clongdouble, + ) + mat_out = eigency_tests.function_type_clongdouble(mat_in) + assert_array_equal(mat_in, mat_out) + + # Edge case tests def test_function_single_col_matrix(self): # Issue #11 mat_in = np.zeros((2, 3), order="F") From 18d62b27ac79327b49557783024f72bac5b7f55d Mon Sep 17 00:00:00 2001 From: Lina Krings <68695780+maki4748@users.noreply.github.com> Date: Sat, 30 Nov 2024 22:36:15 +0100 Subject: [PATCH 6/7] Added tests for long and ulong with version checks to only include them for numpy>=2.0.0 Signed-off-by: Maki4748 <68695780+maki4748@users.noreply.github.com> --- eigency/core.pxd | 1 + tests/eigency_tests/__init__.py | 6 ++++++ tests/eigency_tests/eigency_np2.pyx | 20 ++++++++++++++++++++ tests/eigency_tests/eigency_tests.pyx | 4 ---- tests/run_tests.py | 14 ++++++++++++++ tests/setup.py | 13 ++++++++++++- 6 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/eigency_tests/eigency_np2.pyx diff --git a/eigency/core.pxd b/eigency/core.pxd index a6c4b51..0324ebc 100644 --- a/eigency/core.pxd +++ b/eigency/core.pxd @@ -1,5 +1,6 @@ cimport numpy as np +ctypedef unsigned long u_long ctypedef long long long_long; ctypedef unsigned long long u_long_long; ctypedef long double long_double; diff --git a/tests/eigency_tests/__init__.py b/tests/eigency_tests/__init__.py index 7eb0eb5..9527cad 100644 --- a/tests/eigency_tests/__init__.py +++ b/tests/eigency_tests/__init__.py @@ -1 +1,7 @@ +from numpy import __version__ as numpy_version +from numpy.lib import NumpyVersion + from .eigency_tests import * + +if NumpyVersion(numpy_version) >= "2.0.0": + from .eigency_np2 import * diff --git a/tests/eigency_tests/eigency_np2.pyx b/tests/eigency_tests/eigency_np2.pyx new file mode 100644 index 0000000..b9c4b59 --- /dev/null +++ b/tests/eigency_tests/eigency_np2.pyx @@ -0,0 +1,20 @@ +# distutils: language = c++ +# distutils: sources = eigency_tests/eigency_tests_cpp.cpp +import numpy as np + +from eigency_tests cimport * + +from eigency.core cimport * + + +cdef extern from "eigency_tests/eigency_tests_cpp.h": + cdef PlainObjectBase _function_type_long "function_type_long"(FlattenedMap[Array, long, Dynamic, Dynamic] &) + cdef PlainObjectBase _function_type_ulong "function_type_ulong"(FlattenedMap[Array, unsigned long, Dynamic, Dynamic] &) + +# Functions with different matrix types: unsigned long +def function_type_long(np.ndarray[long, ndim=2] array): + return ndarray(_function_type_long(FlattenedMap[Array, long, Dynamic, Dynamic](array))) + +# Functions with different matrix types: unsigned long +def function_type_ulong(np.ndarray[u_long, ndim=2] array): + return ndarray(_function_type_ulong(FlattenedMap[Array, u_long, Dynamic, Dynamic](array))) diff --git a/tests/eigency_tests/eigency_tests.pyx b/tests/eigency_tests/eigency_tests.pyx index 66ecfae..a5d5a8b 100644 --- a/tests/eigency_tests/eigency_tests.pyx +++ b/tests/eigency_tests/eigency_tests.pyx @@ -4,7 +4,6 @@ import numpy as np from libc.stdint cimport (int8_t, int16_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t) -from numpy.lib._version import NumpyVersion from eigency.core cimport * @@ -62,9 +61,6 @@ cdef extern from "eigency_tests/eigency_tests_cpp.h": cdef PlainObjectBase _function_type_complex_float "function_type_complex_float"(Map[ArrayXXcf] &) cdef PlainObjectBase _function_type_complex_double "function_type_complex_double"(Map[ArrayXXcd] &) - cdef PlainObjectBase _function_type_longdouble "function_type_longdouble"(FlattenedMap[Array, long double, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_long "function_type_long"(FlattenedMap[Array, long, Dynamic, Dynamic] &) - cdef PlainObjectBase _function_type_ulong "function_type_ulong"(FlattenedMap[Array, unsigned long, Dynamic, Dynamic] &) cdef PlainObjectBase _function_type_longlong "function_type_longlong"(FlattenedMap[Array, long long, Dynamic, Dynamic] &) cdef PlainObjectBase _function_type_ulonglong "function_type_ulonglong"(FlattenedMap[Array, unsigned long long, Dynamic, Dynamic] &) cdef PlainObjectBase _function_type_clongdouble "function_type_clongdouble"(FlattenedMap[Array, long double complex, Dynamic, Dynamic] &) diff --git a/tests/run_tests.py b/tests/run_tests.py index e18bc6b..170e50f 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -234,6 +234,20 @@ def test_function_type_complex128(self): mat_out = eigency_tests.function_type_complex128(mat_in) assert_array_equal(mat_in, mat_out) + if np.lib.NumpyVersion(np.__version__) >= "2.0.0": + + def test_function_type_long(self): + # C++ long + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.long) + mat_out = eigency_tests.function_type_long(mat_in) + assert_array_equal(mat_in, mat_out) + + def test_function_type_ulong(self): + # C++ unsigned long + mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.ulong) + mat_out = eigency_tests.function_type_ulong(mat_in) + assert_array_equal(mat_in, mat_out) + def test_function_type_longlong(self): # C++ long long mat_in = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], order="F", dtype=np.longlong) diff --git a/tests/setup.py b/tests/setup.py index 199b014..bf7fac8 100644 --- a/tests/setup.py +++ b/tests/setup.py @@ -1,3 +1,4 @@ +import numpy as np from Cython.Build import cythonize from setuptools import setup from setuptools.extension import Extension @@ -7,12 +8,22 @@ extensions = [ Extension( "eigency_tests.eigency_tests", - ["eigency_tests/eigency_tests.pyx"], + sources=["eigency_tests/eigency_tests.pyx"], include_dirs=[".", "eigency_tests"] + eigency.get_includes(), define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")], ), ] +if np.lib.NumpyVersion(np.__version__) >= "2.0.0": + extensions.append( + Extension( + "eigency_tests.eigency_np2", + sources=["eigency_tests/eigency_np2.pyx"], + include_dirs=[".", "eigency_tests"] + eigency.get_includes(), + define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")], + ) + ) + setup( name="eigency-tests", version="0.0.0", From 98785348eb63d42db8abf23bfbd6d54324349cc0 Mon Sep 17 00:00:00 2001 From: Lina Krings <68695780+maki4748@users.noreply.github.com> Date: Tue, 3 Dec 2024 22:29:11 +0100 Subject: [PATCH 7/7] Changed comments to reflect seriousness of changes to fused types, NDAC and NDAV Signed-off-by: Maki4748 <68695780+maki4748@users.noreply.github.com> --- eigency/conversions.pxd | 3 +++ eigency/core.pxd | 3 +++ 2 files changed, 6 insertions(+) diff --git a/eigency/conversions.pxd b/eigency/conversions.pxd index da73a87..23830fc 100644 --- a/eigency/conversions.pxd +++ b/eigency/conversions.pxd @@ -1,7 +1,9 @@ cimport numpy as np +### Warning ### # If this is changed, array_type_t in core.pxd needs to be changed too # Also, the handling of the types needs to be changed in eigency_cpp.h for NDAC and NDAV +# Otherwise this will break! ctypedef fused array_type_t: signed char unsigned char @@ -19,6 +21,7 @@ ctypedef fused array_type_t: float complex double complex long double complex +############## # Array with limit 2D diff --git a/eigency/core.pxd b/eigency/core.pxd index 0324ebc..38238b7 100644 --- a/eigency/core.pxd +++ b/eigency/core.pxd @@ -6,7 +6,9 @@ ctypedef unsigned long long u_long_long; ctypedef long double long_double; ctypedef long double complex c_long_double; +### Warning ### # If this is changed, array_type_t in conversions.pxd needs to be changed too +# Otherwise this will break! ctypedef fused dtype: signed char unsigned char @@ -24,6 +26,7 @@ ctypedef fused dtype: float complex double complex c_long_double +############## ctypedef fused DenseType: Matrix