2014-03-25 195 views
0

我想制作一个能够将2x2矩阵上升到k次方的程序。我知道如何制造一个能够平衡它的人,但是一旦我想要达到更高的能力,我就努力保存我所得到的结果,并在下一个等式中使用它。 a,b,c,d是矩阵中的数字,n是一次我想要做的矩阵的量,k是我想要矩阵被采用的功率,m是我想要使用的模的数量在数字上。我知道有一种相当简单的方法,但我无法找到一种很好的方法来使用我之前在下一个等式中完成的方程的结果。乘以矩阵C++

#include <iostream> 

using namespace std; 

int mnoz(int a, int b, int c,int d,int m){ 

    int a2 = (a*a + c*b) % m; 

    int b2 = (a*b + b*d) % m; 

    int c2 = (c*a + d*c) % m; 

    int d2 = (c*b + d*d) % m; 
    return a2, b2, c2, d2; 
} 

int main() 

{ 

    int a, b, c, d, k, m, n; 

    int e, f, g, h; 

    int e2, f2, g2, h2; 

    cin >> n; 



    // a^k = (a^2)^k/2 

    for (int i = 0; i<n; i++){ 

     cin >> a >> b >> c >> d >> k >> m; 

     if (k == 1){ 

      cout << a << " " << b << " " << c << " " << d << endl; 



     } 



     else if (k == 2){ 

      e = (a*a + c*b) % m; 

      f = (a*b + b*d) % m; 

      g = (c*a + d*c) % m; 

      h = (c*b + d*d) % m; 

      cout << e << " " << f << " " << g << " " << h << endl; 

     } 
     else{ 

      if (k % 2 == 0){ 
       e = (a*a + c*b) % m; 

       f = (a*b + b*d) % m; 

       g = (c*a + d*c) % m; 

       h = (c*b + d*d) % m; 
       int z = (k/2)-1; 

       for (int j = 0; j < z; j++){ 
        int e2 = e; 
        int f2 = f; 
        int g2 = g; 
        int h2 = h; 
        mnoz(e2, f2, g2, h2, m); 

       } 
       cout << e << " " << f << " " << g << " " << h << endl; 

      } 
     } 
    } 
    system("pause"); 
    return 0; 

} 
+0

我相信你需要学习C或C的内存管理方面++ - 分配2×2矩阵为向量的双指针或载体。然后,您可以将它传递给该功能,并随时随地保留部分产品。 –

回答

2

要减少乘法次数,可以使用递归方法。

要给出一个与实数相似的例子,假设你想计算a^n

  1. 检查n是偶数还是奇数。
  2. 如果偶数,则计算b = a^(n/2)。然后最终的结果是b*b
  3. 如果奇怪,请计算b = a^((n-1)/2。然后最终的结果是b*b*a

既然你说的是矩阵而不是数字,你需要能够乘以任意两个矩阵。您可以为矩阵创建类/结构并添加operator*()函数。

下面是一些示例代码。

#include <iostream> 

struct Matrix 
{ 
    Matrix(double pa, double pb, double pc, double pd) : a(pa), b(pb), c(pc), d(pd) {} 

    Matrix operator*(Matrix const& rhs) const 
    { 
     double an = this->a*rhs.a + this->b*rhs.c; 
     double bn = this->a*rhs.b + this->b*rhs.d; 
     double cn = this->c*rhs.a + this->d*rhs.c; 
     double dn = this->c*rhs.b + this->d*rhs.d; 
     return Matrix(an, bn, cn, dn); 
    } 

    Matrix square() const 
    { 
     return (*this)*(*this); 
    } 

    double a; 
    double b; 
    double c; 
    double d; 
}; 

Matrix matrixPower(Matrix const& m, int k) 
{ 
    if (k == 1) 
    { 
     return m; 
    } 

    Matrix out = matrixPower(m, k/2).square(); 
    if (k%2 == 1) 
    { 
     return out*m; 
    } 
    else 
    { 
     return out; 
    } 
} 

std::ostream& operator<<(std::ostream& out, Matrix const& m) 
{ 
    out << "[ " << m.a << " " << m.b << " ]\n"; 
    out << "[ " << m.c << " " << m.d << " ]\n"; 
} 

int main() 
{ 
    Matrix m(0.4, 0.7, 0.7, 0.4); 
    std::cout << matrixPower(m, 5) << std::endl; 
    std::cout << matrixPower(m, 10) << std::endl; 
    std::cout << matrixPower(m, 15) << std::endl; 
}; 

输出:

 
[ 0.80404 0.80647 ] 
[ 0.80647 0.80404 ] 

[ 1.29687 1.29687 ] 
[ 1.29687 1.29687 ] 

[ 2.08862 2.08862 ] 
[ 2.08862 2.08862 ]