C ++: Matrix raised to square power

0

I want to make a program that based on a square matrix (same number of rows and columns) calculate the square power of that matrix.

Code.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){
int fila,columna,i,j;

cout<<"Ingrese numero de filas: ";
cin>>fila;
cout<<"\n";
cout<<"Ingrese numero de columnas: ";
cin>>columna;

if(fila==columna){


int matriz[fila][columna];

for(i=0;i<fila;i++){
    for(j=0;j<columna;j++){
        cout<<"\n";
        cout<<"Ingrese numero para la posicion ["<<i<<"] - ["<<j<<"] ";
        cin>>matriz[i][j];
        }
    }

        }

else{
    cout<<"\n";
    cout<<"Su matriz no es cuadrada";
}

return 0;
system("pause");

}

My question is how could I calculate the square power of a MXN matrix? From what I understand to obtain the power of a matrix, the matrix must be multiplied by itself 'n' times. But I still do not understand the steps involved in the method for its solution itself.

    
asked by Sasori1264 13.05.2018 в 22:15
source

2 answers

1

Your matrix is wrong.

To begin with, you have a big bug in your program when creating the matrix:

int matriz[fila][columna];

In C ++ the sizes of the 1 formations must be known at compile time, but in your case fila and columna are known at run time. If it works for you it will be because your compiler offers that possibility as an extension but other compilers would reject that code or your compiler could decide to stop supporting that extension. Read these questions to learn more about the topic.

You also have a conceptual error: if you want square matrices, why do you ask for both width and height? Order only the side and use it to make the matrix square by design:

Your matrix should be like this.

int *matriz = new int[lado * lado];

This is the correct way to create a matrix whose size is known at runtime; unfortunately C ++ does not offer ways to create two-dimensional formations using the operator new , so what we create is a one-dimensional formation but we will treat it as if it were two-dimensional with simple pointer arithmetic.

From 1D to 2D.

Having a two-dimensional square matrix stored in a one-dimensional formation, to obtain the element of the row y and column x we should calculate its position in the following way:

  

Element index = (and * side) + x

An auxiliary function can be useful:

int &elemento(int *&matriz_cuadrada, int lado, int x, int y) {
    return matriz_cuadrada[(y * lado) + x];
}

Square.

An auxiliary matrix will be necessary to perform the operation since it is necessary to consult the original cells. On the other hand, since the matrix multiplication implies adding the row and column products, it would be useful to have some auxiliary functions that do this operation:

int opera_fila(int *&matriz_cuadrada, int fila, int lado) {
    int resultado{};
    for (int x = 0; x < lado; ++x)
    {
        auto valor = elemento(matriz_cuadrada, lado, x, fila);
        resultado += (valor * valor);
    }
    return resultado;
}

int opera_columna(int *&matriz_cuadrada, int columna, int lado) {
    int resultado{};
    for (int y = 0; y < lado; ++y)
    {
        auto valor = elemento(matriz_cuadrada, lado, columna, y);
        resultado += (valor * valor);
    }
    return resultado;
}

And finally the function could look like:

void cuadrado(int *&matriz_cuadrada, int lado) {
    int *matriz = new int[lado * lado];

    for (int y = 0; y < lado; ++y)
    {
        for (int x = 0; x < lado; ++x)
        {
            elemento(matriz, lado, x, y) = opera_fila(matriz_cuadrada, y, lado) + opera_columna(matriz_cuadrada, x, lado);
        }
    }

    std::copy(matriz, matriz + (lado * lado), matriz_cuadrada);
    delete[] matriz;
}

You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ , you have to correct it as an exercise.

    
answered by 15.05.2018 / 10:22
source
0

The square of a matrix is calculated by multiplying for each valor[i][j] the values of the fila[i] and columna[j] with each other and finally adding them. Basically it is a product of matrices but with the same matrix.

EJEMPLO cuadrado de una matriz 2x2

|2 2|*|2 2|=|8 6|
|2 1| |2 1| |6 5|

posición (0,0) = 2*2 + 2*2 = 8, posición (0,1) = 2*2 + 2*1 = 6
posición (1,0) = 2*2 + 1*2 = 6, posición (1,1) = 2*2 + 1*1 = 5

Once this is understood, let's see for an MxM matrix, since it is square.

Matriz MxM

[0,0][0,1][0,2]..[0,M]
[1,0][1,1][1,2]..[1,M]
[2,0][2,1][2,2]..[2,M]
 ...  ...  ... .. ...
[M,0][M,1][M,2]..[M,M]

To obtain the new result matrix you will have to go through each position of the matrix MxM and go calculating the value that will be stored in that same position in the result matrix.

For the case of the position matriz[0][2] :

  matriz[0][0]    matriz[0][1]    matriz[0][2] ...   matriz[0][M]
x matriz[0][2]  x matriz[1][2]  x matriz[2][2] ... x matriz[M][2]
-------------  -------------  --------------    -------------
 producto_0  +  producto_1  +  producto_2     +  producto_M  = matrizResult[0][2]

At this point the pattern can already be guessed, but I will try to show it.

We go through the matrix with loops of the form matriz[i][j] where i and j will take values of 0 to the value indicated by fila or columna .

Substituting in the previous case for a position matriz[i][j] :

  matriz[i][0]    matriz[i][1]    matriz[i][2] ...   matriz[i][M]
x matriz[0][j]  x matriz[1][j]  x matriz[2][j] ... x matriz[M][j]
-------------  -------------  --------------    -------------
 producto_0  +  producto_1  +  producto_2     +  producto_M  = matrizResult[i][j]

It can be seen how there is similarity when calculating the product of each term and that a series is followed that increases from 0 to M being M known since it is the size of the matrix M == fila == columna , so we can simplify the calculation by using a new loop where a variable k will take the values of 0 to the value indicated by fila or columna and finally the result would be equal to the sum of all the products.

matrizResult[i][j] += matriz[i][k] * matriz[k][j]
    
answered by 14.05.2018 в 02:39