比较矩阵乘法

发布于 2024-09-06 17:16:54 字数 286 浏览 1 评论 0原文

我必须将一个矩阵与其自身相乘,直到该矩阵在某种程度上不等于前面的矩阵之一。然后我需要获取矩阵相等的度数值。行数和列数相等。矩阵存储在二维数组中。值为 0 或 1。检查与先前矩阵是否相等的最佳方法是什么?我尝试使用向量来存储矩阵:

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

但出现错误cannot conversion from 'const int [5][5]' to 'int [5][5]'

等待建议。

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:

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

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

Waiting for an advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-09-13 17:16:54

如果您可以使用 boost,请查看 boost Matrix 类:

似乎缺少 == 运算符,但很容易添加:

#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;
}

并像这样使用:

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 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]

反差帅 2024-09-13 17:16:54

如果您想使用矢量来实现,您可能需要矢量矢量矢量矢量。向量<整数> >,即整数向量的向量(即一种二维向量)。

vector 将(如果有效)声明一个二维 5x5-int 数组的向量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文