给定一个矩阵,我们需要检查它是否是正交矩阵。正交矩阵是一个方阵,满足以下条件:
A*At = I
例子 :
Input: 1 0 0
0 1 0
0 0 1
Output: Yes
Given Matrix is an orthogonal matrix. When
we multiply it with its transpose, we get
identity matrix.
Input: 1 2 3
4 5 6
7 8 9
Output: No
Given Matrix Is Not An Orthogonal Matrix
简单的解决方案:这个想法很简单,我们首先找到矩阵的转置。然后,将转置与给定矩阵相乘。最后,我们检查获得的矩阵是否为同一性。
C++
// C++ code to check whether
// a matrix is orthogonal or not
#include
using namespace std;
#define MAX 100
bool isOrthogonal(int a[][MAX],
int m, int n)
{
if (m != n)
return false;
// Find transpose
int trans[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
trans[i][j] = a[j][i];
// Find product of a[][]
// and its transpose
int prod[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying with
// transpose of itself. We use
sum = sum + (a[i][k] * a[j][k]);
}
prod[i][j] = sum;
}
}
// Check if product is identity matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j && prod[i][j] != 0)
return false;
if (i == j && prod[i][j] != 1)
return false;
}
}
return true;
}
// Driver Code
int main()
{
int a[][MAX] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java code to check whether
// a matrix is orthogonal or not
import java .io.*;
class GFG {
//static int MAX =100;
static boolean isOrthogonal(int [][]a,
int m,
int n)
{
if (m != n)
return false;
// Find transpose
int [][]trans = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
trans[i][j] = a[j][i];
// Find product of a[][]
// and its transpose
int [][]prod = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying
// transpose of itself. We use
sum = sum + (a[i][k] * a[j][k]);
}
prod[i][j] = sum;
}
}
// Check if product is
// identity matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j && prod[i][j] != 0)
return false;
if (i == j && prod[i][j] != 1)
return false;
}
}
return true;
}
// Driver code
static public void main (String[] args)
{
int [][]a = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by anuj_67.
Python3
# Python code to check
# whether a matrix is
# orthogonal or not
def isOrthogonal(a, m, n) :
if (m != n) :
return False
trans = [[0 for x in range(n)]
for y in range(n)]
# Find transpose
for i in range(0, n) :
for j in range(0, n) :
trans[i][j] = a[j][i]
prod = [[0 for x in range(n)]
for y in range(n)]
# Find product of a[][]
# and its transpose
for i in range(0, n) :
for j in range(0, n) :
sum = 0
for k in range(0, n) :
# Since we are multiplying
# with transpose of itself.
# We use
sum = sum + (a[i][k] *
a[j][k])
prod[i][j] = sum
# Check if product is
# identity matrix
for i in range(0, n) :
for j in range(0, n) :
if (i != j and prod[i][j] != 0) :
return False
if (i == j and prod[i][j] != 1) :
return False
return True
# Driver Code
a = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
if (isOrthogonal(a, 3, 3)) :
print ("Yes")
else :
print ("No")
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# code to check whether
// a matrix is orthogonal or not
using System;
class GFG
{
//static int MAX =100;
static bool isOrthogonal(int [,]a,
int m,
int n)
{
if (m != n)
return false;
// Find transpose
int [,]trans = new int[n, n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
trans[i, j] = a[j, i];
// Find product of a[][]
// and its transpose
int [,]prod = new int[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying
// transpose of itself. We use
sum = sum + (a[i, k] * a[j, k]);
}
prod[i, j] = sum;
}
}
// Check if product is
// identity matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j && prod[i, j] != 0)
return false;
if (i == j && prod[i, j] != 1)
return false;
}
}
return true;
}
// Driver code
static public void Main ()
{
int [,]a = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m
PHP
C++
// C++ code to check whether
// a matrix is orthogonal or not
#include
using namespace std;
#define MAX 100
bool isOrthogonal(int a[][MAX],
int m, int n)
{
if (m != n)
return false;
// Multiply A*A^t
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying with
// transpose of itself. We use
// a[j][k] instead of a[k][j]
sum = sum + (a[i][k] * a[j][k]);
}
if (i == j && sum != 1)
return false;
if (i != j && sum != 0)
return false;
}
}
return true;
}
// Driver Code
int main()
{
int a[][MAX] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// C# code to check whether
// a matrix is orthogonal or not
using System;
class GFG
{
//static int MAX =100;
static bool isOrthogonal(int[,]a,
int m,
int n)
{
if (m != n)
return false;
// Multiply A*A^t
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying
// with transpose of itself.
// We use a[j][k] instead
// of a[k][j]
sum = sum + (a[i, k] * a[j, k]);
}
if (i == j && sum != 1)
return false;
if (i != j && sum != 0)
return false;
}
}
return true;
}
// Driver code
public static void Main ()
{
int [,]a = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m
Python3
# Python code to check
# whether a matrix is
# orthogonal or not
def isOrthogonal(a, m, n) :
if (m != n) :
return False
# Multiply A*A^t
for i in range(0, n) :
for j in range(0, n) :
sum = 0
for k in range(0, n) :
# Since we are multiplying
# with transpose of itself.
# We use a[j][k] instead
# of a[k][j]
sum = sum + (a[i][k] *
a[j][k])
if (i == j and sum != 1) :
return False
if (i != j and sum != 0) :
return False
return True
# Driver Code
a = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
if (isOrthogonal(a, 3, 3)) :
print ("Yes")
else :
print ("No")
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# code to check whether
// a matrix is orthogonal or not
using System;
public class GFG
{
//static int MAX =100;
static bool isOrthogonal(int[,]a, int m, int n)
{
if (m != n)
return false;
// Multiply A*A^t
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
// Since we are multiplying with
// transpose of itself. We use
// a[j][k] instead of a[k][j]
sum = sum + (a[i,k] * a[j,k]);
}
if (i == j && sum != 1)
return false;
if (i != j && sum != 0)
return false;
}
}
return true;
}
// Driver code
public static void Main ()
{
int [,]a = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m
PHP
Javascript
输出 :
Yes
一种有效的解决方案是将三个遍历合并为一个。代替显式地找到转置,我们使用a [j] [k]代替a [k] [j]。此外,我们在计算产品时会检查身份,而不是显式地计算产品。
C++
// C++ code to check whether
// a matrix is orthogonal or not
#include
using namespace std;
#define MAX 100
bool isOrthogonal(int a[][MAX],
int m, int n)
{
if (m != n)
return false;
// Multiply A*A^t
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying with
// transpose of itself. We use
// a[j][k] instead of a[k][j]
sum = sum + (a[i][k] * a[j][k]);
}
if (i == j && sum != 1)
return false;
if (i != j && sum != 0)
return false;
}
}
return true;
}
// Driver Code
int main()
{
int a[][MAX] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// C# code to check whether
// a matrix is orthogonal or not
using System;
class GFG
{
//static int MAX =100;
static bool isOrthogonal(int[,]a,
int m,
int n)
{
if (m != n)
return false;
// Multiply A*A^t
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
// Since we are multiplying
// with transpose of itself.
// We use a[j][k] instead
// of a[k][j]
sum = sum + (a[i, k] * a[j, k]);
}
if (i == j && sum != 1)
return false;
if (i != j && sum != 0)
return false;
}
}
return true;
}
// Driver code
public static void Main ()
{
int [,]a = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m
Python3
# Python code to check
# whether a matrix is
# orthogonal or not
def isOrthogonal(a, m, n) :
if (m != n) :
return False
# Multiply A*A^t
for i in range(0, n) :
for j in range(0, n) :
sum = 0
for k in range(0, n) :
# Since we are multiplying
# with transpose of itself.
# We use a[j][k] instead
# of a[k][j]
sum = sum + (a[i][k] *
a[j][k])
if (i == j and sum != 1) :
return False
if (i != j and sum != 0) :
return False
return True
# Driver Code
a = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
if (isOrthogonal(a, 3, 3)) :
print ("Yes")
else :
print ("No")
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# code to check whether
// a matrix is orthogonal or not
using System;
public class GFG
{
//static int MAX =100;
static bool isOrthogonal(int[,]a, int m, int n)
{
if (m != n)
return false;
// Multiply A*A^t
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
// Since we are multiplying with
// transpose of itself. We use
// a[j][k] instead of a[k][j]
sum = sum + (a[i,k] * a[j,k]);
}
if (i == j && sum != 1)
return false;
if (i != j && sum != 0)
return false;
}
}
return true;
}
// Driver code
public static void Main ()
{
int [,]a = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (isOrthogonal(a, 3, 3))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m
的PHP
Java脚本
输出 :
Yes