<commit_before>#ifndef SVD_HPP
#define SVD_HPP

#include "lrmat.hpp"
#include <cassert>
#include <Eigen/Dense>

namespace htool{

template< typename T >
class SVD: public LowRankMatrix<T>{

private:
  // Eigen
  static const int Dynamic = Eigen::Dynamic;
  typedef Eigen::Matrix<T, Dynamic, Dynamic>     DenseMatrix;
  typedef Eigen::JacobiSVD<DenseMatrix>          SVDType;
  typedef typename SVDType::SingularValuesType   SgValType;
  typedef typename SVDType::MatrixUType		       UMatrixType;
  typedef typename SVDType::MatrixVType		       VMatrixType;

  // Data member
  std::vector<T> singular_values;

  // No assignement or copy
  SVD(const SVD& copy_from);
  SVD & operator=(const SVD& copy_from);

public:
  SVD(const std::vector<int>& ir0, const std::vector<int>& ic0, const Cluster& t0, const Cluster& s0, int rank0=0): LowRankMatrix<T>(ir0,ic0,t0,s0,rank0){}

  void build(const IMatrix<T>& A){
    //// Matrix assembling
    DenseMatrix M(this->ir.size(),this->ic.size());
    for (int i=0; i<M.rows(); i++){
  		for (int j=0; j<M.cols(); j++){
  			M(i,j) = A.get_coef(this->ir[i], this->ic[j]);
      }
    }

    //// SVD
    SVDType svd(M,Eigen::ComputeThinU | Eigen::ComputeThinV );

    const SgValType& sv = svd.singularValues();
    singular_values.resize(sv.size());
    for(int j=0; j<sv.size(); j++){singular_values[j]=sv[j];}
    const UMatrixType& uu = svd.matrixU();
    const VMatrixType& vv = svd.matrixV();

    this->U.resize(M.rows(),sv.size());
    this->V.resize(sv.size(),M.cols());

    for (int i=0;i<M.rows();i++){
      for (int j=0;j<sv.size();j++){
        this->U(i,j)=uu(i,j)*sv[j];
      }
    }
    for (int i=0;i<sv.size();i++){
      for (int j=0;j<M.cols();j++){
          this->V(i,j)=vv(j,i);
        }
    }
  }

  T get_singular_value(int i){return singular_values[i];}
  //
  // double NormFrob(){
  //   T frob = 0.;
  //
  //   for (int j=0;j<m.nr;j++){
  //     for (int k=0;k<m.nc;k++){
  //       T aux=0;
  //       for (int l=0;l<m.rank;l++){
  //         T += m.U(j,l) * m.V(l,k);
  //       }
  //     frob+=pow(abs(aux),2);
  //     }
  //   }
  // }
};

}<commit_msg>SVD updated<commit_after>#ifndef SVD_HPP
#define SVD_HPP

#include "lrmat.hpp"
#include <cassert>
#include <Eigen/Dense>

namespace htool{

template< typename T >
class SVD: public LowRankMatrix<T>{

private:
  // Eigen
  static const int Dynamic = Eigen::Dynamic;
  typedef Eigen::Matrix<T, Dynamic, Dynamic>     DenseMatrix;
  typedef Eigen::JacobiSVD<DenseMatrix>          SVDType;
  typedef typename SVDType::SingularValuesType   SgValType;
  typedef typename SVDType::MatrixUType		       UMatrixType;
  typedef typename SVDType::MatrixVType		       VMatrixType;

  // Data member
  std::vector<T> singular_values;

  // No assignement or copy
  SVD(const SVD& copy_from);
  SVD & operator=(const SVD& copy_from);

public:
  SVD(const std::vector<int>& ir0, const std::vector<int>& ic0, const Cluster& t0, const Cluster& s0,int rank0=-1): LowRankMatrix<T>(ir0,ic0,t0,s0,rank0){}

  void build(const IMatrix<T>& A){
    int reqrank=0;
    if (this->rank==0){
      this->U.resize(this->nr,1);
      this->V.resize(1,this->nc);
      return;
    }
    else if (this->rank==-1){
      reqrank=std::min(this->ir.size(),this->ic.size());
      this->rank=reqrank;
    }
    else{
      reqrank=this->rank;
    }
    //// Matrix assembling
    DenseMatrix M(this->nr,this->nc);
    for (int i=0; i<M.rows(); i++){
  		for (int j=0; j<M.cols(); j++){
  			M(i,j) = A.get_coef(this->ir[i], this->ic[j]);
      }
    }

    //// SVD
    SVDType svd(M,Eigen::ComputeThinU | Eigen::ComputeThinV );

    const SgValType& sv = svd.singularValues();
    singular_values.resize(sv.size());
    for(int j=0; j<sv.size(); j++){singular_values[j]=sv[j];}
    const UMatrixType& uu = svd.matrixU();
    const VMatrixType& vv = svd.matrixV();

    this->U.resize(M.rows(),reqrank);
    this->V.resize(reqrank,M.cols());

    for (int i=0;i<M.rows();i++){
      for (int j=0;j<reqrank;j++){
        this->U(i,j)=uu(i,j)*sv[j];
      }
    }
    for (int i=0;i<reqrank;i++){
      for (int j=0;j<M.cols();j++){
          this->V(i,j)=vv(j,i);
        }
    }
  }

  T get_singular_value(int i){return singular_values[i];}
  //
  // double NormFrob(){
  //   T frob = 0.;
  //
  //   for (int j=0;j<m.nr;j++){
  //     for (int k=0;k<m.nc;k++){
  //       T aux=0;
  //       for (int l=0;l<m.rank;l++){
  //         T += m.U(j,l) * m.V(l,k);
  //       }
  //     frob+=pow(abs(aux),2);
  //     }
  //   }
  // }
};

}
{'fraction_non_alphanumeric': 0.19894133215703572, 'fraction_numerical': 0.01168945743273048, 'mean_word_length': 2.7603648424543947, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3909751', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1819, 'n_words': 332}