Я работаю над матричным инвертором, и он у меня почти готов, но по какой-то причине функция, которая должна поднимать матрицу до определенного значения, не работает, я выделил функцию отдельно, и она работала просто отлично. Но почему-то не работает в этой программе

Изолировано

#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 << "\n\t\t    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 << "\n\t\t    Inverse: " << endl;
printinator(inm);
multiplinator(as,a,z);
cout << "\n\t\t    multi: " << endl;
printinator(z);

*/

}
-1
Dumbquestionsman 23 Ноя 2019 в 22:05
2
Пожалуйста, объясните подробно, что означает "не работает в этой программе". Ваша программа не компилируется? Это segfault? Разве это не дает ожидаемого результата? Пожалуйста, включите любые сообщения об ошибках и выходные данные программы, а также ожидаемые выходные данные.
 – 
walnut
23 Ноя 2019 в 22:14
Зачем вам нужно преобразовывать матрицу в число, чтобы найти ее обратную? Взгляните на это.
 – 
Javier Silva Ortíz
23 Ноя 2019 в 22:20
У меня было что-то подобное, но мой профессор хочет, чтобы это было так. И да, вывод неверный.
 – 
Dumbquestionsman
24 Ноя 2019 в 00:04

1 ответ

Изолированный код

powernator(r,B,3);

Актуальный код.

powernator(r,B,2);

Параметр p отличается..

0
Peter Lee 23 Ноя 2019 в 22:25
Да, но вы можете изменить его, и изолированный правильный, тогда как тот, что в коде, возвращает все нули.
 – 
Dumbquestionsman
23 Ноя 2019 в 23:32
Входная матрица B отличается. Используйте тот же main(), сравните результат..
 – 
Peter Lee
24 Ноя 2019 в 01:34