adjoint.cpp File Reference
#include "main.h"

Classes

struct  adjoint_specific< true >
 
struct  adjoint_specific< false >
 

Functions

template<typename MatrixType , typename Scalar = typename MatrixType::Scalar>
MatrixType RandomMatrix (Index rows, Index cols, Scalar min, Scalar max)
 
template<typename MatrixType >
void adjoint (const MatrixType &m)
 
template<int >
void adjoint_extra ()
 
 EIGEN_DECLARE_TEST (adjoint)
 

Function Documentation

◆ adjoint()

template<typename MatrixType >
void adjoint ( const MatrixType m)
85  {
86  /* this test covers the following files:
87  Transpose.h Conjugate.h Dot.h
88  */
89  using std::abs;
90  typedef typename MatrixType::Scalar Scalar;
91  typedef typename NumTraits<Scalar>::Real RealScalar;
95 
96  Index rows = m.rows();
97  Index cols = m.cols();
98 
99  // Avoid integer overflow by limiting input values.
101  RealScalar rmax = static_cast<RealScalar>(NumTraits<Scalar>::IsInteger ? 100 : 1);
102 
103  MatrixType m1 = RandomMatrix<MatrixType>(rows, cols, rmin, rmax),
104  m2 = RandomMatrix<MatrixType>(rows, cols, rmin, rmax), m3(rows, cols),
105  square = RandomMatrix<SquareMatrixType>(rows, rows, rmin, rmax);
106  VectorType v1 = RandomMatrix<VectorType>(rows, 1, rmin, rmax), v2 = RandomMatrix<VectorType>(rows, 1, rmin, rmax),
107  v3 = RandomMatrix<VectorType>(rows, 1, rmin, rmax), vzero = VectorType::Zero(rows);
108 
109  Scalar s1 = internal::random<Scalar>(rmin, rmax), s2 = internal::random<Scalar>(rmin, rmax);
110 
111  // check basic compatibility of adjoint, transpose, conjugate
112  VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
113  VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
114 
115  // check multiplicative behavior
116  VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
117  VERIFY_IS_APPROX((s1 * m1).adjoint(), numext::conj(s1) * m1.adjoint());
118 
119  // check basic properties of dot, squaredNorm
120  VERIFY_IS_APPROX(numext::conj(v1.dot(v2)), v2.dot(v1));
121  VERIFY_IS_APPROX(numext::real(v1.dot(v1)), v1.squaredNorm());
122 
124 
125  VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1));
126 
127  // like in testBasicStuff, test operator() to check const-qualification
128  Index r = internal::random<Index>(0, rows - 1), c = internal::random<Index>(0, cols - 1);
129  VERIFY_IS_APPROX(m1.conjugate()(r, c), numext::conj(m1(r, c)));
130  VERIFY_IS_APPROX(m1.adjoint()(c, r), numext::conj(m1(r, c)));
131 
132  // check inplace transpose
133  m3 = m1;
134  m3.transposeInPlace();
135  VERIFY_IS_APPROX(m3, m1.transpose());
136  m3.transposeInPlace();
137  VERIFY_IS_APPROX(m3, m1);
138 
139  if (PacketSize < m3.rows() && PacketSize < m3.cols()) {
140  m3 = m1;
141  Index i = internal::random<Index>(0, m3.rows() - PacketSize);
142  Index j = internal::random<Index>(0, m3.cols() - PacketSize);
143  m3.template block<PacketSize, PacketSize>(i, j).transposeInPlace();
144  VERIFY_IS_APPROX((m3.template block<PacketSize, PacketSize>(i, j)),
145  (m1.template block<PacketSize, PacketSize>(i, j).transpose()));
146  m3.template block<PacketSize, PacketSize>(i, j).transposeInPlace();
147  VERIFY_IS_APPROX(m3, m1);
148  }
149 
150  // check inplace adjoint
151  m3 = m1;
152  m3.adjointInPlace();
153  VERIFY_IS_APPROX(m3, m1.adjoint());
154  m3.transposeInPlace();
155  VERIFY_IS_APPROX(m3, m1.conjugate());
156 
157  // check mixed dot product
159  RealVectorType rv1 = RandomMatrix<RealVectorType>(rows, 1, rmin, rmax);
160 
161  VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
162  VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
163 
164  VERIFY(is_same_type(m1, m1.template conjugateIf<false>()));
165  VERIFY(is_same_type(m1.conjugate(), m1.template conjugateIf<true>()));
166 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:133
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:85
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
float real
Definition: datatypes.h:10
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY(a)
Definition: main.h:362
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
std::enable_if_t< internal::is_same< T1, T2 >::value, bool > is_same_type(const T1 &, const T2 &)
Definition: main.h:407
squared absolute sa ArrayBase::abs2 DOXCOMMA MatrixBase::cwiseAbs2 square(power 2)
double rmax
Definition: time_harmonic_fourier_decomposed_linear_elasticity/cylinder/cylinder.cc:70
double rmin
Definition: time_harmonic_fourier_decomposed_linear_elasticity/cylinder/cylinder.cc:68
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100
double Zero
Definition: pseudosolid_node_update_elements.cc:35
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: fft_test_shared.h:66
Definition: adjoint.cpp:13
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317

References abs(), calibrate::c, cols, conj(), i, Eigen::is_same_type(), j, m, m1, m2(), UniformPSDSelfTest::r, Global_Parameters::rmax, Global_Parameters::rmin, rows, run(), size, Eigen::square(), v1(), v2(), VERIFY, VERIFY_IS_APPROX, VERIFY_IS_MUCH_SMALLER_THAN, and oomph::PseudoSolidHelper::Zero.

Referenced by Eigen::CompleteOrthogonalDecomposition< MatrixType_, PermutationIndex_ >::_solve_impl(), Eigen::MatrixBase< Derived >::adjointInPlace(), Eigen::CompleteOrthogonalDecomposition< MatrixType_, PermutationIndex_ >::applyZAdjointOnTheLeftInPlace(), block(), Eigen::DGMRES< MatrixType_, Preconditioner_ >::dgmresCycle(), EIGEN_DECLARE_TEST(), Eigen::internal::gmres(), Eigen::internal::idrs(), Eigen::internal::idrstabl(), product_extra(), product_selfadjoint(), Eigen::internal::solve_assertion< CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< Scalar >, const Transpose< Derived > > >::run(), symm(), and trmm().

◆ adjoint_extra()

template<int >
void adjoint_extra ( )
169  {
170  MatrixXcf a(10, 10), b(10, 10);
171  VERIFY_RAISES_ASSERT(a = a.transpose());
172  VERIFY_RAISES_ASSERT(a = a.transpose() + b);
173  VERIFY_RAISES_ASSERT(a = b + a.transpose());
174  VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
175  VERIFY_RAISES_ASSERT(a = a.adjoint());
176  VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
177  VERIFY_RAISES_ASSERT(a = b + a.adjoint());
178 
179  // no assertion should be triggered for these cases:
180  a.transpose() = a.transpose();
181  a.transpose() += a.transpose();
182  a.transpose() += a.transpose() + b;
183  a.transpose() = a.adjoint();
184  a.transpose() += a.adjoint();
185  a.transpose() += a.adjoint() + b;
186 
187  // regression tests for check_for_aliasing
188  MatrixXd c(10, 10);
189  c = 1.0 * MatrixXd::Ones(10, 10) + c;
190  c = MatrixXd::Ones(10, 10) * 1.0 + c;
191  c = c + MatrixXd::Ones(10, 10).cwiseProduct(MatrixXd::Zero(10, 10));
192  c = MatrixXd::Ones(10, 10) * MatrixXd::Zero(10, 10);
193 
194  // regression for bug 1646
195  for (int j = 0; j < 10; ++j) {
196  c.col(j).head(j) = c.row(j).head(j);
197  }
198 
199  for (int j = 0; j < 10; ++j) {
200  c.col(j) = c.row(j);
201  }
202 
203  a.conservativeResize(1, 1);
204  a = a.transpose();
205 
206  a.conservativeResize(0, 0);
207  a = a.transpose();
208 }
Scalar * b
Definition: benchVecAdd.cpp:17
const Scalar * a
Definition: level2_cplx_impl.h:32
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329

References a, b, calibrate::c, j, VERIFY_RAISES_ASSERT, and oomph::PseudoSolidHelper::Zero.

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( adjoint  )
210  {
211  for (int i = 0; i < g_repeat; i++) {
213  CALL_SUBTEST_2(adjoint(Matrix3d()));
214  CALL_SUBTEST_3(adjoint(Matrix4f()));
215 
216  CALL_SUBTEST_4(adjoint(MatrixXcf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE / 2),
217  internal::random<int>(1, EIGEN_TEST_MAX_SIZE / 2))));
219  MatrixXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
221  MatrixXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
222 
223  // Complement for 128 bits vectorization:
224  CALL_SUBTEST_8(adjoint(Matrix2d()));
226 
227  // 256 bits vectorization:
231  }
232  // test a large static matrix only once
234 
235  CALL_SUBTEST_13(adjoint_extra<0>());
236 }
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
static int g_repeat
Definition: main.h:191
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_13(FUNC)
Definition: split_test_helper.h:76
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58

References adjoint(), CALL_SUBTEST_1, CALL_SUBTEST_10, CALL_SUBTEST_11, CALL_SUBTEST_12, CALL_SUBTEST_13, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, CALL_SUBTEST_9, EIGEN_TEST_MAX_SIZE, Eigen::g_repeat, and i.

◆ RandomMatrix()

template<typename MatrixType , typename Scalar = typename MatrixType::Scalar>
MatrixType RandomMatrix ( Index  rows,
Index  cols,
Scalar  min,
Scalar  max 
)
74  {
76  for (Index i = 0; i < rows; ++i) {
77  for (Index j = 0; j < cols; ++j) {
78  M(i, j) = Eigen::internal::random<Scalar>(min, max);
79  }
80  }
81  return M;
82 }
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:50
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23

References cols, i, j, max, min, and rows.