Programa para comprobar Array Involutiva

Dada una array y la tarea es verificar que la array sea array involutiva o no. 
Array involutiva : Se dice que una array es array involutiva si la array multiplicada por sí misma devuelve la array identidad. La array involutiva es la array que es su propia inversa. Se dice que la array A es array involutiva si A * A = I . Donde I es la array identidad. 
 

Involutory-Matrix

Ejemplos: 

Input : mat[N][N] = {{1, 0, 0},
                     {0, -1, 0},
                     {0, 0, -1}}
Output : Involutory Matrix

Input : mat[N][N] = {{1, 0, 0},
                     {0, 1, 0},
                     {0, 0, 1}} 
Output : Involutory Matrix

C++

// Program to implement involutory matrix.
#include <bits/stdc++.h>
#define N 3
using namespace std;
 
// Function for matrix multiplication.
void multiply(int mat[][N], int res[][N])
{
    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 involutory matrix.
bool InvolutoryMatrix(int mat[N][N])
{
    int res[N][N];
 
    // multiply function call.
    multiply(mat, res);
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i == j && res[i][j] != 1)
                return false;
            if (i != j && res[i][j] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver function.
int main()
{
    int mat[N][N] = { { 1, 0, 0 },
                      { 0, -1, 0 },
                      { 0, 0, -1 } };
 
    // Function call. If function return
    // true then if part will execute otherwise
    // else part will execute.
    if (InvolutoryMatrix(mat))
        cout << "Involutory Matrix";
    else
        cout << "Not Involutory Matrix";
 
    return 0;
}

Java

// Java  Program to implement
// involutory matrix.
import java.io.*;
 
class GFG {
     
    static int N = 3;
     
    // Function for matrix multiplication.
    static void multiply(int mat[][], int res[][])
    {
        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 involutory matrix.
    static boolean InvolutoryMatrix(int mat[][])
    {
        int res[][] = new int[N][N];
     
        // multiply function call.
        multiply(mat, res);
     
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (i == j && res[i][j] != 1)
                    return false;
                if (i != j && res[i][j] != 0)
                    return false;
            }
        }
        return true;
    }
     
    // Driver function.
    public static void main (String[] args)
    {
         
        int mat[][] = { { 1, 0, 0 },
                        { 0, -1, 0 },
                        { 0, 0, -1 } };
     
        // Function call. If function return
        // true then if part will execute
        // otherwise else part will execute.
        if (InvolutoryMatrix(mat))
            System.out.println ( "Involutory Matrix");
        else
            System.out.println ( "Not Involutory Matrix");
     
             
    }
}
 
// This code is contributed by vt_m

Python3

# Program to implement involutory matrix.
N = 3;
 
# Function for matrix multiplication.
def multiply(mat, res):
 
    for i in range(N):
        for j in range(N):
            res[i][j] = 0;
            for k in range(N):
                res[i][j] += mat[i][k] * mat[k][j];
    return res;
 
# Function to check involutory matrix.
def InvolutoryMatrix(mat):
 
    res=[[0 for i in range(N)]
            for j in range(N)];
 
    # multiply function call.
    res = multiply(mat, res);
 
    for i in range(N):
        for j in range(N):
            if (i == j and res[i][j] != 1):
                return False;
            if (i != j and res[i][j] != 0):
                return False;
    return True;
 
# Driver Code
mat = [[1, 0, 0], [0, -1, 0], [0, 0, -1]];
 
# Function call. If function
# return true then if part
# will execute otherwise
# else part will execute.
if (InvolutoryMatrix(mat)):
    print("Involutory Matrix");
else:
    print("Not Involutory Matrix");
 
# This code is contributed by mits

C#

// C# Program to implement
// involutory matrix.
using System;
 
class GFG {
     
    static int N = 3;
     
    // Function for matrix multiplication.
    static void multiply(int [,]mat, int [,]res)
    {
        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 involutory matrix.
    static bool InvolutoryMatrix(int [,]mat)
    {
        int [,]res = new int[N,N];
     
        // multiply function call.
        multiply(mat, res);
     
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (i == j && res[i,j] != 1)
                    return false;
                if (i != j && res[i,j] != 0)
                    return false;
            }
        }
        return true;
    }
     
    // Driver function.
    public static void Main ()
    {
         
        int [,]mat = { { 1, 0, 0 },
                        { 0, -1, 0 },
                        { 0, 0, -1 } };
     
        // Function call. If function return
        // true then if part will execute
        // otherwise else part will execute.
        if (InvolutoryMatrix(mat))
            Console.WriteLine( "Involutory Matrix");
        else
            Console.WriteLine( "Not Involutory Matrix");
     
             
    }
}
 
// This code is contributed by vt_m

PHP

<?php
// Program to implement
// involutory matrix.
 
$N = 3;
 
// Function for matrix
// multiplication.
function multiply($mat, $res)
{
    global $N;
    for ($i = 0; $i < $N; $i++)
    {
        for ($j = 0; $j < $N; $j++)
        {
            $res[$i][$j] = 0;
            for ($k = 0; $k < $N; $k++)
                $res[$i][$j] += $mat[$i][$k] *
                                $mat[$k][$j];
        }
    }
    return $res;
}
 
// Function to check
// involutory matrix.
function InvolutoryMatrix($mat)
{
    global $N;
    $res;
    for ($i = 0; $i < $N; $i++)
        for ($j = 0; $j < $N; $j++)
            $res[$i][$j] = 0;
 
    // multiply function call.
    $res = multiply($mat, $res);
 
    for ($i = 0; $i < $N; $i++)
    {
        for ($j = 0; $j < $N; $j++)
        {
            if ($i == $j &&
                $res[$i][$j] != 1)
                return false;
            if ($i != $j &&
                $res[$i][$j] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver Code
$mat = array(array(1, 0, 0),
             array(0, -1, 0),
             array(0, 0, -1));
 
// Function call. If function
// return true then if part
// will execute otherwise
// else part will execute.
if (InvolutoryMatrix($mat))
    echo "Involutory Matrix";
else
    echo "Not Involutory Matrix";
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript to implement involutory matrix.
var N = 3;
 
// Function for matrix multiplication.
function multiply(mat, res)
{
    for(var i = 0; i < N; i++)
    {
        for(var j = 0; j < N; j++)
        {
            res[i][j] = 0;
            for(var k = 0; k < N; k++)
                res[i][j] += mat[i][k] * mat[k][j];
        }
    }
}
 
// Function to check involutory matrix.
function InvolutoryMatrix(mat)
{
    var res = Array(N).fill(0).map(
        x => Array(N).fill(0));
 
    // Multiply function call.
    multiply(mat, res);
 
    for(var i = 0; i < N; i++)
    {
        for(var j = 0; j < N; j++)
        {
            if (i == j && res[i][j] != 1)
                return false;
            if (i != j && res[i][j] != 0)
                return false;
        }
    }
    return true;
}
     
// Driver code
var mat = [ [ 1, 0, 0 ],
            [ 0, -1, 0 ],
            [ 0, 0, -1 ] ];
 
// Function call. If function return
// true then if part will execute
// otherwise else part will execute.
if (InvolutoryMatrix(mat))
    document.write("Involutory Matrix");
else
    document.write("Not Involutory Matrix");
 
// This code is contributed by 29AjayKumar
 
</script>

Producción : 

Involutory Matrix

Complejidad de Tiempo: O(n 3 )
Espacio Auxiliar: O(n 2 )
 

Publicación traducida automáticamente

Artículo escrito por Dharmendra_Kumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *