使用映射向量实现的稀疏矩阵

时间:2017-11-04 18:01:41

标签: c++ matrix

正如我在另一篇文章中所写的那样(因为我发现了一些解决方案而关闭)我实现了一个稀疏矩阵类,数据容器由map的vactor完成,其中vector的索引rappresent矩阵的行的索引因此每个索引都存储了一张地图!

这里是界面:

template <typename element_type>
class SparseMatrix {

  public:
   template<class T>
   friend SparseMatrix<T> operator+(const SparseMatrix<T>& m1 , const SparseMatrix<T>& m2 );  

   template<class T>
   friend SparseMatrix<T> operator-(const SparseMatrix<T>& m1 , const SparseMatrix<T>& m2 );  

   template <class T>
   friend SparseMatrix<T> operator*(const SparseMatrix<T>& m1 , const SparseMatrix<T>& m2  );

  public:
    // container type ;  
    using data_type = std::vector<std::map<std::size_t , element_type >> ;
    using it_rows   = typename std::map<std::size_t , element_type>::iterator ; 

    SparseMatrix(std::size_t rows , std::size_t cols) : rows{rows} , columns{cols}
    {
       data.resize(rows);     
    }

    SparseMatrix(std::initializer_list<std::initializer_list<element_type>> l );

    SparseMatrix(const std::string );  


    auto insert(std::size_t i , std::pair<std::size_t, element_type> p )
    { 
       assert( i <= rows && p.first <= columns); // , "Index out of bound" );
       data.at(i).insert(p);
    }

    auto insert(std::size_t i, std::size_t j, element_type val)
    {
      assert(i<=rows && j <=columns);
      data.at(i)[j] = val ;
    }

    auto identity() noexcept ;

    auto diag(const element_type& v) noexcept ;

    auto print() const noexcept ; 

    auto dataType() const noexcept ;

    auto traspose() noexcept ;

  private:

    std::size_t rows    ;
    std::size_t columns ;
      data_type data    ;     // vector containing row element 


};

在上一篇文章中,我询问了traspose矩阵..我找到了这个解决方案:

template <typename T>
auto SparseMatrix<T>::traspose() noexcept 
{

    //rows = columns ;   
    std::swap(rows,columns);  

    data_type newData ;
    newData.resize(rows);

    for(std::size_t i=0; i < columns ; i++)
    { 
      for(std::size_t j=0 ; j < rows ; j++)
      {
        if(data.at(i).find(j) != data.at(i).end())
        {
            newData.at(j)[i] = data.at(i).at(j) ;
        }    
      }
    }
    std::swap(data,newData);
}

现在我不知道如何计算矩阵产品......你能给我一些建议吗?

好的..感谢@BeyelerStudios收到的suggeriment我得到了poind并以这种方式写了点积:

template <class T>
SparseMatrix<T> dot(const SparseMatrix<T>& m1 , const SparseMatrix<T>& m2  )
{
      if(m1.columns != m2.rows)
      {
            throw std::runtime_error("Matrix sizes dosent match the dot-product");
      }

      SparseMatrix<T> result{m1.rows  ,  m2.columns };

      for(std::size_t i=0 ; i < result.rows ; i++)
      {
           for(std::size_t j=0 ; j < result.columns ; j++ )
           {
                 for(std::size_t k=0; k< m1.columns ; k++)
                 {
                        if( m1.data.at(i).find(k) != m1.data.at(i).end() && 
                            m2.data.at(k).find(j) != m2.data.at(k).end()    )
                        {    
                            result.data.at(i)[j] += (m1.data.at(i).at(k) * m2.data.at(k).at(j)) ;
                        }
                 }
           }
      }
      return result ;      
}     

现在我打开另一个帖子询问行列式和反向!

0 个答案:

没有答案