xref: /aosp_15_r20/external/eigen/test/spqr_support.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2012 Desire Nuentsa Wakam <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li 
9*bf2c3715SXin Li #define EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
10*bf2c3715SXin Li #include "sparse.h"
11*bf2c3715SXin Li #include <Eigen/SPQRSupport>
12*bf2c3715SXin Li 
13*bf2c3715SXin Li 
14*bf2c3715SXin Li template<typename MatrixType,typename DenseMat>
generate_sparse_rectangular_problem(MatrixType & A,DenseMat & dA,int maxRows=300,int maxCols=300)15*bf2c3715SXin Li int generate_sparse_rectangular_problem(MatrixType& A, DenseMat& dA, int maxRows = 300, int maxCols = 300)
16*bf2c3715SXin Li {
17*bf2c3715SXin Li   eigen_assert(maxRows >= maxCols);
18*bf2c3715SXin Li   typedef typename MatrixType::Scalar Scalar;
19*bf2c3715SXin Li   int rows = internal::random<int>(1,maxRows);
20*bf2c3715SXin Li   int cols = internal::random<int>(1,rows);
21*bf2c3715SXin Li   double density = (std::max)(8./(rows*cols), 0.01);
22*bf2c3715SXin Li 
23*bf2c3715SXin Li   A.resize(rows,cols);
24*bf2c3715SXin Li   dA.resize(rows,cols);
25*bf2c3715SXin Li   initSparse<Scalar>(density, dA, A,ForceNonZeroDiag);
26*bf2c3715SXin Li   A.makeCompressed();
27*bf2c3715SXin Li   return rows;
28*bf2c3715SXin Li }
29*bf2c3715SXin Li 
test_spqr_scalar()30*bf2c3715SXin Li template<typename Scalar> void test_spqr_scalar()
31*bf2c3715SXin Li {
32*bf2c3715SXin Li   typedef SparseMatrix<Scalar,ColMajor> MatrixType;
33*bf2c3715SXin Li   MatrixType A;
34*bf2c3715SXin Li   Matrix<Scalar,Dynamic,Dynamic> dA;
35*bf2c3715SXin Li   typedef Matrix<Scalar,Dynamic,1> DenseVector;
36*bf2c3715SXin Li   DenseVector refX,x,b;
37*bf2c3715SXin Li   SPQR<MatrixType> solver;
38*bf2c3715SXin Li   generate_sparse_rectangular_problem(A,dA);
39*bf2c3715SXin Li 
40*bf2c3715SXin Li   Index m = A.rows();
41*bf2c3715SXin Li   b = DenseVector::Random(m);
42*bf2c3715SXin Li   solver.compute(A);
43*bf2c3715SXin Li   if (solver.info() != Success)
44*bf2c3715SXin Li   {
45*bf2c3715SXin Li     std::cerr << "sparse QR factorization failed\n";
46*bf2c3715SXin Li     exit(0);
47*bf2c3715SXin Li     return;
48*bf2c3715SXin Li   }
49*bf2c3715SXin Li   x = solver.solve(b);
50*bf2c3715SXin Li   if (solver.info() != Success)
51*bf2c3715SXin Li   {
52*bf2c3715SXin Li     std::cerr << "sparse QR factorization failed\n";
53*bf2c3715SXin Li     exit(0);
54*bf2c3715SXin Li     return;
55*bf2c3715SXin Li   }
56*bf2c3715SXin Li   //Compare with a dense solver
57*bf2c3715SXin Li   refX = dA.colPivHouseholderQr().solve(b);
58*bf2c3715SXin Li   VERIFY(x.isApprox(refX,test_precision<Scalar>()));
59*bf2c3715SXin Li }
EIGEN_DECLARE_TEST(spqr_support)60*bf2c3715SXin Li EIGEN_DECLARE_TEST(spqr_support)
61*bf2c3715SXin Li {
62*bf2c3715SXin Li   CALL_SUBTEST_1(test_spqr_scalar<double>());
63*bf2c3715SXin Li   CALL_SUBTEST_2(test_spqr_scalar<std::complex<double> >());
64*bf2c3715SXin Li }
65