在块矩阵中查找值

时间:2017-12-05 13:01:00

标签: c++ matrix

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

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

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

  • 中块的第一个元素
  • aj_将块列的索引存储在阻塞矩阵中。
  • ai_将每行的第一个块存储在阻塞矩阵中。

    图片澄清了任何内容:enter image description here

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

 # 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 个答案:

答案 0 :(得分:1)

将原始findValue重命名为findVal,然后定义一个新的findValue,它将2个元素定义为以下(我知道这很糟糕):

 template <typename T, std::size_t BS> 
T constexpr SqBCSmatrix<T,BS>::findValue(const std::size_t r, const std::size_t c) const noexcept 
{      
    //for each BCRS row
    for(auto i=0 ,k=0; i < denseRows / BS ; i++){
        //for each Block sub row.
        for(auto rBlock = 0; rBlock < BS; k++ ,rBlock++){
            //for each BCSR col.
            for(auto j = 1 , l=0; j <= denseCols / BS; j++){
                //for each Block sub col.
                for(auto cBlock = 0; cBlock < BS; l++ , cBlock++){
                      if(k == r && c == l ) 
                          return findVal(i,j,rBlock, cBlock);

                } 
            }
        }
    }
    return 0;  
}

答案 1 :(得分:1)

  

如何编写给出原始矩阵的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 ) whit if( aj_.at(j-1) == c+1 ),而不是修改 for 语句其他功能for(auto j = 1; j <= .. whit for(auto j = 0; j < ..

如果有问题或者这不是您正在寻找的答案,请告诉我。 我希望对你有所帮助,

最好的问候Marco。