Matrix multiplication by matrix transposed in C language

Asked

Viewed 887 times

2

I am new to programming and I am trying to make a program in C language that multiplies a 3x3 matrix by transposing it and prints the result. I’m having a hard time creating a multiplication algorithm between the matrix and the transposta. Any help will be willingly.

Follows the code:

used libraries (stdio.h and stdlib.h)

int main(){

int mat[3][3], mat_transposta[3][3], mat_resultado[3][3];
int i, j;

// leitura matriz
for(i = 0; i < 3; i++){
    for(j = 0; j < 3; j++){
        scanf("%d",&mat[i][j]);
    }
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            mat_transposta[i][j] = mat[j][i]; // transformação matriz principal p/ transposta (FUNCIONANDO)
        }
    }
        for(i = 0; i < 3; i++){
            for(j = 0; j < 3; j++){
                mat[i][j] *= mat_transposta[i][j];// multiplicação da matriz pela sua transposta correspondente

            }
        }

            for (i = 0; i < 3; i++){
                for (j = 0; j < 3; j++){
                    printf("%d\t", mat_resultado[i][j]); // impressão matriz resultado (A SAÍDA TEM QUE SER NESSE FORMATO)
                }
                printf("\n");
            }
return 0;
}

So the X of the question is to solve the following algorithm (that is logically wrong, I know). I could not create a pattern ( i and j) for multiplication, which could be implemented as the minimum use of commands for to perform this multiplication.

for(i = 0; i < 3; i++){
            for(j = 0; j < 3; j++){
                mat[i][j] *= mat_transposta[i][j];// multiplicação da matriz pela sua transposta correspondente

            }
        }

I know that in the first multiplication of line 1 by transposta **i0j0*i0j0 + i0j1*i1j0 + i0j2*i2j0** and in the second multiplication of line 1 by transpose **i0j0*i0j1 + i0j1*i1j1 + i0j2*i2j1** and in the third multiplication of line 1 by transpose **i0j0*i0j2 + i0j1*i1j2 + i0j2*i2j2**

and then in the first multiplication of line 2 by transpose **i1j0*i0j0 + i1j1*i1j0 + i1j2*i2j0** in the second multiplication of line 2 by transpose **i1j0*i0j1 + i1j1*i1j1 + i1j2*i2j1** in the third multiplication of line 2 by transpose **i1j0*i0j2 + i1j1*i1j2 + i1j2*i2j2**

and finally in the first multiplication of line 3 by transpose **i2j0*i0j0 + i2j1*i1j0 + i2j2*i2j0** in the second multiplication of line 3 by transpose **i2j0*i0j1 + i2j1*i1j1 + i2j2*i2j1** in the 3rd multiplication of line 3 by transpose **i2j0*i0j2 + i2j1*i1j2 + i2j2*i2j2**

An example, if the matrix (mat[i][j]) entered by the user for 10, 0, 1, 3, 5, 2, -4, 2, 3. The result matrix (mat_resultado) will have at output the following numbers: 101, 32, -37, 32, 38, 4, -37, 4, 29.

  • You can’t multiply a matrix with less than 3 for because, for each element M[i][j], you have to add the elements of the line i and the column j. But since you’re multiplying a matrix by transpose, you can simply make the product inside the line i with the j without materializing the transposta. Of course, we still can not do in-place, since we will lose the original values after the first modified cell, then you need two more for to copy the result to the original matrix...

1 answer

2


First of all the product of two matrices is defined only when the number of columns of the first matrix is equal to the number of rows of the second matrix.

#define L 3
#define M 4
...
    int mat[L][M], mat_transposta[M][L], mat_resultado[L][L];
    int i, j, k;
    ...
    /* Multiplicação*/
    for (i=0; i<L; i++) {
        for (j=0; j<L; j++) {
            mat_resultado[i][j] = 0;
            for (k=0; k<M; k++) {
                mat_resultado[i][j] += mat[i][k] * mat_transposta[k][j];
            }
        }
    }
    ...

In your particular case L and M must have the same value, ie 3.

===================

Give a studied in the product definition of matrices to understand the use of the variable k and its loop.

  • because vc created an extra column for the matrix entered by the user mat[L][M]? To mat_resultado was set to zero because of previous memory trash?

  • Another thing is that at the exit of mat_resultado the algorithm is working only in the first and second column, ie in the third column the value is going wrong.

  • I tried to understand the passage mat_resultado[i][j] += mat[i][k] * mat_transposta[k][j]; but I did not succeed. If I am not abusing your good will, you can explain me a little bit of this step, because I did not understand how the k is used in that code.

  • In the declaration of swabs int mat[L][M], mat_transposta[M][L], mat_resultado[L][L]; exchanging a detail of mat_transposta[M][L] for mat_transposta[M][M] made the program run round now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.