tags:

views:

97

answers:

2

I must multiply matrix by itself until the matrix in some degree would not be equal 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. 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:

vector<int[5][5]> m;

but got an error cannot convert from 'const int [5][5]' to 'int [5][5]'. Waiting for an advice.

+1  A: 

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]

Bill
A: 

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.

phimuemue