在块矩阵中找到一个值 [英] find a value in a block matrix

查看:85
本文介绍了在块矩阵中找到一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个基于Block压缩存储的稀疏矩阵类,几乎写了所有方法,但是我不知道如何编写给出原始矩阵2个索引的方法findValue(i,j)!存储由四个向量组成:

I wrote a sparse matrix class, based on Block compressed storage, I wrote almost all the method, but I have not idea to how to write the method findValue(i,j) that give 2 indexes of the original matrix ! the storage consists in four vectors :

  • `ba_':以自上而下的左右顺序存储矩阵的非零块(几乎一个元素与零不同的矩形块)

  • `ba_': stored the non zero block (rectangular block in which almost one element is different from zero) of the matrix in top-down left-right order

an_是索引向量,指向向量ba

an_ is the vector of index that points to the first element of the block in the vector ba

ai_将每行的第一块存储在块矩阵中.

ai_ stored the first block of each row in the blocked matrix.

图片澄清了任何内容:

the picture clarify anything :

在下面的类中,我使用两种方法来获得结果,即findBlockIndexfindValue(i,j,Brows,Bcols),但是我需要使用findValue(i,j)获取原始i,j索引的值,其中i,j是稀疏完整矩阵中的索引

here the following class in which I use two methods to achieve the result, findBlockIndex and findValue(i,j,Brows,Bcols) but I need to get the value of the original i,j index using findValue(i,j) in which i,j are the index in the sparse complete matrix

 # include <iosfwd>
    # include <vector>
    # include <string>
    # include <initializer_list>
    # include "MatrixException.H"
    # include <sstream>
    # include <fstream>
    # include <algorithm>
    # include <iomanip>

    // forward declarations
    template <typename T, std::size_t R, std::size_t C>
    class BCRSmatrix ;


    template <typename T, std::size_t R, std::size_t C>
    std::ostream& operator<<(std::ostream& os , const BCRSmatrix<T,R,C>& m );

    template <typename T, std::size_t Br, std::size_t Bc >
    std::vector<T> operator*(const BCRSmatrix<T,Br,Bc>& m, const std::vector<T>& x );



    template <typename data_type, std::size_t BR , std::size_t BC>
    class BCRSmatrix {


          template <typename T, std::size_t R, std::size_t C>
          friend std::ostream& operator<<(std::ostream& os , const BCRSmatrix<T,R,C>& m );

          template <typename T, std::size_t Br,std::size_t Bc>
          friend std::vector<T> operator*(const BCRSmatrix<T,Br,Bc>& m, const std::vector<T>& x );

       public:

         constexpr BCRSmatrix(std::initializer_list<std::vector<data_type>> dense );  

         constexpr BCRSmatrix(const std::string& );  

         virtual ~BCRSmatrix() = default ; 

         auto constexpr print_block(const std::vector<std::vector<data_type>>& dense,
                                      std::size_t i, std::size_t j) const noexcept ; 

         auto constexpr validate_block(const std::vector<std::vector<data_type>>& dense,
                                      std::size_t i, std::size_t j) const noexcept ; 

         auto constexpr insert_block(const std::vector<std::vector<data_type>>& dense,
                                                           std::size_t i, std::size_t j) noexcept ;

         auto constexpr printBCRS() const noexcept ; 

         auto constexpr printBlockMatrix() const noexcept  ; 

         auto constexpr size1() const noexcept { return denseRows ;}

         auto constexpr size2() const noexcept { return denseCols ;} 

         auto constexpr printBlock(std::size_t i) const noexcept ;

         auto constexpr print() const noexcept ; 





       private:

        std::size_t bn  ;
        std::size_t bBR ;
        std::size_t nnz ;
        std::size_t denseRows ;
        std::size_t denseCols ;

        std::vector<data_type>    ba_ ; 
        std::vector<std::size_t>  an_ ;
        std::vector<std::size_t>  ai_ ;
        std::vector<std::size_t>  aj_ ;


        std::size_t index =0 ;

        auto constexpr findBlockIndex(const std::size_t r, const std::size_t c) const noexcept ;  

        auto constexpr recomposeMatrix() const noexcept ;

        auto constexpr findValue(
                                  const std::size_t i, const std::size_t j, 
                                  const std::size_t rBlock, const std::size_t cBlock
                                ) const noexcept ;

    };


    //---------------------------      IMPLEMENTATION      
    template <typename T, std::size_t BR, std::size_t BC>
    constexpr BCRSmatrix<T,BR,BC>::BCRSmatrix(std::initializer_list<std::vector<T>> dense_ )
    {
          this->denseRows = dense_.size();   
          auto it         = *(dense_.begin());
          this->denseCols = it.size();

          if( (denseRows*denseCols) % BR != 0 )
          {
                throw InvalidSizeException("Error block size is not multiple of dense matrix size");
          }

         std::vector<std::vector<T>> dense(dense_);
         bBR = BR*BC ;  
         bn  = denseRows*denseCols/(BR*BC) ;


        ai_.resize(denseRows/BR +1);
        ai_[0] = 1;

        for(std::size_t i = 0; i < dense.size() / BR ; i++)
        {    
            auto rowCount =0;
            for(std::size_t j = 0; j < dense[i].size() / BC ; j++)
            {
                if(validate_block(dense,i,j))
                {     
                      aj_.push_back(j+1);
                      insert_block(dense, i, j);
                      rowCount ++ ;
                }      

            }
            ai_[i+1] = ai_[i] + rowCount ;
         }
         printBCRS();
    }


    template <typename T, std::size_t BR, std::size_t BC>
    constexpr BCRSmatrix<T,BR,BC>::BCRSmatrix(const std::string& fname)
    {
        std::ifstream f(fname , std::ios::in);
        if(!f)
        {
           throw OpeningFileException("error opening file in constructor !");
        }
        else
        {
           std::vector<std::vector<T>> dense;
           std::string line, tmp;
           T elem = 0 ;
           std::vector<T> row;
           std::size_t i=0, j=0 ;     

           while(getline(f, line))
           {
              row.clear();  
              std::istringstream ss(line);
              if(i==0) 
              {
                while(ss >> elem)
                {
                  row.push_back(elem);
                  j++;
                }
              }
              else
              {
                while(ss >> elem) 
                  row.push_back(elem);
              }
              dense.push_back(row);  
              i++;  
           }

           this->denseRows = i;
           this->denseCols = j;

           bBR = BR*BR ;  
           bn  = denseRows*denseCols/(BR*BC) ;

           ai_.resize(denseRows/BR +1);
           ai_[0] = 1;


           for(std::size_t i = 0; i < dense.size() / BR ; i++)
           {    
              auto rowCount =0;
              for(std::size_t j = 0; j < dense[i].size() / BC ; j++)
              {
                  if(validate_block(dense,i,j))
                  {     
                        aj_.push_back(j+1);
                        insert_block(dense, i, j);
                        rowCount ++ ;
                  }      

              }
              ai_[i+1] = ai_[i] + rowCount ;
           }

        }
        printBCRS();

    }


    template <typename T,std::size_t BR, std::size_t BC>
    inline auto constexpr BCRSmatrix<T,BR,BC>::printBlockMatrix() const noexcept  
    {

          for(auto i=0 ; i < denseRows / BR ; i++)
          {
            for(auto j=1 ; j <= denseCols / BC  ; j++)
            {
                std::cout << findBlockIndex(i,j) << ' ' ;  
            }
             std::cout << std::endl;   
          }
    }



    template <typename T,std::size_t BR,std::size_t BC>
    inline auto constexpr BCRSmatrix<T,BR,BC>::printBlock(std::size_t i) const noexcept
    {  
       auto w = i-1 ;
       auto k = 0;   
       for(std::size_t i = 0 ; i < BR ; ++i) 
       {
          for(std::size_t j=0 ; j < BC ; ++j )
          {
              std::cout << std::setw(8) << ba_.at(an_.at(w)-1+k) << ' ';
              k++;        
          }            
       }
    }



    template <typename T,std::size_t BR, std::size_t BC>
    inline auto constexpr BCRSmatrix<T,BR,BC>::print_block(const std::vector<std::vector<T>>& dense,
                                                           std::size_t i, std::size_t j) const noexcept
    {   
       for(std::size_t m = i * BR ; m < BR * (i + 1); ++m) 
       {
          for(std::size_t n = j * BC ; n < BC * (j + 1); ++n)
                      std::cout << dense[m][n] << ' ';
          std::cout << '\n';
       }
    }

    template <typename T,std::size_t BR, std::size_t BC>
    inline auto constexpr BCRSmatrix<T,BR,BC>::validate_block(const std::vector<std::vector<T>>& dense,
                                                           std::size_t i, std::size_t j) const noexcept
    {   
       bool nonzero = false ;
       for(std::size_t m = i * BR ; m < BR * (i + 1); ++m)
       {
          for(std::size_t n = j * BC ; n < BC * (j + 1); ++n)
          {
                if(dense[m][n] != 0) nonzero = true;
          }
       }
       return nonzero ;
    }


    template <typename T,std::size_t BR, std::size_t BC>
    inline auto constexpr BCRSmatrix<T,BR,BC>::insert_block(const std::vector<std::vector<T>>& dense,
                                                           std::size_t i, std::size_t j) noexcept
    {   

       bool firstElem = true ;
       for(std::size_t m = i * BR ; m < BR * (i + 1); ++m)
       {
          for(std::size_t n = j * BC ; n < BC * (j + 1); ++n)
          {    
                if(firstElem)
                {
                      an_.push_back(index+1);
                      firstElem = false ;
                }
                ba_.push_back(dense[m][n]);
                index ++ ;
          }
       }
    }   


    template <typename T, std::size_t BR,std::size_t BC> 
    auto constexpr BCRSmatrix<T,BR,BC>::findBlockIndex(const std::size_t r, const std::size_t c) const noexcept 
    {
          for(auto j= ai_.at(r) ; j < ai_.at(r+1) ; j++ )
          {   
             if( aj_.at(j-1) == c  )
             {
                return j ;
             }
          }
    }

    template <typename T, std::size_t BR, std::size_t BC>
    auto constexpr BCRSmatrix<T,BR,BC>::printBCRS() const noexcept 
    { 

      std::cout << "ba_ :   " ;
      for(auto &x : ba_ ) 
          std::cout << x << ' ' ;
        std::cout << std::endl; 

      std::cout << "an_ :   " ;
      for(auto &x : an_ ) 
          std::cout <<  x << ' ' ;
        std::cout << std::endl; 

      std::cout << "aj_ :   " ;
      for(auto &x : aj_ ) 
          std::cout <<  x << ' ' ;
        std::cout << std::endl; 

       std::cout << "ai_ :   " ; 
       for(auto &x : ai_ ) 
          std::cout << x << ' ' ;
        std::cout << std::endl; 

    }

    template <typename T, std::size_t BR, std::size_t BC> 
    auto constexpr BCRSmatrix<T,BR,BC>::print() const noexcept 
    {      
        //for each BCRS row
        for(auto i=0 ; i < denseRows / BR ; i++){
            //for each Block sub row.
            for(auto rBlock = 0; rBlock < BR; rBlock++){
                //for each BCSR col.
                for(auto j = 1; j <= denseCols / BC; j++){
                    //for each Block sub col.
                    for(auto cBlock = 0; cBlock < BC; cBlock++){
                        std::cout<< findValue(i, j, rBlock, cBlock) <<'\t';
                    }
                }
                std::cout << std::endl;
            }
        }
    }


    template <typename T, std::size_t BR,std::size_t BC> 
    auto constexpr BCRSmatrix<T,BR,BC>::recomposeMatrix() const noexcept
    {

        std::vector<std::vector<T>> sparseMat(denseRows, std::vector<T>(denseCols, 0));
        auto BA_i = 0, AJ_i = 0;
        //for each BCSR row
        for(auto r = 0; r < denseRows/BR; r++){
            //for each Block in row
            for(auto nBlock = 0; nBlock < ai_.at(r+1)-ai_.at(r); nBlock++){  
                //for each subMatrix (Block)
                for(auto rBlock = 0; rBlock < BR; rBlock++){
                    for(auto cBlock = 0; cBlock < BC; cBlock++){
                        //insert value
                        sparseMat.at(rBlock + r*BR).at(cBlock + (aj_.at(AJ_i)-1)*BC) = ba_.at(BA_i);
                        ++BA_i;
                    }
                }
            ++AJ_i;
            }
        }
        return sparseMat;
    }


    template <typename T, std::size_t BR,std::size_t BC> 
    auto constexpr BCRSmatrix<T,BR,BC>::findValue(
                                               const std::size_t i, const std::size_t j, 
                                               const std::size_t rBlock, const std::size_t cBlock
                                              ) const noexcept 
    {
        auto index = findBlockIndex(i,j);
        if(index != 0)
            return ba_.at(an_.at(index-1)-1 + cBlock + rBlock*BC);
        else
          return T(0);
    }



    template <typename T, std::size_t BR,std::size_t BC>
    std::ostream& operator<<(std::ostream& os , const BCRSmatrix<T,BR,BC>& m )
    {
        for(auto i=0 ; i < m.denseRows / BR ; i++)
        {
            //for each Block sub row.
            for(auto rBlock = 0; rBlock < BR; rBlock++)
            {
                //for each BCSR col.
                for(auto j = 1; j <= m.denseCols / BC; j++)
                {
                    //for each Block sub col.
                    for(auto cBlock = 0; cBlock < BC; cBlock++)
                    {
                        os << m.findValue(i, j, rBlock, cBlock) <<'\t';
                    }
                }
                os << std::endl;
            }
        }
        return os;  
    }

    template <typename T, std::size_t BR, std::size_t BC>
    std::vector<T> operator*(const BCRSmatrix<T,BR,BC>& m, const std::vector<T>& x )
    {
          std::vector<T> y(x.size());
          if(m.size1() != x.size())
          {
           std::string to = "x" ;
           std::string mess = "Error occured in operator* attempt to perfor productor between op1: "
                            + std::to_string(m.size1()) + to + std::to_string(m.size2()) +
                                     " and op2: " + std::to_string(x.size());
                throw InvalidSizeException(mess.c_str());
          }
          else
          {
                auto brows = m.denseRows/BR ;  
                auto bnze  = m.an_.size()   ;

                auto z=0;

                for(auto b=0 ; b < brows ; b++)
                {     
                   for(auto j= m.ai_.at(b) ; j <= m.ai_.at(b+1)-1; j++ )
                   {      
                      for(auto k=0 ; k < BR ; k++ )
                      {
                         for(auto t=0 ; t < BC ; t++)
                         {
                             y.at(BC*b+k) += m.ba_.at(z) * x.at(BC*(m.aj_.at(j-1)-1)+t) ;          
                             z++ ;
                         }     
                      }
                   }   
                }

          }
          return y;      

    }

这是主要的

# include "BCSmatrix.H"


using namespace std;



int main(){

  BCRSmatrix<int,2,2> bbcsr1 =  {{11,12,13,14,0,0},{0,22,23,0,0,0},{0,0,33,34,35,36},{0,0,0,44,45,0},
                           {0,0,0,0,0,56},{0,0,0,0,0,66}};


  BCRSmatrix<int,2,2> bbcsr2 = {{11,12,0,0,0,0,0,0} ,{0,22,0,0,0,0,0,0} ,{31,32,33,0,0,0,0,0},
                              {41,42,43,44,0,0,0,0}, {0,0,0,0,55,56,0,0},{0,0,0,0,0,66,67,0},{0,0,0,0,0,0,77,78},{0,0,0,0,0,0,87,88}};


  BCRSmatrix<int,2,4> bbcsr3 = {{11,12,0,0,0,0,0,0} ,{0,22,0,0,0,0,0,0} ,{31,32,33,0,0,0,0,0},
                              {41,42,43,44,0,0,0,0}, {0,0,0,0,55,56,0,0},{0,0,0,0,0,66,67,0},{0,0,0,0,0,0,77,78},{0,0,0,0,0,0,87,88}};


  bbcsr3.printBlockMatrix();

  bbcsr3.print();

  BCRSmatrix<int,2,2> bbcsr4("input17.dat");

  bbcsr4.printBlockMatrix();

  BCRSmatrix<int,2,4> bbcsr5("input18.dat");

  bbcsr5.printBlockMatrix();
  cout << bbcsr5 ;

  BCRSmatrix<int,4,4> bbcsr6("input18.dat");

  bbcsr6.printBlockMatrix();

  bbcsr6.print();


  cout << bbcsr4 ; //.print();    

  BCRSmatrix<int,2,4> bbcsr7("input20.dat");

  cout << bbcsr7;   

  bbcsr7.printBlockMatrix();


  std::vector<int> v1 = {3,4,0,1,6,8,1,19};

  std::vector<int> v01 = {3,4,0,1,6,8,1,19,15,2};

  std::vector<int> v2 = bbcsr4 *v1 ;

  for(auto& x : v2)
        cout << x << ' ' ;
  cout << endl; 



  BCRSmatrix<double,2,2> bbcsr8("input21.dat");

  bbcsr8.print() ;
  bbcsr8.printBlockMatrix();    



  return 0;
}

推荐答案

如何编写给出原始矩阵的2个索引的方法findValue(i,j)

它与以前的 findValue 方法相似:

template <typename T, std::size_t BR,std::size_t BC>
auto constexpr BCRSmatrix<T,BR,BC>::myNewfindValue(const std::size_t i, const std::size_t j) const noexcept{
    auto index = findBlockIndex(i/BR, j/BC);
    if(index != 0)
        return ba_.at(an_.at(index-1)-1 + j%BC + (i%BR)*BC);
    else
        return T(0);
}

要调用此函数:您必须对findBlockIndex进行一些更改:只需更改if( aj_.at(j-1) == c )白色if( aj_.at(j-1) == c+1 ),而不是必须在其他函数 for 语句> for(auto j = 0; j < ...

To recall this function: you have to do a little change to your findBlockIndex: just change if( aj_.at(j-1) == c ) whit if( aj_.at(j-1) == c+1 ), than you have to modify your for statements in the others functions for(auto j = 1; j <= .. whit for(auto j = 0; j < ...

让我知道是否有问题,或者这不是您要寻找的答案. 希望对您有所帮助,

Let me know if there are problems or this is not the answer you were looking for. I hope to be of help to you,

问候马可.

这篇关于在块矩阵中找到一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆