Adjoint of Matrix in C++. (Inverse of Matrix)

Viewed 56

I am Implementing a Inverse Matrix by using the following formula

Inverse of A = (Adj of A) / (Determinant of A)

I am implementing Adjoint Matrix, But When I printing the result of adjoint function , the result is printed in this way.

-23 -20 34
19 4 -14
-8 4 4

But it should be In the following order

-23 19 -8
-20 4 4 
34 -14 4

Here, rows became the column. But when I debug the code I found this

[0]:{...}
  [0] -23
  [1] 19
  [2] -8
[1]:{...}
  [0] -20
  [1]  4
  [2]  4
[2]:{...}
  [0] 34
  [1] -14
  [2] -4

According to debugger result, is the result is correct??? I am not getting why the result is printing in that way. Here Is my code

#include<iostream>
#include<vector> 

/* Function call for to get Cofactor of Matrix */
void getCofactor(std:: vector< std :: vector <int >> &vec, std:: vector< std :: vector <int >> &temp, int p, int q){
    //std::vector<std::vector<int>> temp(3, std::vector<int>(3));
    int i =0;
    int j = 0;
    for(int row =0; row< vec.size(); row++){
        for (int col = 0; col < vec.size(); col++)
        {
            //copying into temporary matix only those element which are not given row and column
            if(row != p && col != q){
                
                temp[i][j++] = vec[row][col];
            }

            if (j == vec.size() - 1) {
                    j = 0;
                    i++;
            }
        }

    }

}

int determinent (std:: vector< std :: vector <int >> &vec, int n){
    int D = 0; // Initializa the result 
    // Base case: if the matrix contains single element
    if(n==1){
        return vec[0][0];
    }
    std::vector<std::vector<int>> temp(3, std::vector<int>(3));
    int sign = 1;
    for(int f = 0; f<vec.size(); f++){
        //get cofactor of vec[0][f]
        getCofactor(vec, temp, 0,  f);
        D += sign * vec[0][f] * determinent(temp, n-1);

        //terms are to be added with alternate sign
        sign = -sign;
    }
    return D;
}

/* Function Call for Adjoint Matrix */
void adjoint(std:: vector< std :: vector <int >> &vec, std:: vector< std :: vector <int >> &adj){
    //std::vector<std::vector<int>> adj(vec.size(), std::vector<int>(vec.size()));
    if (vec.size() == 1){
        adj[0][0] = 1;
        return;
    }
    std::vector<std::vector<int>> temp(vec.size(), std::vector<int>(vec.size()));
    int sign = 1;
    for (int i = 0; i < vec.size(); i++){
        for (int j = 0; j < vec.size(); j++){
            //Get Co-factor of the Matrix[i][j]
            getCofactor(vec,temp, i, j);
            //Sign of adj[i][j] positive if sum of row and column indexs is even
            sign = ((i + j) % 2 == 0) ? 1 : -1;
            // Interchanging rows and columns to get the transpose of the cofector Matrix
            adj[j][i] = (sign) * (determinent(temp, vec.size() - 1));
            std:: cout <<adj[j][i]<<" ";
        }
        std::cout<<std::endl;
    }  
}

/* Display Function */
void display (std::vector<std::vector<int>> vec){
     for(int i = 0; i < vec.size(); i++){
        for (int j = 0; j < vec.size(); j++) {
            std :: cout<< vec[i][j]<<" ";
        } 
        std:: cout << std::endl;  
    }
}

int main(){
    
    std :: vector < std :: vector< int > > matrix = {{2,1,3},{6,5,7},{4,9,8}}; // Initialize the vector
    std::vector<std::vector<int>> Ad(matrix.size(), std::vector<int>(matrix.size()));
    std :: cout << "The Given Matrix is: "<<std :: endl;
    display(matrix);
    std::cout<<std::endl;
    std::cout<<"The Adjacent Matrix is: "<< std::endl; 
    adjoint(matrix, Ad);
    display(Ad); 
    return 0;
}

How can I solve this? The display function is not printing result of adjoint function. How can I print the result of adjoint function through display function

1 Answers

Your code works fine. if you comment out the debug print calls to std::cout in void adjoint(), the unwanted output disappears.

void adjoint(std:: vector< std :: vector <int >> &vec, std:: vector< std :: vector <int >> &adj){
    //std::vector<std::vector<int>> adj(vec.size(), std::vector<int>(vec.size()));
    if (vec.size() == 1){
        adj[0][0] = 1;
        return;
    }
    std::vector<std::vector<int>> temp(vec.size(), std::vector<int>(vec.size()));
    int sign = 1;
    for (int i = 0; i < vec.size(); i++){
        for (int j = 0; j < vec.size(); j++){
            //Get Co-factor of the Matrix[i][j]
            getCofactor(vec,temp, i, j);
            //Sign of adj[i][j] positive if sum of row and column indexs is even
            sign = ((i + j) % 2 == 0) ? 1 : -1;
            // Interchanging rows and columns to get the transpose of the cofector Matrix
            adj[j][i] = (sign) * (determinent(temp, vec.size() - 1));

            // Comment this out:
            std:: cout <<adj[j][i]<<" ";
        }
        // Comment this out:
        std::cout<<std::endl;
    }  
}

That's it.

Execution output:

Program returned: 0
Program stdout

The Given Matrix is: 
2 1 3 
6 5 7 
4 9 8 

The Adjacent Matrix is: 
-23 19 -8 
-20 4 4 
34 -14 4 

You can run and test your corrected code here:

https://godbolt.org/z/PzqKMsrsv

No more comments.

Related