| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  | // This file is part of Eigen, a lightweight C++ template library
 | 
					
						
							|  |  |  | // for linear algebra.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Copyright (C) 2012 Desire Nuentsa Wakam <desire.nuentsa_wakam@inria.fr>
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This Source Code Form is subject to the terms of the Mozilla
 | 
					
						
							|  |  |  | // Public License v. 2.0. If a copy of the MPL was not distributed
 | 
					
						
							| 
									
										
										
										
											2014-04-25 17:04:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
 | 
					
						
							| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  | #include "sparse.h"
 | 
					
						
							|  |  |  | #include <Eigen/SPQRSupport>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename MatrixType, typename DenseMat> | 
					
						
							|  |  |  | int generate_sparse_rectangular_problem(MatrixType& A, DenseMat& dA, int maxRows = 300, int maxCols = 300) { | 
					
						
							|  |  |  |   eigen_assert(maxRows >= maxCols); | 
					
						
							|  |  |  |   typedef typename MatrixType::Scalar Scalar; | 
					
						
							|  |  |  |   int rows = internal::random<int>(1, maxRows); | 
					
						
							| 
									
										
										
										
											2013-01-30 00:48:30 +08:00
										 |  |  |   int cols = internal::random<int>(1, rows); | 
					
						
							| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  |   double density = (std::max)(8. / (rows * cols), 0.01); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 04:39:33 +08:00
										 |  |  |   A.resize(rows, cols); | 
					
						
							|  |  |  |   dA.resize(rows, cols); | 
					
						
							| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  |   initSparse<Scalar>(density, dA, A, ForceNonZeroDiag); | 
					
						
							|  |  |  |   A.makeCompressed(); | 
					
						
							|  |  |  |   return rows; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename Scalar> | 
					
						
							|  |  |  | void test_spqr_scalar() { | 
					
						
							|  |  |  |   typedef SparseMatrix<Scalar, ColMajor> MatrixType; | 
					
						
							|  |  |  |   MatrixType A; | 
					
						
							|  |  |  |   Matrix<Scalar, Dynamic, Dynamic> dA; | 
					
						
							|  |  |  |   typedef Matrix<Scalar, Dynamic, 1> DenseVector; | 
					
						
							|  |  |  |   DenseVector refX, x, b; | 
					
						
							|  |  |  |   SPQR<MatrixType> solver; | 
					
						
							|  |  |  |   generate_sparse_rectangular_problem(A, dA); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-16 20:19:05 +08:00
										 |  |  |   Index m = A.rows(); | 
					
						
							| 
									
										
										
										
											2013-01-30 00:48:30 +08:00
										 |  |  |   b = DenseVector::Random(m); | 
					
						
							| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  |   solver.compute(A); | 
					
						
							|  |  |  |   if (solver.info() != Success) { | 
					
						
							|  |  |  |     std::cerr << "sparse QR factorization failed\n"; | 
					
						
							|  |  |  |     exit(0); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-11-22 02:47:44 +08:00
										 |  |  |   x = solver.solve(b); | 
					
						
							| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  |   if (solver.info() != Success) { | 
					
						
							|  |  |  |     std::cerr << "sparse QR factorization failed\n"; | 
					
						
							|  |  |  |     exit(0); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // Compare with a dense solver
 | 
					
						
							|  |  |  |   refX = dA.colPivHouseholderQr().solve(b); | 
					
						
							|  |  |  |   VERIFY(x.isApprox(refX, test_precision<Scalar>())); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-17 20:46:15 +08:00
										 |  |  | EIGEN_DECLARE_TEST(spqr_support) { | 
					
						
							| 
									
										
										
										
											2012-11-12 22:20:37 +08:00
										 |  |  |   CALL_SUBTEST_1(test_spqr_scalar<double>()); | 
					
						
							|  |  |  |   CALL_SUBTEST_2(test_spqr_scalar<std::complex<double> >()); | 
					
						
							| 
									
										
										
										
											2013-06-11 05:40:56 +08:00
										 |  |  | } |