You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 Desire Nuentsa Wakam <desire.nuentsa_wakam@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. #include "sparse.h"
  9. #include <Eigen/SparseQR>
  10. template<typename MatrixType,typename DenseMat>
  11. int generate_sparse_rectangular_problem(MatrixType& A, DenseMat& dA, int maxRows = 300, int maxCols = 300)
  12. {
  13. eigen_assert(maxRows >= maxCols);
  14. typedef typename MatrixType::Scalar Scalar;
  15. int rows = internal::random<int>(1,maxRows);
  16. int cols = internal::random<int>(1,rows);
  17. double density = (std::max)(8./(rows*cols), 0.01);
  18. A.resize(rows,rows);
  19. dA.resize(rows,rows);
  20. initSparse<Scalar>(density, dA, A,ForceNonZeroDiag);
  21. A.makeCompressed();
  22. int nop = internal::random<int>(0, internal::random<double>(0,1) > 0.5 ? cols/2 : 0);
  23. for(int k=0; k<nop; ++k)
  24. {
  25. int j0 = internal::random<int>(0,cols-1);
  26. int j1 = internal::random<int>(0,cols-1);
  27. Scalar s = internal::random<Scalar>();
  28. A.col(j0) = s * A.col(j1);
  29. dA.col(j0) = s * dA.col(j1);
  30. }
  31. return rows;
  32. }
  33. template<typename Scalar> void test_sparseqr_scalar()
  34. {
  35. typedef SparseMatrix<Scalar,ColMajor> MatrixType;
  36. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMat;
  37. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  38. MatrixType A;
  39. DenseMat dA;
  40. DenseVector refX,x,b;
  41. SparseQR<MatrixType, AMDOrdering<int> > solver;
  42. generate_sparse_rectangular_problem(A,dA);
  43. int n = A.cols();
  44. b = DenseVector::Random(n);
  45. solver.compute(A);
  46. if (solver.info() != Success)
  47. {
  48. std::cerr << "sparse QR factorization failed\n";
  49. exit(0);
  50. return;
  51. }
  52. x = solver.solve(b);
  53. if (solver.info() != Success)
  54. {
  55. std::cerr << "sparse QR factorization failed\n";
  56. exit(0);
  57. return;
  58. }
  59. //Compare with a dense QR solver
  60. ColPivHouseholderQR<DenseMat> dqr(dA);
  61. refX = dqr.solve(b);
  62. VERIFY_IS_EQUAL(dqr.rank(), solver.rank());
  63. if(solver.rank()<A.cols())
  64. VERIFY((dA * refX - b).norm() * 2 > (A * x - b).norm() );
  65. else
  66. VERIFY_IS_APPROX(x, refX);
  67. // Compute explicitly the matrix Q
  68. MatrixType Q, QtQ, idM;
  69. Q = solver.matrixQ();
  70. //Check ||Q' * Q - I ||
  71. QtQ = Q * Q.adjoint();
  72. idM.resize(Q.rows(), Q.rows()); idM.setIdentity();
  73. VERIFY(idM.isApprox(QtQ));
  74. }
  75. void test_sparseqr()
  76. {
  77. for(int i=0; i<g_repeat; ++i)
  78. {
  79. CALL_SUBTEST_1(test_sparseqr_scalar<double>());
  80. CALL_SUBTEST_2(test_sparseqr_scalar<std::complex<double> >());
  81. }
  82. }