开发者

Compare matrices multiplication

开发者 https://www.devze.com 2023-01-04 14:53 出处:网络
I must multiply a matrix by itself until the matrix in some degree would not be equal to one of the preceding matrices. Then I need to get the values of degrees in which the matrices are equal. The nu

I must multiply a matrix by itself until the matrix in some degree would not be equal to one of the preceding matrices. Then I need to get the values of degrees in which the matrices are equal. The number of rows and columns are equal. The matrix is stored in a two-dimensional array. Values are 0 or 1. What is the best way to check for equality with the previous matrices? I tried to use vector to store matrices:

v开发者_开发技巧ector<int[5][5]> m;

but I got an error cannot convert from 'const int [5][5]' to 'int [5][5]'.

Waiting for an advice.


If you can use boost, look at the boost Matrix class:

It seems to be missing an == operator, but it's easy to add:

#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost::numeric::ublas;

template<typename T>
bool operator==(const matrix<T>& m, const matrix<T>& n)
{
  bool returnValue = 
    (m.size1() == n.size1()) &&
    (m.size2() == n.size2());

  if (returnValue)
  {
    for (unsigned int i = 0; returnValue && i < m.size1(); ++i)
    {
      for (unsigned int j = 0; returnValue && j < m.size2(); ++j)
      {
        returnValue &= m(i,j) == n(i,j);
      }
    }
  }
  return returnValue;
}

And used like so:

int main ()
{

  matrix<double> m (3, 3);
  for (unsigned int i = 0; i < m.size1(); ++ i)
  {
    for (unsigned int j = 0; j < m.size2(); ++ j)
    {
      m (i, j) = 3 * i + j;
    }
  }
  std::cout << m << std::endl;

  matrix<double> n (3, 3);

  std::cout << (m == n) << std::endl;
  std::cout << (m == m) << std::endl;
}

[Code]


If you want to do it with vector, you probably want vector < vector < int > >, i.e. a vector of vectors of ints (i.e. kind of 2-dimensional vector).

vector<int[5][5]> would (if it worked) declare a vector of 2-dimensional 5x5-int-arrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号