Open In App

Program to check idempotent matrix

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a square matrix mat[][] of order n*n, the task is to check if it is an Idempotent Matrix or not.

Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the same matrix, i.e. the matrix mat[][] is said to be an idempotent matrix if and only if M * M = M.

Examples: 

Input: mat[][] = [[2, -2, -4],
[-1, 3, 4],
[1, -2, 3]]

Output: True
Explanation: Given matrix if multiplied by itself returns the same matrix.

Idempotent-Matrix


Input: mat[][] = [[1, 2],
[3, 4]]
Output: False
Explanation: Given matrix if multiplied by itself returns the different matrix.

Approach:

The idea is to multiply matrix mat[][] by itself and check if all the elements of resultant matrix res[][] is equal to corresponding elements of mat[][]. Firstly multiply matrix mat[][] by itself using Matrix Multiplication and store the result in matrix res[][]. Then compare if both the matrices are equal or not.

C++
// Program to check given matrix 
// is idempotent matrix or not.
#include<bits/stdc++.h>
using namespace std;

// Function for matrix multiplication.
void mulMatrix(vector<vector<int>>& mat,  
            vector<vector<int>>& res) {
    int n = mat.size();
  
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                res[i][j] += mat[i][k] * mat[k][j];
            }
        }
    }
}

// Function to check idempotent
// property of matrix.
bool checkIdempotent(vector<vector<int>>& mat) {   
    int n = mat.size();

    // multiply matrix mat by iself
    // and store it into res.
    vector<vector<int>> res(n, vector<int>(n, 0));
    mulMatrix(mat, res);

    // check if all the elements of 
    // res are same as mat.
    for (int i = 0; i < n; i++)    
        for (int j = 0; j < n; j++)        
            if (mat[i][j] != res[i][j])
                return false;
    return true;
}

int main() {
    vector<vector<int>> mat = {{2, -2, -4},
                    {-1, 3, 4},
                    {1, -2, -3}};
    
    if (checkIdempotent(mat))
        cout << "True";
    else
        cout << "False";
    return 0;
}
Java
// Java program to check given matrix 
// is idempotent matrix or not.
import java.io.*;

class GfG {
    
    // Function for matrix multiplication.
    static void mulMatrix(int[][] mat, int[][] res) {
        int n = mat.length;

        // Perform matrix multiplication
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                res[i][j] = 0;
                for (int k = 0; k < n; k++) {
                    res[i][j] += mat[i][k] * mat[k][j];
                }
            }
        }
    }
    
    // Function to check idempotent
    // property of matrix.
    static boolean checkIdempotent(int mat[][]) { 
        int n = mat.length;

        // Calculate multiplication of matrix
        // with itself and store it into res.
        int res[][] = new int[n][n];
        mulMatrix(mat, res);
    
        for (int i = 0; i < n; i++) { 
            for (int j = 0; j < n; j++) {
                if (mat[i][j] != res[i][j])
                    return false;
            }
        }
        return true;
    }

    public static void main (String[] args) {
        int mat[][] = {{2, -2, -4},
                       {-1, 3, 4},
                       {1, -2, -3}};
    
        if (checkIdempotent(mat))
            System.out.println("True");
        else
            System.out.println("False");
        
    }
}
Python
# Python Program to check given matrix 
# is idempotent matrix or not.
import math

# Function for matrix multiplication.
def mulMatrix(mat, res):
    n = len(mat)

    # Perform matrix multiplication
    for i in range(n):
        for j in range(n):
            for k in range(n):
                res[i][j] += mat[i][k] * mat[k][j]

# Function to check idempotent
# property of matrix.
def checkIdempotent(mat):
    n = len(mat)

    # Calculate multiplication of matrix
    # with itself and store it into res.
    res =[[0]*n for i in range(0,n)]
    mulMatrix(mat, res)

    for i in range(0,n):
        for j in range(0,n):     
            if (mat[i][j] != res[i][j]):
                return False
    return True

mat = [ [2, -2, -4],
        [-1, 3, 4],
        [1, -2, -3] ]

if (checkIdempotent(mat)):
    print("True")
else:
    print("False")
C#
// C# program to check given matrix 
// is idempotent matrix or not.
using System;

class GfG {

    // Function for matrix multiplication.
    static void mulMatrix(int[,] mat, int[,] res) {
        int n = mat.GetLength(0);

        // Perform matrix multiplication
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
              	res[i, j] = 0;
                for (int k = 0; k < n; k++) {
                    res[i, j] += mat[i, k] * mat[k, j];
                }
            }
        }
    }
    
    // Function to check idempotent
    // property of matrix.
    static bool checkIdempotent(int[,] mat){ 
        int n = mat.GetLength(0);

        // Calculate multiplication of matrix
        // with itself and store it into res.
        int [,]res = new int[n,n];
        mulMatrix(mat, res);
    
        for (int i = 0; i < n; i++) { 
            for (int j = 0; j < n; j++) {
                if (mat[i,j] != res[i,j])
                    return false;
            }
        }
        return true;
    }

    static void Main () {
        int [,]mat = {{2, -2, -4},
                    {-1, 3, 4},
                    {1, -2, -3}};
    
        if (checkIdempotent(mat))
            Console.WriteLine( "True");
        else
            Console.WriteLine("False");
        
    }
}
JavaScript
// Javascript program to check given matrix 
// is idempotent matrix or not.

// Function for matrix multiplication.
function mulMatrix(mat, res) {
    const n = mat.length;

    // Perform matrix multiplication
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            for (let k = 0; k < n; k++) {
                res[i][j] += mat[i][k] * mat[k][j];
            }
        }
    }
}

// Function to check idempotent
// property of matrix.
function checkIdempotent(mat) { 

    const n = mat.length;

    // Calculate multiplication of matrix
    // with itself and store it into res.
    const res = Array.from(Array(n), ()=>Array(n).fill(0));
    mulMatrix(mat, res);

    for (let i = 0; i < n; i++) { 
        for (let j = 0; j < n; j++) {
            if (mat[i][j] != res[i][j])
                return false;
        }
    }
    return true;
}

//driver code
const mat = [[2, -2, -4],
            [-1, 3, 4],
            [1, -2, -3]];
            
if (checkIdempotent(mat))
    console.log( "True");
else
    console.log("False");

Output
True

Time Complexity: O(n3)
Auxiliary Space: O(n2), since n2 extra space has been taken to store the result of multiplication.



Next Article
Article Tags :
Practice Tags :

Similar Reads