46 #include <boost/random.hpp>
48 #include <Eigen/Dense>
54 template <
typename ScalarType>
58 typedef Eigen::Matrix<ScalarType, Eigen::Dynamic, 1> VectorType;
59 typedef Eigen::Matrix<
ScalarType, Eigen::Dynamic, Eigen::Dynamic,
60 Eigen::RowMajor> MatrixType;
62 RandSVD(
const MatrixType& A,
unsigned k) {
64 unsigned n = A.rows();
67 static boost::minstd_rand randgen(static_cast<unsigned>(time(0)));
68 static boost::normal_distribution<> dist(0, 1);
69 static boost::variate_generator<boost::minstd_rand, boost::normal_distribution<> > r(randgen, dist);
72 MatrixType Omega(n, k);
73 for (
unsigned i =0; i < n ; i++) {
74 for (
unsigned j = 0; j < k ; j++) {
80 MatrixType Y = A * A.transpose() * A * Omega;
81 Eigen::FullPivHouseholderQR<MatrixType> qr(Y);
82 MatrixType Q = qr.matrixQ().leftCols(k + k);
84 MatrixType B = Q.transpose() * A;
86 typedef Eigen::JacobiSVD<MatrixType> SVDType;
87 SVDType SVD(B, Eigen::ComputeThinU);
88 MatrixType Uhat = SVD.matrixU();
89 m_D = SVD.singularValues();
90 m_U = (Q * Uhat).leftCols(k);
93 MatrixType matrixU()
const {
97 VectorType singularValues()
const {
109 #endif // __LANCZOS_H
float ScalarType
the type that is used for all vector and matrices throughout the library.
Definition: CommonTypes.h:60