Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在块矩阵中查找值_C++_Matrix - Fatal编程技术网

C++ 在块矩阵中查找值

C++ 在块矩阵中查找值,c++,matrix,C++,Matrix,我写了一个稀疏矩阵类,基于块压缩存储,我写了几乎所有的方法,但我不知道如何编写方法findValue(I,j),它给出了原始矩阵的两个索引!存储由四个向量组成: `ba_u2;”:按自上而下的左右顺序存储矩阵的非零块(其中几乎有一个元素与零不同的矩形块) an是指向向量ba aj将块列的索引存储在块矩阵中 ai将每行的第一个块存储在分块矩阵中 这幅图澄清了一切: 在下面的类中,我使用两种方法来实现结果,findBlockIndex和findValue(I,j,Brows,Bcols)

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

  • `ba_u2;”:按自上而下的左右顺序存储矩阵的非零块(其中几乎有一个元素与零不同的矩形块)

  • an
    是指向向量
    ba

  • aj
    将块列的索引存储在块矩阵中

  • ai
    将每行的第一个块存储在分块矩阵中

    这幅图澄清了一切:

在下面的类中,我使用两种方法来实现结果,
findBlockIndex
findValue(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;      
    
    }
#包括
#包括
#包括
#包括
#包括“MatrixException.H”
#包括
#包括
#包括
#包括
//转发声明
模板
类BCR矩阵;
模板
std::ostream和操作员(要素)
{
世界其他地区。推回(elem);
j++;
}
}
其他的
{
while(ss>>元素)
世界其他地区。推回(elem);
}
密集。向后推(世界其他地区);
i++;
}
这->denseRows=i;
这->denseCols=j;
bBR=BR*BR;
bn=denseRows*denseCols/(BR*BC);
ai.调整大小(denseRows/BR+1);
ai_u0]=1;
对于(std::size\u t i=0;i对于(auto j=1;j将原始
findValue
重命名为
findVal
,然后定义一个新的
findValue
,该新的
findValue
正好包含以下定义的2个元素(我知道是错误的):

模板
T constexpr SqBCSmatrix::findValue(const std::size\u T r,const std::size\u T c)const noexcept
{      
//对于每个BCRS行
对于(自动i=0,k=0;ifindValue(i,j)
,给出原始矩阵的两个索引

它类似于前面的findValue方法:

模板
auto constexpr BCRSmatrix::myNewfindValue(const std::size\u t i,const std::size\u t j)const noexcept{
自动索引=findBlockIndex(i/BR,j/BC);
如果(索引!=0)
返回ba_uu.at(an_uu.at(索引-1)-1+j%BC+(i%BR)*BC);
其他的
返回T(0);
}

要调用此函数:您必须对findBlockIndex做一些更改:只需更改if(aj.at(j-1)==c)
whit
if(aj.at(j-1)==c+1)
,而不必修改其他函数中的for语句(auto j=1;老实说,j不是我想要的……好吧,它很管用!但我会搜索类似于
findBlockIndex()
methodMarco sei un mostro:)的东西!非常感谢!ti devo una birra(意思是:Marco你很棒!我给你一杯啤酒)
# 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;
}
 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;  
}
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);
}