|
| 1 | +// |
| 2 | +// Created by Marco Galliani on 30/10/24. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef REVD_H |
| 6 | +#define REVD_H |
| 7 | + |
| 8 | +#include <utility> |
| 9 | +#include <memory> |
| 10 | +#include <tuple> |
| 11 | +#include <limits> |
| 12 | +#include <type_traits> |
| 13 | + |
| 14 | +namespace fdapde{ |
| 15 | +namespace core{ |
| 16 | +//Interface for the Approximation strategy |
| 17 | +template<typename MatrixType> |
| 18 | +class REVDStrategy{ |
| 19 | +protected: |
| 20 | + unsigned int seed_=fdapde::random_seed; |
| 21 | + double tol_=1e-3; |
| 22 | + //storage of the decomposition |
| 23 | + DMatrix<double> U_; |
| 24 | + DVector<double> Lambda_; |
| 25 | +public: |
| 26 | + REVDStrategy()=default; |
| 27 | + REVDStrategy(unsigned int seed, double tol) : seed_(seed), tol_(tol){} |
| 28 | + virtual void compute(const MatrixType &A, int rank, int max_iter) = 0; |
| 29 | + //setters |
| 30 | + void setTol(double tol){ tol_=tol;} |
| 31 | + void setSeed(unsigned int seed){ seed_=seed;} |
| 32 | + //getters |
| 33 | + int rank() const{ return Lambda_.size();} |
| 34 | + DMatrix<double> matrixU() const{ return U_;} |
| 35 | + DVector<double> eigenValues() const{ return Lambda_;} |
| 36 | + //destructor |
| 37 | + virtual ~REVDStrategy() = default; |
| 38 | +}; |
| 39 | + |
| 40 | +template<typename MatrixType> |
| 41 | +class NysRSI : public REVDStrategy<MatrixType>{ |
| 42 | +public: |
| 43 | + NysRSI()=default; |
| 44 | + NysRSI(unsigned int seed, double tol) : REVDStrategy<MatrixType>(seed,tol){} |
| 45 | + void compute(const MatrixType &A, int rank, int max_iter) override{ |
| 46 | + //params init |
| 47 | + int max_rank = A.rows(); //equal to A.cols() |
| 48 | + int block_sz = std::min(2*rank,max_rank); //default setting |
| 49 | + max_iter = std::min(max_iter, max_rank); |
| 50 | + double shift = A.trace()*std::numeric_limits<double>::epsilon(); |
| 51 | + //factor init |
| 52 | + DMatrix<double> Y = fdapde::internals::GaussianMatrix(A.rows(), block_sz, this->seed_); |
| 53 | + DMatrix<double> X; |
| 54 | + DMatrix<double> F; |
| 55 | + Eigen::HouseholderQR<DMatrix<double>> qr; |
| 56 | + //error |
| 57 | + Eigen::JacobiSVD<DMatrix<double>> svd; |
| 58 | + DMatrix<double> E; |
| 59 | + double norm_A = A.norm(), res_err = norm_A; |
| 60 | + //iterations |
| 61 | + for(int i=0; res_err > this->tol_*norm_A && i<max_iter; ++i) { |
| 62 | + qr.compute(Y); |
| 63 | + X = qr.householderQ() * DMatrix<double>::Identity(A.rows(),block_sz); |
| 64 | + Y = A*X; |
| 65 | + //construct the factor |
| 66 | + Y += shift*DMatrix<double>::Identity(Y.rows(),Y.cols()); |
| 67 | + Eigen::LLT<DMatrix<double>> chol(X.transpose()*Y); |
| 68 | + F = chol.matrixU().solve<Eigen::OnTheRight>(Y); |
| 69 | + //update the error |
| 70 | + svd.compute(F,Eigen::ComputeThinU | Eigen::ComputeThinV); |
| 71 | + E = A*svd.matrixU().leftCols(rank) - svd.matrixU().leftCols(rank)*(svd.singularValues().head(rank).array().pow(2)-shift).matrix().asDiagonal(); |
| 72 | + res_err = E.colwise().template lpNorm<2>().maxCoeff(); |
| 73 | + } |
| 74 | + this->U_ = svd.matrixU().leftCols(rank); |
| 75 | + this->Lambda_ = (svd.singularValues().head(rank).array().pow(2)-shift).matrix(); |
| 76 | + return; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +template<typename MatrixType> |
| 81 | +class NysRBKI : public REVDStrategy<MatrixType>{ |
| 82 | +public: |
| 83 | + NysRBKI()=default; |
| 84 | + NysRBKI(unsigned int seed, double tol) : REVDStrategy<MatrixType>(seed,tol){} |
| 85 | + void compute(const MatrixType &A, int rank, int max_iter) override{ |
| 86 | + //params init |
| 87 | + int max_rank = A.rows(); //equal to A.cols() |
| 88 | + int block_sz; //default setting |
| 89 | + if(A.rows()<1000){ |
| 90 | + block_sz = 1; |
| 91 | + }else{ |
| 92 | + block_sz = 10; |
| 93 | + } |
| 94 | + max_iter = std::min(max_iter,max_rank/block_sz-1); |
| 95 | + double shift = A.trace()*std::numeric_limits<double>::epsilon(); |
| 96 | + //factor init |
| 97 | + DMatrix<double> X,Y,S,F; |
| 98 | + X.resize(A.rows(),max_rank); Y.resize(A.rows(),max_rank); |
| 99 | + S = DMatrix<double>::Zero(max_rank,max_rank); |
| 100 | + Eigen::HouseholderQR<DMatrix<double>> qr(fdapde::internals::GaussianMatrix(A.rows(),block_sz,this->seed_)); |
| 101 | + X.leftCols(block_sz) = qr.householderQ()*DMatrix<double>::Identity(A.rows(),block_sz); |
| 102 | + Y.leftCols(block_sz) = A*X.leftCols(block_sz); |
| 103 | + //error |
| 104 | + Eigen::JacobiSVD<DMatrix<double>> svd; |
| 105 | + DMatrix<double> E; |
| 106 | + double norm_A=A.norm(), res_err=norm_A; |
| 107 | + //iterations |
| 108 | + int n_cols_X = block_sz; |
| 109 | + for(int i=0; i<max_iter && res_err>this->tol_*norm_A;i++,n_cols_X+=block_sz){ |
| 110 | + X.middleCols((i+1)*block_sz,block_sz) = Y.middleCols(i*block_sz,block_sz) + shift*X.middleCols(i*block_sz,block_sz); |
| 111 | + //blocked column |
| 112 | + DMatrix<double> new_col = DMatrix<double>::Zero(X.rows(),(i+1)*block_sz); |
| 113 | + new_col.middleCols(std::max(i-1,0)*block_sz,block_sz) = X.middleCols(std::max(i-1,0)*block_sz,block_sz); |
| 114 | + new_col.middleCols(i*block_sz,block_sz) = X.middleCols(i*block_sz,block_sz); |
| 115 | + new_col = new_col.transpose()*X.middleCols((i+1)*block_sz,block_sz); |
| 116 | + //orthogonalisation |
| 117 | + auto new_block_qr = fdapde::internals::BCGS_plus(X.leftCols((i+1)*block_sz),X.middleCols((i+1)*block_sz,block_sz)); |
| 118 | + X.middleCols((i+1)*block_sz,block_sz) = new_block_qr.first; |
| 119 | + //cholesky |
| 120 | + S.block(0,i*block_sz,(i+1)*block_sz,block_sz) = new_col; |
| 121 | + Eigen::LLT<DMatrix<double>> chol(S.block(0,0,(i+1)*block_sz,(i+1)*block_sz)); |
| 122 | + S.block((i+1)*block_sz,i*block_sz,block_sz,block_sz) = new_block_qr.second; |
| 123 | + F = chol.matrixU().solve<Eigen::OnTheRight>(S.block(0,0,(i+2)*block_sz,(i+1)*block_sz)); |
| 124 | + //update Y |
| 125 | + Y.middleCols((i+1)*block_sz,block_sz) = A*X.middleCols((i+1)*block_sz,block_sz); |
| 126 | + //update the error |
| 127 | + svd.compute(F, Eigen::ComputeThinU | Eigen::ComputeThinV); |
| 128 | + E = Y.leftCols((i+2)*block_sz)*svd.matrixU().leftCols(std::min(rank,(i+1)*block_sz)) - X.leftCols((i+2)*block_sz)*svd.matrixU().leftCols(std::min(rank,(i+1)*block_sz))*(svd.singularValues().head(std::min(rank,(i+1)*block_sz)).array().pow(2)-shift).matrix().asDiagonal(); |
| 129 | + res_err = E.colwise().template lpNorm<2>().maxCoeff(); |
| 130 | + } |
| 131 | + rank = std::min((int)svd.singularValues().size(), rank); |
| 132 | + this->U_ = X.leftCols(n_cols_X)*svd.matrixU().leftCols(rank); |
| 133 | + this->Lambda_ = (svd.singularValues().head(rank).array().pow(2)-shift).matrix(); |
| 134 | + return; |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +template<typename MatrixType> |
| 139 | +class REVD{ |
| 140 | +private: |
| 141 | + std::unique_ptr<REVDStrategy<MatrixType>> revd_strategy_; |
| 142 | + DMatrix<double> U_; |
| 143 | + DVector<double> Lambda_; |
| 144 | +public: |
| 145 | + explicit REVD(std::unique_ptr<REVDStrategy<MatrixType>> &&strategy=std::make_unique<NysRSI<DMatrix<double>>>()): revd_strategy_(std::move(strategy)){} |
| 146 | + void compute(const MatrixType &A, int tr_rank, int max_iter=1e3){ |
| 147 | + revd_strategy_->compute(A,tr_rank,max_iter); |
| 148 | + return; |
| 149 | + } |
| 150 | + //setters |
| 151 | + void setTol(double tol){ revd_strategy_->setTol(tol);} |
| 152 | + void setSeed(unsigned int seed){ revd_strategy_->setSeed(seed);} |
| 153 | + //getters |
| 154 | + int rank() const{ return revd_strategy_->rank();} |
| 155 | + DMatrix<double> matrixU() const{ return revd_strategy_->matrixU();} |
| 156 | + DVector<double> eigenValues() const{ return revd_strategy_->eigenValues();} |
| 157 | +}; |
| 158 | + |
| 159 | +}//core |
| 160 | +}//fdpade |
| 161 | + |
| 162 | +#endif //REVD_H |
0 commit comments