lu.cpp File Reference
#include "main.h"
#include <Eigen/LU>
#include "solverbase.h"

Functions

template<typename MatrixType >
MatrixType::RealScalar matrix_l1_norm (const MatrixType &m)
 
template<typename MatrixType >
void lu_non_invertible ()
 
template<typename MatrixType >
void lu_invertible ()
 
template<typename MatrixType >
void lu_partial_piv (Index size=MatrixType::ColsAtCompileTime)
 
template<typename MatrixType >
void lu_verify_assert ()
 
 EIGEN_DECLARE_TEST (lu)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( lu  )
198  {
199  for (int i = 0; i < g_repeat; i++) {
200  CALL_SUBTEST_1(lu_non_invertible<Matrix3f>());
201  CALL_SUBTEST_1(lu_invertible<Matrix3f>());
202  CALL_SUBTEST_1(lu_verify_assert<Matrix3f>());
203  CALL_SUBTEST_1(lu_partial_piv<Matrix3f>());
204 
207  CALL_SUBTEST_2(lu_partial_piv<Matrix2d>());
208  CALL_SUBTEST_2(lu_partial_piv<Matrix4d>());
210 
211  CALL_SUBTEST_3(lu_non_invertible<MatrixXf>());
212  CALL_SUBTEST_3(lu_invertible<MatrixXf>());
213  CALL_SUBTEST_3(lu_verify_assert<MatrixXf>());
214 
215  CALL_SUBTEST_4(lu_non_invertible<MatrixXd>());
216  CALL_SUBTEST_4(lu_invertible<MatrixXd>());
217  CALL_SUBTEST_4(lu_partial_piv<MatrixXd>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE)));
218  CALL_SUBTEST_4(lu_verify_assert<MatrixXd>());
219 
220  CALL_SUBTEST_5(lu_non_invertible<MatrixXcf>());
221  CALL_SUBTEST_5(lu_invertible<MatrixXcf>());
222  CALL_SUBTEST_5(lu_verify_assert<MatrixXcf>());
223 
224  CALL_SUBTEST_6(lu_non_invertible<MatrixXcd>());
225  CALL_SUBTEST_6(lu_invertible<MatrixXcd>());
226  CALL_SUBTEST_6(lu_partial_piv<MatrixXcd>(internal::random<int>(1, EIGEN_TEST_MAX_SIZE)));
227  CALL_SUBTEST_6(lu_verify_assert<MatrixXcd>());
228 
230 
231  // Test problem size constructors
234  }
235 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
LU decomposition of a matrix with complete pivoting, and related features.
Definition: FullPivLU.h:63
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
LU decomposition of a matrix with partial pivoting, and related features.
Definition: PartialPivLU.h:77
void lu_verify_assert()
Definition: lu.cpp:168
void lu_partial_piv(Index size=MatrixType::ColsAtCompileTime)
Definition: lu.cpp:142
void lu_non_invertible()
Definition: lu.cpp:21
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_5(FUNC)
Definition: split_test_helper.h:28
#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

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_9, EIGEN_TEST_MAX_SIZE, Eigen::g_repeat, i, lu_non_invertible(), lu_partial_piv(), and lu_verify_assert().

◆ lu_invertible()

template<typename MatrixType >
void lu_invertible ( )
98  {
99  /* this test covers the following files:
100  FullPivLU.h
101  */
103  Index size = MatrixType::RowsAtCompileTime;
104  if (size == Dynamic) size = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE);
105 
106  MatrixType m1(size, size), m2(size, size), m3(size, size);
108  lu.setThreshold(RealScalar(0.01));
109  do {
110  m1 = MatrixType::Random(size, size);
111  lu.compute(m1);
112  } while (!lu.isInvertible());
113 
114  VERIFY_IS_APPROX(m1, lu.reconstructedMatrix());
115  VERIFY(0 == lu.dimensionOfKernel());
116  VERIFY(lu.kernel().cols() == 1); // the kernel() should consist of a single (zero) column vector
117  VERIFY(size == lu.rank());
118  VERIFY(lu.isInjective());
119  VERIFY(lu.isSurjective());
120  VERIFY(lu.isInvertible());
121  VERIFY(lu.image(m1).fullPivLu().isInvertible());
122 
123  check_solverbase<MatrixType, MatrixType>(m1, lu, size, size, size);
124 
125  MatrixType m1_inverse = lu.inverse();
126  m3 = MatrixType::Random(size, size);
127  m2 = lu.solve(m3);
128  VERIFY_IS_APPROX(m2, m1_inverse * m3);
129 
130  RealScalar rcond = (RealScalar(1) / matrix_l1_norm(m1)) / matrix_l1_norm(m1_inverse);
131  const RealScalar rcond_est = lu.rcond();
132  // Verify that the estimated condition number is within a factor of 10 of the
133  // truth.
134  VERIFY(rcond_est > rcond / 10 && rcond_est < rcond * 10);
135 
136  // Regression test for Bug 302
137  MatrixType m4 = MatrixType::Random(size, size);
138  VERIFY_IS_APPROX(lu.solve(m3 * m4), lu.solve(m3) * m4);
139 }
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
cout<< "Here is the matrix m:"<< endl<< m<< endl;Eigen::FullPivLU< Matrix5x3 > lu(m)
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
MatrixType::RealScalar matrix_l1_norm(const MatrixType &m)
Definition: lu.cpp:16
#define VERIFY(a)
Definition: main.h:362
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References Eigen::Dynamic, EIGEN_TEST_MAX_SIZE, lu(), m1, m2(), matrix_l1_norm(), size, VERIFY, and VERIFY_IS_APPROX.

◆ lu_non_invertible()

template<typename MatrixType >
void lu_non_invertible ( )
21  {
22  typedef typename MatrixType::RealScalar RealScalar;
23  /* this test covers the following files:
24  LU.h
25  */
26  Index rows, cols, cols2;
27  if (MatrixType::RowsAtCompileTime == Dynamic) {
28  rows = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
29  } else {
30  rows = MatrixType::RowsAtCompileTime;
31  }
32  if (MatrixType::ColsAtCompileTime == Dynamic) {
33  cols = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
34  cols2 = internal::random<int>(2, EIGEN_TEST_MAX_SIZE);
35  } else {
36  cols2 = cols = MatrixType::ColsAtCompileTime;
37  }
38 
39  enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime };
40  typedef typename internal::kernel_retval_base<FullPivLU<MatrixType> >::ReturnType KernelMatrixType;
41  typedef typename internal::image_retval_base<FullPivLU<MatrixType> >::ReturnType ImageMatrixType;
44 
45  Index rank = internal::random<Index>(1, (std::min)(rows, cols) - 1);
46 
47  // The image of the zero matrix should consist of a single (zero) column vector
48  VERIFY((MatrixType::Zero(rows, cols).fullPivLu().image(MatrixType::Zero(rows, cols)).cols() == 1));
49 
50  // The kernel of the zero matrix is the entire space, and thus is an invertible matrix of dimensions cols.
51  KernelMatrixType kernel = MatrixType::Zero(rows, cols).fullPivLu().kernel();
52  VERIFY((kernel.fullPivLu().isInvertible()));
53 
54  MatrixType m1(rows, cols), m3(rows, cols2);
55  CMatrixType m2(cols, cols2);
57 
59 
60  // The special value 0.01 below works well in tests. Keep in mind that we're only computing the rank
61  // of singular values are either 0 or 1.
62  // So it's not clear at all that the epsilon should play any role there.
63  lu.setThreshold(RealScalar(0.01));
64  lu.compute(m1);
65 
66  MatrixType u(rows, cols);
67  u = lu.matrixLU().template triangularView<Upper>();
68  RMatrixType l = RMatrixType::Identity(rows, rows);
69  l.block(0, 0, rows, (std::min)(rows, cols)).template triangularView<StrictlyLower>() =
70  lu.matrixLU().block(0, 0, rows, (std::min)(rows, cols));
71 
72  VERIFY_IS_APPROX(lu.permutationP() * m1 * lu.permutationQ(), l * u);
73 
74  KernelMatrixType m1kernel = lu.kernel();
75  ImageMatrixType m1image = lu.image(m1);
76 
77  VERIFY_IS_APPROX(m1, lu.reconstructedMatrix());
78  VERIFY(rank == lu.rank());
79  VERIFY(cols - lu.rank() == lu.dimensionOfKernel());
80  VERIFY(!lu.isInjective());
81  VERIFY(!lu.isInvertible());
82  VERIFY(!lu.isSurjective());
83  VERIFY_IS_MUCH_SMALLER_THAN((m1 * m1kernel), m1);
84  VERIFY(m1image.fullPivLu().rank() == rank);
85  VERIFY_IS_APPROX(m1 * m1.adjoint() * m1image, m1image);
86 
87  check_solverbase<CMatrixType, MatrixType>(m1, lu, rows, cols, cols2);
88 
89  m2 = CMatrixType::Random(cols, cols2);
90  m3 = m1 * m2;
91  m2 = CMatrixType::Random(cols, cols2);
92  // test that the code, which does resize(), may be applied to an xpr
93  m2.block(0, 0, m2.rows(), m2.cols()) = lu.solve(m3);
94  VERIFY_IS_APPROX(m3, m1 * m2);
95 }
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
#define min(a, b)
Definition: datatypes.h:22
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: random_matrix_helper.h:60
double Zero
Definition: pseudosolid_node_update_elements.cc:35

References cols, Eigen::createRandomPIMatrixOfRank(), Eigen::Dynamic, EIGEN_TEST_MAX_SIZE, lu(), m1, m2(), min, rows, VERIFY, VERIFY_IS_APPROX, VERIFY_IS_MUCH_SMALLER_THAN, and oomph::PseudoSolidHelper::Zero.

Referenced by EIGEN_DECLARE_TEST().

◆ lu_partial_piv()

template<typename MatrixType >
void lu_partial_piv ( Index  size = MatrixType::ColsAtCompileTime)
142  {
143  /* this test covers the following files:
144  PartialPivLU.h
145  */
147 
148  MatrixType m1(size, size), m2(size, size), m3(size, size);
149  m1.setRandom();
151 
152  VERIFY_IS_APPROX(m1, plu.reconstructedMatrix());
153 
154  check_solverbase<MatrixType, MatrixType>(m1, plu, size, size, size);
155 
156  MatrixType m1_inverse = plu.inverse();
157  m3 = MatrixType::Random(size, size);
158  m2 = plu.solve(m3);
159  VERIFY_IS_APPROX(m2, m1_inverse * m3);
160 
161  RealScalar rcond = (RealScalar(1) / matrix_l1_norm(m1)) / matrix_l1_norm(m1_inverse);
162  const RealScalar rcond_est = plu.rcond();
163  // Verify that the estimate is within a factor of 10 of the truth.
164  VERIFY(rcond_est > rcond / 10 && rcond_est < rcond * 10);
165 }

References Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::inverse(), m1, m2(), matrix_l1_norm(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::rcond(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::reconstructedMatrix(), size, Eigen::SolverBase< Derived >::solve(), VERIFY, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ lu_verify_assert()

template<typename MatrixType >
void lu_verify_assert ( )
168  {
169  MatrixType tmp;
170 
172  VERIFY_RAISES_ASSERT(lu.matrixLU())
173  VERIFY_RAISES_ASSERT(lu.permutationP())
174  VERIFY_RAISES_ASSERT(lu.permutationQ())
175  VERIFY_RAISES_ASSERT(lu.kernel())
176  VERIFY_RAISES_ASSERT(lu.image(tmp))
181  VERIFY_RAISES_ASSERT(lu.rank())
182  VERIFY_RAISES_ASSERT(lu.dimensionOfKernel())
183  VERIFY_RAISES_ASSERT(lu.isInjective())
184  VERIFY_RAISES_ASSERT(lu.isSurjective())
185  VERIFY_RAISES_ASSERT(lu.isInvertible())
187 
189  VERIFY_RAISES_ASSERT(plu.matrixLU())
190  VERIFY_RAISES_ASSERT(plu.permutationP())
196 }
void adjoint(const MatrixType &m)
Definition: adjoint.cpp:85
void determinant(const MatrixType &m)
Definition: determinant.cpp:15
void inverse(const MatrixType &m)
Definition: inverse.cpp:64
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
void transpose()
Definition: skew_symmetric_matrix3.cpp:135
Update the problem specs before solve
Definition: steady_axisym_advection_diffusion.cc:353

References Eigen::SolverBase< Derived >::adjoint(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::determinant(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::inverse(), lu(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::matrixLU(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::permutationP(), Eigen::SolverBase< Derived >::solve(), tmp, Eigen::SolverBase< Derived >::transpose(), and VERIFY_RAISES_ASSERT.

Referenced by EIGEN_DECLARE_TEST().

◆ matrix_l1_norm()

template<typename MatrixType >
MatrixType::RealScalar matrix_l1_norm ( const MatrixType m)
16  {
17  return m.cwiseAbs().colwise().sum().maxCoeff();
18 }
int * m
Definition: level2_cplx_impl.h:294

References m.

Referenced by lu_invertible(), and lu_partial_piv().