Skip to content

Commit 0d0b9a1

Browse files
committed
test: add test of complex types
1 parent 7c81b8a commit 0d0b9a1

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

unittest/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# Copyright (c) 2014-2019 CNRS
3-
# Copyright (c) 2018-2019 INRIA
3+
# Copyright (c) 2018-2020 INRIA
44
#
55

66
MACRO(ADD_LIB_UNIT_TEST test PKGS)
@@ -31,12 +31,14 @@ ENDMACRO(ADD_LIB_UNIT_TEST)
3131

3232
ADD_LIB_UNIT_TEST(matrix "eigen3")
3333
ADD_LIB_UNIT_TEST(geometry "eigen3")
34+
ADD_LIB_UNIT_TEST(complex "eigen3")
3435
IF(NOT ${EIGEN3_VERSION} VERSION_LESS "3.2.0")
3536
ADD_LIB_UNIT_TEST(ref "eigen3")
3637
ENDIF()
3738

3839
ADD_PYTHON_UNIT_TEST("py-matrix" "unittest/python/test_matrix.py" "unittest")
3940
ADD_PYTHON_UNIT_TEST("py-geometry" "unittest/python/test_geometry.py" "unittest")
41+
ADD_PYTHON_UNIT_TEST("py-complex" "unittest/python/test_complex.py" "unittest")
4042

4143
ADD_PYTHON_UNIT_TEST("py-switch" "unittest/python/test_switch.py" "python/eigenpy")
4244
SET_TESTS_PROPERTIES("py-switch" PROPERTIES DEPENDS ${PYWRAP})

unittest/complex.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2020 INRIA
3+
*/
4+
5+
#include "eigenpy/eigenpy.hpp"
6+
7+
namespace Eigen
8+
{
9+
#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
10+
/** \ingroup matrixtypedefs */ \
11+
typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
12+
/** \ingroup matrixtypedefs */ \
13+
typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
14+
/** \ingroup matrixtypedefs */ \
15+
typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
16+
17+
#define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
18+
/** \ingroup matrixtypedefs */ \
19+
typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
20+
/** \ingroup matrixtypedefs */ \
21+
typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
22+
23+
#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
24+
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
25+
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
26+
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
27+
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
28+
EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
29+
EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
30+
EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
31+
32+
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(long double, ld)
33+
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<long double>, cld)
34+
35+
#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
36+
#undef EIGEN_MAKE_TYPEDEFS
37+
#undef EIGEN_MAKE_FIXED_TYPEDEFS
38+
}
39+
40+
template<typename ComplexMatrix>
41+
typename Eigen::Matrix<typename ComplexMatrix::RealScalar,ComplexMatrix::RowsAtCompileTime,ComplexMatrix::ColsAtCompileTime,ComplexMatrix::Options>
42+
real(const Eigen::MatrixBase<ComplexMatrix> & complex_mat)
43+
{
44+
return complex_mat.real();
45+
}
46+
47+
template<typename ComplexMatrix>
48+
typename Eigen::Matrix<typename ComplexMatrix::RealScalar,ComplexMatrix::RowsAtCompileTime,ComplexMatrix::ColsAtCompileTime,ComplexMatrix::Options>
49+
imag(const Eigen::MatrixBase<ComplexMatrix> & complex_mat)
50+
{
51+
return complex_mat.imag();
52+
}
53+
54+
template<typename Scalar, int Rows, int Cols, int Options>
55+
Eigen::Matrix<std::complex<Scalar>,Rows,Cols,Options>
56+
ascomplex(const Eigen::Matrix<Scalar,Rows,Cols,Options> & mat)
57+
{
58+
typedef Eigen::Matrix<std::complex<Scalar>,Rows,Cols,Options> ReturnType;
59+
return ReturnType(mat);
60+
}
61+
62+
BOOST_PYTHON_MODULE(complex)
63+
{
64+
using namespace Eigen;
65+
namespace bp = boost::python;
66+
eigenpy::enableEigenPy();
67+
68+
bp::def("ascomplex", ascomplex<float,Eigen::Dynamic,Eigen::Dynamic,0>);
69+
bp::def("ascomplex", ascomplex<double,Eigen::Dynamic,Eigen::Dynamic,0>);
70+
bp::def("ascomplex", ascomplex<long double,Eigen::Dynamic,Eigen::Dynamic,0>);
71+
72+
bp::def("real", (MatrixXf (*)(const Eigen::MatrixBase<MatrixXcf> &))&real<MatrixXcf>);
73+
bp::def("real", (MatrixXd (*)(const Eigen::MatrixBase<MatrixXcd> &))&real<MatrixXcd>);
74+
bp::def("real", (MatrixXld (*)(const Eigen::MatrixBase<MatrixXcld> &))&real<MatrixXcld>);
75+
76+
bp::def("imag", (MatrixXf (*)(const Eigen::MatrixBase<MatrixXcf> &))&imag<MatrixXcf>);
77+
bp::def("imag", (MatrixXd (*)(const Eigen::MatrixBase<MatrixXcd> &))&imag<MatrixXcd>);
78+
bp::def("imag", (MatrixXld (*)(const Eigen::MatrixBase<MatrixXcld> &))&imag<MatrixXcld>);
79+
}

unittest/python/test_complex.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from __future__ import print_function
2+
3+
import numpy as np
4+
from complex import *
5+
6+
switchToNumpyArray()
7+
8+
rows = 10
9+
cols = 20
10+
11+
def test(dtype):
12+
Z = np.zeros((rows,cols),dtype=dtype)
13+
Z.real = np.random.rand(rows,cols)
14+
Z.imag = np.random.rand(rows,cols)
15+
16+
Z_real = real(Z)
17+
assert (Z_real == Z.real).all()
18+
Z_imag = imag(Z)
19+
assert (Z_imag == Z.imag).all()
20+
21+
Y = np.ones((rows,cols))
22+
Y_complex = ascomplex(Y)
23+
assert (Y_complex.real == Y).all()
24+
assert (Y_complex.imag == np.zeros((rows,cols))).all()
25+
26+
# Float
27+
test(np.csingle)
28+
# Double
29+
test(np.cdouble)
30+
# Long Double
31+
test(np.clongdouble)

0 commit comments

Comments
 (0)