Using the lower triangularization method calculate the determinant of a transposed matrix

Asked

Viewed 799 times

4

Using the lower triangularization method, I have to calculate the determinant of a transposed matrix.

I want, from the transposed matrix, to calculate the determinant using the inferior triangularization method, because the determinant has to be equal to the determinant of the original matrix.

The transposed matrix is already in the code, I’m not getting is to triangulate and then calculate the determinant.

Follows the code:

#include <stdio.h> 

#define linhas 2
#define colunas 2

void main(){

    double mat[linhas][colunas] = { 1, 4, 7, 3 };
    // double mat[linhas][colunas] = { {2, -4, 8}, {5, 4, 6}, {-3, 0, 2} };
    // double mat[linhas][colunas] = { 1, 4, 7, 3, 2, 6, -3, 0, 2, 1, 3, 6, 8, -2, 5, -1 };
    // double mat[linhas][colunas] = { 1, 4, 7, 3, 2, 6, -3, 0, 2, 1, 3, 6, 8, -2, 5, -1, 0, 1, -7, -5, 4, 6, -10.5, 0, -5 };

    double det=1, mult, aux;

    int i, j, pivo; //indices para efetuar modificações na matriz
    int a, b; //indices apenas para debugar a modificação da matriz
    int r, l, inicio;
    int p, c;

    printf("\nMatriz antes do cálculo\n");
    for(a=0; a<linhas; a++){
        for(b=0; b<colunas; b++)
        printf("%.2f\t", mat[a][b]);
    printf("\n");
}
    printf("\n");

    aux= mat[linhas][colunas];
    mat[linhas][colunas] = mat[colunas][linhas];
    mat[colunas][linhas] = aux;
    printf("\nMatriz transposta é\n");
    for(a=0; a<colunas; a++){
        for(b=0; b<linhas; b++)
    printf("%.2f\t",mat[b][a]);
    printf("\n");
}
    printf("\n");

    //Triangulizando a matriz
    for(pivo=0; pivo>linhas; pivo--){

    for(i=pivo-1; i>linhas; i--){

    mult=(-1)*mat[i][pivo]/mat[pivo][pivo];

    for(j=0; j>colunas; j--){
        mat[i][j] = mat[i][j] + (mult*mat[pivo][j]);
        }//fim for j

    printf("Matriz alterada com pivo=%d, i=%d\n", pivo,i);
    for(a=0; a<linhas; a++){
        for(b=0; b<colunas; b++)
        printf("%.2f\t", mat[a][b]);
        printf("\n");
}//fim for a

}//fim for i
}//fim for pivo
//fim da triangulização da matriz. Pronto para cálculo do determinante



   //calculando determinante
    for(i=0; i<linhas; i++){
        printf("mat[%d][%d] = %.2f\n",i , i, mat[i][i]);
        det=det*mat[i][i];
    }//fim cálculo do determinante
    printf("\nDeterminante = %.2f\n", det);

    printf("\nMatriz após o cálculo\n");
    for(a=0; a<linhas; a++){
        for(b=0; b<colunas; b++)
        printf("%.2f\t", mat[a][b]);
    printf("\n");
    }//fim for a
    printf("\n");

}
  • 1

    Apparently you are new here, so to begin with, whenever you ask a question, you need to detail your doubt and what is really bothering or disturbing you, so we can help you :). Be direct, don’t bullshit, but tell me everything you can collaborate with for an answer that will help you.

  • Hello Alan, I’m not getting to understand your problem, I suggest you edit your question and add more information to it.

  • Take a look at other questions to get an idea of how to proceed in a query. Something that can also help you is the area of help from stackoverflow.

  • So what I’m looking for is, from the transposed matrix to calculate the determinant using the lower triangularization method, because the determinant has to be equal to the determinant of the original matrix, the transposed matrix is already in the code, I’m not getting is to triangulate and then calculate the determinant.

1 answer

1


1. First minor corrections and improvements

First of all, look at this:

double mat[linhas][colunas] = { 1, 4, 7, 3 };

This is not a 2x2 matrix! This is a 4 position vector. What you wanted was this:

double mat[linhas][colunas] = { {1, 4}, {7, 3} };

You can also simplify that:

det=det*mat[i][i];

For that reason:

det *= mat[i][i];

And that:

mat[i][j] = mat[i][j] + (mult*mat[pivo][j]);

For that reason:

mat[i][j] += mult * mat[pivo][j];

Let’s take a closer look at this:

2. The error in the triangulation

Let’s take a look at this snippet of your code:

    for(pivo=0; pivo>linhas; pivo--){

    for(i=pivo-1; i>linhas; i--){

    mult=(-1)*mat[i][pivo]/mat[pivo][pivo];

    for(j=0; j>colunas; j--){

Observe the condition of your three fors are totally wrong! pivo>linhas, i>linhas and j>colunas are conditions that will always be false. I think what you wanted was this:

    for (pivo = linhas - 1; pivo >= 0; pivo--) {

        for (i = pivo - 1; i >= 0; i--) {

            mult = (-1) * mat[i][pivo] / mat[pivo][pivo];

            for (j = colunas - 1; j >= 0; j--) {
                mat[i][j] += mult * mat[pivo][j];
            } // Fim do for j.

3. Transposition which does nothing

And also, watch this:

    aux = mat[linhas][colunas];
    mat[linhas][colunas] = mat[colunas][linhas];
    mat[colunas][linhas] = aux;

Since the matrix is square, then we will always have to linhas = colunas. Supposing linhas = colunas = N, for some whole value N, we’ll have it:

    aux = mat[N][N];
    mat[N][N] = mat[N][N];
    mat[N][N] = aux;

The middle line is completely unnecessary, so this is reduced to:

    aux = mat[N][N];
    mat[N][N] = aux;

And that effectively does nothing! Therefore, it can be eliminated. By eliminating this, the variable aux is no longer needed.

There is one more however in this section. In an Nxn matrix, the row and column indexes range from 0 to 0 N-1, and therefore access mat[N][N] is to access a memory region outside the matrix. Thankfully that part of the code can simply be deleted.

4. The revised code

Made the above corrections, we will re-code your code and delete the variables that are not used (aux, r, l, inicio, p and c). I’m also moving det and mult to be declared only when they will be needed. Since only square matrices have meaning, then we can unify linhas and colunas in one thing, call tamanho. I also take the opportunity to do main return int instead of void. And finally, your whole revised code goes like this:

#include <stdio.h> 

#define tamanho 2

int main() {

    double mat[tamanho][tamanho] = { {1, 4}, {7, 3} };
    // double mat[tamanho][tamanho] = { {2, -4, 8}, {5, 4, 6}, {-3, 0, 2} };
    // double mat[tamanho][tamanho] = { {1, 4, 7, 3}, {2, 6, -3, 0}, {2, 1, 3, 6}, {8, -2, 5, -1} };
    // double mat[tamanho][tamanho] = { {1, 4, 7, 3, 2}, {6, -3, 0, 2, 1}, {3, 6, 8, -2, 5}, {-1, 0, 1, -7, -5}, {4, 6, -10.5, 0, -5} };

    int i, j, pivo; // Índices para efetuar modificações na matriz.
    int a, b; // Índices apenas para debugar a modificaçõo da matriz.

    printf("\nMatriz antes do cálculo\n");
    for (a = 0; a < tamanho; a++) {
        for (b = 0; b < tamanho; b++) {
            printf("%.2f\t", mat[a][b]);
        }
        printf("\n");
    }
    printf("\n");

    printf("\nMatriz transposta é\n");
    for (a = 0; a < tamanho; a++) {
        for (b = 0; b < tamanho; b++) {
            printf("%.2f\t", mat[b][a]);
        }
        printf("\n");
    }
    printf("\n");

    // Triangularizando a matriz.
    for (pivo = tamanho - 1; pivo >= 0; pivo--) {

        for (i = pivo - 1; i >= 0; i--) {

            double mult = (-1) * mat[i][pivo] / mat[pivo][pivo];

            for (j = tamanho - 1; j >= 0; j--) {
                mat[i][j] += mult * mat[pivo][j];
            } // Fim do for j.

            printf("Matriz alterada com pivo=%d, i=%d\n", pivo, i);
            for (a = 0; a < tamanho; a++) {
                for (b = 0; b < tamanho; b++) {
                    printf("%.2f\t", mat[a][b]);
                }
                printf("\n");
            } // Fim do for a.

        } // Fim do for i.
    } // Fim do for pivo.
    // Fim da triangularização da matriz. Pronto para o cálculo do determinante.

    // Calculando determinante.
    double det = 1;
    for (i = 0; i < tamanho; i++) {
        printf("mat[%d][%d] = %.2f\n", i, i, mat[i][i]);
        det *= mat[i][i];
    } // Fim do cálculo do determinante.
    printf("\nDeterminante = %.2f\n", det);

    printf("\nMatriz após o cálculo\n");
    for (a = 0; a < tamanho; a++) {
        for (b = 0; b < tamanho; b++) {
            printf("%.2f\t", mat[a][b]);
        }
        printf("\n");
    } // Fim do for a.
    printf("\n");

    return 0;
}

5. Testing the code

And then, here’s the result for the 2x2 matrix:

Matriz antes do cálculo
1.00    4.00    
7.00    3.00    


Matriz transposta é
1.00    7.00    
4.00    3.00    

Matriz alterada com pivo=1, i=0
-8.33   0.00    
7.00    3.00    
mat[0][0] = -8.33
mat[1][1] = 3.00

Determinante = -25.00

Matriz após o cálculo
-8.33   0.00    
7.00    3.00    

For the 3x3 matrix:

Matriz antes do cálculo
2.00    -4.00   8.00    
5.00    4.00    6.00    
-3.00   0.00    2.00    


Matriz transposta é
2.00    5.00    -3.00   
-4.00   4.00    0.00    
8.00    6.00    2.00    

Matriz alterada com pivo=2, i=1
2.00    -4.00   8.00    
14.00   4.00    0.00    
-3.00   0.00    2.00    
Matriz alterada com pivo=2, i=0
14.00   -4.00   0.00    
14.00   4.00    0.00    
-3.00   0.00    2.00    
Matriz alterada com pivo=1, i=0
28.00   0.00    0.00    
14.00   4.00    0.00    
-3.00   0.00    2.00    
mat[0][0] = 28.00
mat[1][1] = 4.00
mat[2][2] = 2.00

Determinante = 224.00

Matriz após o cálculo
28.00   0.00    0.00    
14.00   4.00    0.00    
-3.00   0.00    2.00    

For the 4x4 matrix:

Matriz antes do cálculo
1.00    4.00    7.00    3.00    
2.00    6.00    -3.00   0.00    
2.00    1.00    3.00    6.00    
8.00    -2.00   5.00    -1.00   


Matriz transposta é
1.00    2.00    2.00    8.00    
4.00    6.00    1.00    -2.00   
7.00    -3.00   3.00    5.00    
3.00    0.00    6.00    -1.00   

Matriz alterada com pivo=3, i=2
1.00    4.00    7.00    3.00    
2.00    6.00    -3.00   0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   
Matriz alterada com pivo=3, i=1
1.00    4.00    7.00    3.00    
2.00    6.00    -3.00   0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   
Matriz alterada com pivo=3, i=0
25.00   -2.00   22.00   0.00    
2.00    6.00    -3.00   0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   
Matriz alterada com pivo=2, i=1
25.00   -2.00   22.00   0.00    
6.55    5.00    0.00    0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   
Matriz alterada com pivo=2, i=0
-8.33   5.33    0.00    0.00    
6.55    5.00    0.00    0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   
Matriz alterada com pivo=1, i=0
-15.32  -0.00   0.00    0.00    
6.55    5.00    0.00    0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   
mat[0][0] = -15.32
mat[1][1] = 5.00
mat[2][2] = 33.00
mat[3][3] = -1.00

Determinante = 2527.00

Matriz após o cálculo
-15.32  -0.00   0.00    0.00    
6.55    5.00    0.00    0.00    
50.00   -11.00  33.00   0.00    
8.00    -2.00   5.00    -1.00   

For the 5x5 matrix:

Matriz antes do cálculo
1.00    4.00    7.00    3.00    2.00    
6.00    -3.00   0.00    2.00    1.00    
3.00    6.00    8.00    -2.00   5.00    
-1.00   0.00    1.00    -7.00   -5.00   
4.00    6.00    -10.50  0.00    -5.00   


Matriz transposta é
1.00    6.00    3.00    -1.00   4.00    
4.00    -3.00   6.00    0.00    6.00    
7.00    0.00    8.00    1.00    -10.50  
3.00    2.00    -2.00   -7.00   0.00    
2.00    1.00    5.00    -5.00   -5.00   

Matriz alterada com pivo=4, i=3
1.00    4.00    7.00    3.00    2.00    
6.00    -3.00   0.00    2.00    1.00    
3.00    6.00    8.00    -2.00   5.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=4, i=2
1.00    4.00    7.00    3.00    2.00    
6.00    -3.00   0.00    2.00    1.00    
7.00    12.00   -2.50   -2.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=4, i=1
1.00    4.00    7.00    3.00    2.00    
6.80    -1.80   -2.10   2.00    -0.00   
7.00    12.00   -2.50   -2.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=4, i=0
2.60    6.40    2.80    3.00    -0.00   
6.80    -1.80   -2.10   2.00    -0.00   
7.00    12.00   -2.50   -2.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=3, i=2
2.60    6.40    2.80    3.00    -0.00   
6.80    -1.80   -2.10   2.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=3, i=1
2.60    6.40    2.80    3.00    -0.00   
5.37    -3.51   1.19    0.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=3, i=0
0.46    3.83    7.73    0.00    -0.00   
5.37    -3.51   1.19    0.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=2, i=1
0.46    3.83    7.73    0.00    -0.00   
7.10    -0.70   -0.00   0.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=2, i=0
11.72   22.15   -0.00   0.00    -0.00   
7.10    -0.70   -0.00   0.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
Matriz alterada com pivo=1, i=0
235.14  0.00    -0.00   0.00    -0.00   
7.10    -0.70   -0.00   0.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   
mat[0][0] = 235.14
mat[1][1] = -0.70
mat[2][2] = -5.79
mat[3][3] = -7.00
mat[4][4] = -5.00

Determinante = 33507.50

Matriz após o cálculo
235.14  0.00    -0.00   0.00    -0.00   
7.10    -0.70   -0.00   0.00    -0.00   
8.43    13.71   -5.79   -0.00   0.00    
-5.00   -6.00   11.50   -7.00   0.00    
4.00    6.00    -10.50  0.00    -5.00   

See here working on ideone.

6. Division by zero

Ah, to yet one last detail to see. Note this line in the triangularization process:

double mult = (-1) * mat[i][pivo] / mat[pivo][pivo];

What happens if there is a zero in one of the main diagonal elements? The result is that you will get stuck trying to divide by zero!

To resolve this, I suggest that before making a step in the triangulation, that if mat[pivo][pivo] is zero, that you permute between two rows or two columns in order to try to never leave zeros on the main diagonal. Also remember that each time you make such a permutation, the determinant sign reverses.

If it is not possible to take any zero from the main diagonal by exchanging two rows or two columns, then that means there is a row or a column with only zeros. And if that’s the case, then you can stop the triangular calculations because the resulting determinant will always be zero.

Browser other questions tagged

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