在矩阵逆变器上工作,由于某种原因,我的指数循环不起作用

Working on a matrix inverter and my exponential loop is not working for some reason

本文关键字:由于某种原因 我的 循环 不起作用 指数 工作 逆变器      更新时间:2023-10-16

我正在研究矩阵逆变器,我几乎完成了它,但由于某种原因,应该将矩阵提高到某个值的功能不起作用,我已经隔离了该功能本身,它工作得很好。但由于某种原因在此程序中不起作用

孤立

#include <iomanip>
#include <stdio.h>
using namespace std;
void printano(double a[3][3])
{
for(int i=0; i<3; i++)
    {
    for(int j=0; j<3; j++)
        cout << fixed << setprecision(2) << setw(12) << a[i][j] << "  ";
    cout << endl;
    }
}
void powernator(double r[][3],double B[][3], int p)
{
    double temp[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
    int n = 3;
  for (int b = 0; b < n; b++)
{
    for (int d = 0; d < n; d++)
    {
      r[b][d] = B[b][d];
    }
}
  for (int i = 0; i < p - 1; i++)
  {
    int sum = 0;
    for (int b = 0; b < n; b++)
    {
      for (int d = 0; d < n; d++)
      {
        for (int k = 0; k < n; k++)
        {
          sum += B[b][k] * r[k][d];
        }
        temp[b][d] = sum;
        sum = 0;
      }
    }
    for (int b = 0; b < n; b++)
    {
      for (int d = 0; d < n; d++)
      {
        r[b][d] = temp[b][d];
      }
    }
  }
}
int main()
{
double B[3][3] = { {1, 2, 3} , {4, 5, 6} , {7, 8, 9} };
double r[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
powernator(r,B,3);
printano(r);
}

实际代码

#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdio.h>
using namespace std;
void multiplinator(double x[][3], double y[][3], double z[][3]) //At the end I double check to make sure the value is correct as it needs to equal the identity matrix
{
    for(int i = 0; i < 3; i++)
    {
            for(int j = 0; j < 3; j++)
            {
                    for(int k = 0; k < 3; k++)
                    {
                            z[i][j] += x[i][k] * y[k][j];
                    }
            }
    }
}
void printinator(double a[3][3]) //prints a matrix
{
for(int i=0; i<=2; i++)
    {
    for(int j=0; j<=2; j++)
        cout << fixed << setprecision(4) << setw(12) << a[i][j] << "  ";
    cout << endl;
    }
        cout << endl;
}
void sub(double as[3][3], double in[][3], double B[][3]) //Matrix subtraction
{
   for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            B[i][j] = in[i][j] - as[i][j];
}
void powernator(double r[][3],double B[][3], int p) //Array which is supposed to raise a matrix to a certain power
{
    double temp[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
    int n = 3;
  for (int b = 0; b < n; b++)
{
    for (int d = 0; d < n; d++)
    {
      r[b][d] = B[b][d];
    }
}
  for (int i = 0; i < p - 1; i++)
  {
    int sum = 0;
    for (int b = 0; b < n; b++)
    {
      for (int d = 0; d < n; d++)
      {
        for (int k = 0; k < n; k++)
        {
          sum += B[b][k] * r[k][d];
        }
        temp[b][d] = sum;
        sum = 0;
      }
    }
    for (int b = 0; b < n; b++)
    {
      for (int d = 0; d < n; d++)
      {
        r[b][d] = temp[b][d];
      }
    }
  }
}
void gettem(double r[][3], double in[][3], double inm[][3]) //Supposed to return the final value, aka, the inverse matrix, as a^-1 = I + B^1 +B^2...
{
       for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            inm[i][j] = in[i][j] + r[i][j];
    }

}
int main()
{
double a[3][3] = { {1./2, 1, 0} , {0, 2./3, 0} , {-1./2, -1, 2./3} };
double as[3][3] ={ {1./2, 1, 0} , {0, 2./3, 0} , {-1./2, -1, 2./3} };
double in[3][3] = { {1, 0, 0} , {0, 1, 0} , {0, 0, 1} };
double B[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
double r[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
double inm[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
double z[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };

cout << "ntt    Original : " << endl;
    printinator(a);
sub(as,in,B);
printinator(B);
powernator(r,B,2);
printinator(r); //testing the power function, not working
/*for(int n = 0; n < 20; n++) //Final part of the code commented out for debug, this loop is meant to add up B^n where n is from 1 - 20
{
   for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            r[i][j] += B[i][j];
}
gettem(r,in,inm);
cout << "ntt    Inverse: " << endl;
printinator(inm);
multiplinator(as,a,z);
cout << "ntt    multi: " << endl;
printinator(z);
*/
}

隔离代码

powernator(r,B,3);

实际代码。

powernator(r,B,2);

参数p不同。

相关文章: