mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add support for Sparse QR factorization
This commit is contained in:
@@ -86,6 +86,19 @@ else()
|
||||
ei_add_property(EIGEN_MISSING_BACKENDS "PaStiX, ")
|
||||
endif()
|
||||
|
||||
find_package(SPQR)
|
||||
if(SPQR_FOUND AND BLAS_FOUND AND LAPACK_FOUND)
|
||||
if(CHOLMOD_FOUND)
|
||||
add_definitions("-DEIGEN_SPQR_SUPPORT")
|
||||
include_directories(${SPQR_INCLUDES})
|
||||
set(SPQR_ALL_LIBS ${SPQR_LIBRARIES} ${CHOLMOD_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
|
||||
set(SPARSE_LIBS ${SPARSE_LIBS} ${SPQR_ALL_LIBS})
|
||||
ei_add_property(EIGEN_TESTED_BACKENDS "SPQR, ")
|
||||
else(CHOLMOD_FOUND)
|
||||
ei_add_property(EIGEN_MISSING_BACKENDS "SPQR, ")
|
||||
endif(CHOLMOD_FOUND)
|
||||
endif()
|
||||
|
||||
option(EIGEN_TEST_NOQT "Disable Qt support in unit tests" OFF)
|
||||
if(NOT EIGEN_TEST_NOQT)
|
||||
find_package(Qt4)
|
||||
@@ -227,6 +240,10 @@ if(PASTIX_FOUND AND (SCOTCH_FOUND OR METIS_FOUND))
|
||||
ei_add_test(pastix_support "" "${PASTIX_ALL_LIBS}")
|
||||
endif()
|
||||
|
||||
if(SPQR_FOUND AND CHOLMOD_FOUND)
|
||||
ei_add_test(spqr_support "" "${SPQR_ALL_LIBS}")
|
||||
endif()
|
||||
|
||||
string(TOLOWER "${CMAKE_CXX_COMPILER}" cmake_cxx_compiler_tolower)
|
||||
if(cmake_cxx_compiler_tolower MATCHES "qcc")
|
||||
set(CXX_IS_QCC "ON")
|
||||
|
||||
62
test/spqr_support.cpp
Normal file
62
test/spqr_support.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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
|
||||
#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);
|
||||
int cols = internal::random<int>(1,maxCols);
|
||||
double density = (std::max)(8./(rows*cols), 0.01);
|
||||
|
||||
A.resize(rows,rows);
|
||||
dA.resize(rows,rows);
|
||||
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);
|
||||
|
||||
int n = A.cols();
|
||||
b = DenseVector::Random(n);
|
||||
solver.compute(A);
|
||||
if (solver.info() != Success)
|
||||
{
|
||||
std::cerr << "sparse QR factorization failed\n";
|
||||
exit(0);
|
||||
return;
|
||||
}
|
||||
solver._solve(b,x);
|
||||
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>()));
|
||||
}
|
||||
void test_spqr_support()
|
||||
{
|
||||
CALL_SUBTEST_1(test_spqr_scalar<double>());
|
||||
CALL_SUBTEST_2(test_spqr_scalar<std::complex<double> >());
|
||||
}
|
||||
Reference in New Issue
Block a user