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.

62 lines
1.7 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/SPQRSupport>
  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. return rows;
  23. }
  24. template<typename Scalar> void test_spqr_scalar()
  25. {
  26. typedef SparseMatrix<Scalar,ColMajor> MatrixType;
  27. MatrixType A;
  28. Matrix<Scalar,Dynamic,Dynamic> dA;
  29. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  30. DenseVector refX,x,b;
  31. SPQR<MatrixType> solver;
  32. generate_sparse_rectangular_problem(A,dA);
  33. int m = A.rows();
  34. b = DenseVector::Random(m);
  35. solver.compute(A);
  36. if (solver.info() != Success)
  37. {
  38. std::cerr << "sparse QR factorization failed\n";
  39. exit(0);
  40. return;
  41. }
  42. x = solver.solve(b);
  43. if (solver.info() != Success)
  44. {
  45. std::cerr << "sparse QR factorization failed\n";
  46. exit(0);
  47. return;
  48. }
  49. //Compare with a dense solver
  50. refX = dA.colPivHouseholderQr().solve(b);
  51. VERIFY(x.isApprox(refX,test_precision<Scalar>()));
  52. }
  53. void test_spqr_support()
  54. {
  55. CALL_SUBTEST_1(test_spqr_scalar<double>());
  56. CALL_SUBTEST_2(test_spqr_scalar<std::complex<double> >());
  57. }