📜  身份矩阵程序

📅  最后修改于: 2021-05-07 01:18:05             🧑  作者: Mango

身份矩阵简介:

单位矩阵的字典定义是一个方矩阵,其中主或对角线的所有元素均为1,所有其他元素为零。在下图中,每个矩阵都是一个Identity Matrix。

在线性代数中,有时将其称为单位矩阵,即方矩阵(大小= nxn),其中主对角线为1,其他地方为零。单位矩阵由“ I ”表示。有时,U或E也用于表示身份矩阵。
单位矩阵的一个特性是,如果将其乘以单位矩阵,则矩阵将保持不变。
例子:

Input  : 2
Output : 1 0
         0 1

Input :  4
Output : 1 0 0 0
         0 1 0 0
         0 0 1 0
         0 0 0 1
The explanation is simple. We need to make all
the elements of principal or main diagonal as 
1 and everything else as 0.

程序打印身份矩阵:
逻辑很简单。您需要在行等于矩阵列的那些位置打印1,并将所有其他位置设为0。

C++
// C++ program to print Identity Matrix
#include
using namespace std;
 
int Identity(int num)
{
    int row, col;
     
    for (row = 0; row < num; row++)
    {
        for (col = 0; col < num; col++)
        {
            // Checking if row is equal to column
            if (row == col)
                cout << 1 << " ";
            else
                cout << 0 << " ";
        }
        cout << endl;
    }
    return 0;
}
 
// Driver Code
int main()
{
    int size = 5;
    Identity(size);
    return 0;
}
 
// This code is contributed by shubhamsingh10


C
// C program to print Identity Matrix
#include
 
int Identity(int num)
{
    int row, col;
     
    for (row = 0; row < num; row++)
    {
        for (col = 0; col < num; col++)
        {
            // Checking if row is equal to column
            if (row == col)
                printf("%d ", 1);
            else
                printf("%d ", 0);
        }
        printf("\n");
    }
    return 0;
}
 
// Driver Code
int main()
{
    int size = 5;
    identity(size);
    return 0;
}


Java
// Java program to print Identity Matrix
class GFG {
     
    static int identity(int num)
    {
        int row, col;
          
        for (row = 0; row < num; row++)
        {
            for (col = 0; col < num; col++)
            {
                // Checking if row is equal to column
                if (row == col)
                    System.out.print( 1+" ");
                else
                    System.out.print( 0+" ");
            }
            System.out.println();
        }
        return 0;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int size = 5;
        identity(size);
    }
}
 
/*This code is contributed by Nikita tiwari.*/


Python3
# Python code to print identity matrix
 
# Function to print identity matrix
def Identity(size):
    for row in range(0, size):
        for col in range(0, size):
 
            # Here end is used to stay in same line
            if (row == col):
                print("1 ", end=" ")
            else:
                print("0 ", end=" ")
        print()
 
# Driver Code       
size = 5
Identity(size)


C#
// C# program to print Identity Matrix
using System;
 
class GFG {
     
    static int identity(int num)
    {
        int row, col;
         
        for (row = 0; row < num; row++)
        {
            for (col = 0; col < num; col++)
            {
                // Checking if row is equal to column
                if (row == col)
                    Console.Write( 1+" ");
                else
                    Console.Write( 0+" ");
            }
            Console.WriteLine();
        }
        return 0;
    }
     
    // Driver Code
    public static void Main()
    {
        int size = 5;
        identity(size);
    }
}
 
/*This code is contributed by vt_m.*/


PHP


Javascript


C++
// CPP program to check if a given matrix is identity
#include
using namespace std;
 
const int MAX = 100;
 
bool isIdentity(int mat[][MAX], int N)
{
    for (int row = 0; row < N; row++)
    {
        for (int col = 0; col < N; col++)
        {
            if (row == col && mat[row][col] != 1)
                return false;
            else if (row != col && mat[row][col] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver Code
int main()
{
    int N = 4;
    int mat[][MAX] = {{1, 0, 0, 0},
                    {0, 1, 0, 0},
                    {0, 0, 1, 0},
                    {0, 0, 0, 1}};
    if (isIdentity(mat, N))
       cout << "Yes ";
    else
       cout << "No ";
    return 0;
}


Java
// Java program to check if a given
// matrix is identity
class GFG {
     
    int MAX = 100;
  
    static boolean isIdentity(int mat[][], int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row][col] != 1)
                    return false;
                else if (row != col && mat[row][col] != 0)
                    return false;
            }
        }
        return true;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int N = 4;
        int mat[][] = {{1, 0, 0, 0},
                       {0, 1, 0, 0},
                       {0, 0, 1, 0},
                       {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
           System.out.println("Yes ");
        else
           System.out.println("No ");
    }
}
 
 
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python3 program to check
# if a given matrix is identity
MAX = 100;
def isIdentity(mat, N):
    for row in range(N):
        for col in range(N):
            if (row == col and
                mat[row][col] != 1):
                return False;
            elif (row != col and
                  mat[row][col] != 0):
                return False;
    return True;
 
# Driver Code
N = 4;
mat = [[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]];
if (isIdentity(mat, N)):
    print("Yes ");
else:
    print("No ");
 
# This code is contributed
# by mits


C#
// C# program to check if a given
// matrix is identity
using System;
 
class GFG {
     
    //int MAX = 100;
 
    static bool isIdentity(int[,] mat, int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row,col] != 1)
                    return false;
                else if (row != col && mat[row,col] != 0)
                    return false;
            }
        }
        return true;
    }
     
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int [,]mat = {{1, 0, 0, 0},
                      {0, 1, 0, 0},
                      {0, 0, 1, 0},
                      {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
        Console.WriteLine("Yes ");
        else
        Console.WriteLine("No ");
    }
}
 
 
/*This code is contributed by vt_m.*/


PHP


输出:

1  0  0  0  0  
0  1  0  0  0  
0  0  1  0  0  
0  0  0  1  0  
0  0  0  0  1  

检查给定方阵是否为Identity Matrix的程序:

C++

// CPP program to check if a given matrix is identity
#include
using namespace std;
 
const int MAX = 100;
 
bool isIdentity(int mat[][MAX], int N)
{
    for (int row = 0; row < N; row++)
    {
        for (int col = 0; col < N; col++)
        {
            if (row == col && mat[row][col] != 1)
                return false;
            else if (row != col && mat[row][col] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver Code
int main()
{
    int N = 4;
    int mat[][MAX] = {{1, 0, 0, 0},
                    {0, 1, 0, 0},
                    {0, 0, 1, 0},
                    {0, 0, 0, 1}};
    if (isIdentity(mat, N))
       cout << "Yes ";
    else
       cout << "No ";
    return 0;
}

Java

// Java program to check if a given
// matrix is identity
class GFG {
     
    int MAX = 100;
  
    static boolean isIdentity(int mat[][], int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row][col] != 1)
                    return false;
                else if (row != col && mat[row][col] != 0)
                    return false;
            }
        }
        return true;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int N = 4;
        int mat[][] = {{1, 0, 0, 0},
                       {0, 1, 0, 0},
                       {0, 0, 1, 0},
                       {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
           System.out.println("Yes ");
        else
           System.out.println("No ");
    }
}
 
 
/*This code is contributed by Nikita Tiwari.*/

Python3

# Python3 program to check
# if a given matrix is identity
MAX = 100;
def isIdentity(mat, N):
    for row in range(N):
        for col in range(N):
            if (row == col and
                mat[row][col] != 1):
                return False;
            elif (row != col and
                  mat[row][col] != 0):
                return False;
    return True;
 
# Driver Code
N = 4;
mat = [[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]];
if (isIdentity(mat, N)):
    print("Yes ");
else:
    print("No ");
 
# This code is contributed
# by mits

C#

// C# program to check if a given
// matrix is identity
using System;
 
class GFG {
     
    //int MAX = 100;
 
    static bool isIdentity(int[,] mat, int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row,col] != 1)
                    return false;
                else if (row != col && mat[row,col] != 0)
                    return false;
            }
        }
        return true;
    }
     
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int [,]mat = {{1, 0, 0, 0},
                      {0, 1, 0, 0},
                      {0, 0, 1, 0},
                      {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
        Console.WriteLine("Yes ");
        else
        Console.WriteLine("No ");
    }
}
 
 
/*This code is contributed by vt_m.*/

的PHP


输出:

Yes