Check whether a given matrix is orthogonal or not - GeeksforGeeks

18 Aug, 2021
We are given a matrix, we need to check whether it is an orthogonal matrix or not. An orthogonal matrix is a square matrix and satisfies the following condition: 
  A*At = I
Examples : 
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
Simple Solution : The idea is simple, we first find transpose of matrix. Then we multiply the transpose with given matrix. Finally we check if the matrix obtained is identity or not. 
C++
Java
Python3
C#
PHP
Javascript
// C++ code to check whether// a matrix is orthogonal or not#include <bits/stdc++.h>using namespace std; #define MAX 100 bool isOrthogonal(int a[][MAX],                  int m, int n){if (m != n)    return false; // Find transposeint 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 transposeint 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 matrixfor (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 Codeint 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 code to check whether// a matrix is orthogonal or notimport 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.
# 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 Codea = [[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# code to check whether// a matrix is orthogonal or notusing 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// PHP code to check whether// a matrix is orthogonal or not function isOrthogonal($a,                      $m, $n){    if ($m != $n)        return false;         // Find transpose    for($i = 0; $i < $n; $i++)        for ($j = 0; $j < $n; $j++)        $trans[$i][$j] = $a[$j][$i];         // Find product of a[][]    // and its transpose    for($i = 0; $i < $n; $i++)    {        for ($j = 0; $j < $n; $j++)        {             $sum = 0;        for ($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($i = 0; $i < $n; $i++)    {        for ($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    $a = array(array(1, 0, 0),               array(0, 1, 0),               array(0, 0, 1));    if (isOrthogonal($a, 3, 3))        echo "Yes";    else        echo "No"; // This code is contributed by ajit.?>
<script> // Javascript code to check whether// a matrix is orthogonal or not // static int MAX =100;function isOrthogonal(a, m, n){    if (m != n)        return false;          // Find transpose    let trans = new Array(n);         for(let i = 0; i < n; i++)    {        trans[i] = new Array(n);        for(let j = 0; j < n; j++)            trans[i][j] = a[j][i];    }          // Find product of a[][]    // and its transpose    let prod = new Array(n);    for(let i = 0; i < n; i++)    {        prod[i] = new Array(n);        for(let j = 0; j < n; j++)        {            let sum = 0;            for(let 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(let i = 0; i < n; i++)    {        for(let 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 codelet a = [ [ 1, 0, 0 ],          [ 0, 1, 0 ],          [ 0, 0, 1 ] ];           if (isOrthogonal(a, 3, 3))  document.write("Yes");else  document.write("No");   // This code is contributed by divyesh072019 </script>
Yes
 
An efficient solution is to combine three traversals into one. Instead of explicitly finding transpose, we use a[j][k] instead of a[k][j]. Also, instead of explicitly computing product, we check identity while computing product. 
C++
Java
Python3
C#
PHP
Javascript
// C++ code to check whether// a matrix is orthogonal or not#include <bits/stdc++.h>using namespace std; #define MAX 100 bool isOrthogonal(int a[][MAX],                  int m, int n){if (m != n)    return false; // Multiply A*A^tfor (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 Codeint 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;}
// C# code to check whether// a matrix is orthogonal or notusing 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
# 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 Codea = [[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# code to check whether// a matrix is orthogonal or notusing 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// PHP code to check whether// a matrix is orthogonal or not  //$MAX = 100; function isOrthogonal($a, $m, $n){if ($m != $n)    return false; // Multiply A*A^tfor ( $i = 0; $i < $n; $i++){    for ( $j = 0; $j < $n; $j++)    {     $sum = 0;    for ( $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 and $sum != 1)        return false;    if ($i != $j and $sum != 0)        return false;    }} return true;} // Driver Code$a = array(array(1, 0, 0),           array(0, 1, 0),           array(0, 0, 1));if (isOrthogonal($a, 3, 3))    echo "Yes";else    echo "No"; // This code is contributed by anuj_67.?>
<script> // Javascript code to check whether// a matrix is orthogonal or not MAX=100 function isOrthogonal(a, m, n){if (m != n)    return false; // Multiply A*A^tfor (var i = 0; i < n; i++){    for (var j = 0; j < n; j++)    {     var sum = 0;    for (var 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 Codevar a = [[1, 0, 0],                [0, 1, 0],                [0, 0, 1]];if (isOrthogonal(a, 3, 3))    document.write( "Yes");else    document.write( "No"); // This code is contributed by noob2000.</script>
Yes
 
YouTubeGeeksforGeeks500K subscribersCheck whether a given matrix is orthogonal or not | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:14•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=ui4OQJjViMs" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
KRV
vt_m
jit_t
manishshaw1
noob2000
divyesh072019
adnanirshad158
Mathematical
Matrix
Mathematical
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to convert a given number to words
Modular multiplicative inverse
Algorithm to solve Rubik's Cube
Program to multiply two matrices
Count ways to reach the n'th stair
Matrix Chain Multiplication | DP-8
Program to find largest element in an array
Sudoku | Backtracking-7
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Rat in a Maze | Backtracking-2