A square matrix as sum of symmetric and skew-symmetric matrices - GeeksforGeeks

27 May, 2021
Let A be a square matrix with all real number entries. Find two symmetric matrix P and skew symmetric matrix Q such that P + Q = A.Symmetric Matrix:- A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the original matrix. Skew Symmetric Matrix:- A square matrix is said to be skew symmetric matrix if the negative transpose of matrix is same as the original matrix.Examples :  
Input :
          {{ 2, -2, -4},
     mat=  {-1,  3,  4},
           { 1, -2, -3}};
Output :
Symmetric matrix-
        2  -1.5 -1.5
      -1.5   3    1
      -1.5   1   -3
Skew Symmetric Matrix-
       0 -0.5 -2.5
     0.5   0   3
     2.5  -3   0
Explanation : The first matrix is symmetric as
transpose of it is same as the given matrix. The
second matrix is Skew Symmetric as negative transpose
is same as this matrix. Also sum of the two matrices
is same as mat[][].


Input:
          {{5, 6, 8},
     mat = {3, 4, 9},
           {7, 2, 3}};
Output :
Symmetric matrix-
       5   4.5   7.5
      4.5   4    5.5
      7.5  5.5    3
Skew Symmetric Matrix-
      0   1.5   0.5
    -1.5   0    3.5
    -0.5 -3.5    0
 
Let A be a square matrix, then A = (1/2)*(A + A’) + (1/2)*(A – A’) Where A’ is the transpose matrix of A. In the above formula (1/2)*(A + A’) represents symmetric matrix and (1/2)*(A – A’) represents skew symmetric matrix. If we take a closer look, we can notice that the two matrices are symmetric and skew symmetric (We are basically distributing half of two cell values to both).  
C++
Java
Python3
C#
PHP
Javascript
// C++ program for distribute a square matrix into// symmetric and skew symmetric matrix.#include <bits/stdc++.h>#define N 3using namespace std; /* Below functions can be used to verify result// Returns true if matrix is skew symmetric,// else false.bool isSymmetric(float mat[N][N]){    for (int i = 0; i < N; i++)        for (int j = 0; j < N; j++)            if (mat[i][j] != mat[j][i])                return false;    return true;} // Returns true if matrix is skew symmetric,// else false.bool isSkewSymmetric(float mat[N][N]){    for (int i = 0; i < N; i++)        for (int j = 0; j < N; j++)            if (mat[i][j] != -mat[j][i])                return false;    return true;} */ void printMatrix(float mat[N][N]){    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++)            cout << mat[i][j] << "   ";        cout << endl;    }} void printDistribution(float mat[N][N]){    // tr is the transpose of matrix mat.    float tr[N][N];     // Find transpose of matrix.    for (int i = 0; i < N; i++)        for (int j = 0; j < N; j++)            tr[i][j] = mat[j][i];     // Declare two square matrix symm and    // skewsymm of size N.    float symm[N][N], skewsymm[N][N];     // Loop to find symmetric and skew symmetric    // and store it into symm and skewsymm matrix.    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {            symm[i][j] = (mat[i][j] + tr[i][j]) / 2;            skewsymm[i][j] = (mat[i][j] - tr[i][j]) / 2;        }    }     cout << "Symmetric matrix-" << endl;    printMatrix(symm);     cout << "Skew Symmetric matrix-" << endl;    printMatrix(skewsymm);} // Driver function.int main(){    // mat is the N * N square matrix.    float mat[N][N] = { { 2, -2, -4 },                        { -1, 3, 4 },                        { 1, -2, -3 } };    printDistribution(mat);     return 0;}
// Java program for distribute// a square matrix into// symmetric and skew symmetric// matrix. import java.io.*;import java.util.*; class GFG {static void printMatrix(float mat[][]){    for (int i = 0; i < mat.length; i++) {        for (int j = 0; j < mat[i].length; j++)            System.out.print(mat[i][j] + "   ");        System.out.println();    }}  static void printDistribution(float mat[][]){    // tr is the transpose of matrix mat.    int N=mat.length;    float[][] tr = new float[N][N];      // Find transpose of matrix.    for (int i = 0; i < N; i++)        for (int j = 0; j < N; j++)            tr[i][j] = mat[j][i];      // Declare two square matrix symm and    // skewsymm of size N.    float[][] symm=new float[N][N];    float[][] skewsymm=new float[N][N];      // Loop to find symmetric and skew symmetric    // and store it into symm and skewsymm matrix.    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {            symm[i][j] = (mat[i][j] + tr[i][j]) / 2;            skewsymm[i][j] = (mat[i][j] - tr[i][j]) / 2;        }    }      System.out.println("Symmetric matrix-" );    printMatrix(symm);      System.out.println("Skew Symmetric matrix-" );    printMatrix(skewsymm);}    public static void main (String[] args) {     // mat is the N * N square matrix.    float mat[][] = { { 2, -2, -4 },                        { -1, 3, 4 },                        { 1, -2, -3 } };    printDistribution(mat);     }} // This code is contributed by Gitanjali.
# Python3 program to distribute a# square matrix into symmetric# and skew symmetric matrix.N = 3; def printMatrix(mat):     for i in range(N):        for j in range(N):            print(mat[i][j], end = " ");        print(""); def printDistribution(mat):         # tr is the transpose    # of matrix mat.    tr = [[0 for x in range(N)]             for y in range(N)];     # Find transpose of matrix.    for i in range(N):        for j in range(N):            tr[i][j] = mat[j][i];     # Declare two square    # matrix symm and    # skewsymm of size N.    symm = [[0 for x in range(N)]               for y in range(N)] ;    skewsymm = [[0 for x in range(N)]                   for y in range(N)];     # Loop to find symmetric    # and skew symmetric and    # store it into symm and    # skewsymm matrix.    for i in range(N):        for j in range(N):            symm[i][j] = (mat[i][j] + tr[i][j]) / 2;            skewsymm[i][j] = (mat[i][j] - tr[i][j]) / 2;     print("Symmetric matrix-");    printMatrix(symm);     print("Skew Symmetric matrix");    printMatrix(skewsymm); # Driver Code # mat is the N * N# square matrix.mat = [[2, -2, -4], [-1, 3, 4], [1, -2, -3]];printDistribution(mat); # This code is contributed by mits.
// C# program for distribute// a square matrix into// symmetric and skew// symmetric matrix.using System; class GFG{ static int N = 3;static void printMatrix(float[,] mat){    for (int i = 0; i < N; i++)    {        for (int j = 0; j < N; j++)            Console.Write(mat[i, j] + " ");        System.Console.WriteLine();    }} static void printDistribution(float[,] mat){    // tr is the transpose    // of matrix mat.    float[,] tr = new float[N, N];     // Find transpose of matrix.    for (int i = 0; i < N; i++)        for (int j = 0; j < N; j++)            tr[i, j] = mat[j, i];     // Declare two square matrix symm and    // skewsymm of size N.    float[,] symm = new float[N, N];    float[,] skewsymm = new float[N, N];     // Loop to find symmetric and skew symmetric    // and store it into symm and skewsymm matrix.    for (int i = 0; i < N; i++)    {        for (int j = 0; j < N; j++)        {            symm[i, j] = (mat[i, j] +                           tr[i, j]) / 2;            skewsymm[i, j] = (mat[i, j] -                               tr[i, j]) / 2;        }     }     System.Console.WriteLine("Symmetric matrix-" );    printMatrix(symm);     System.Console.WriteLine("Skew Symmetric matrix-" );    printMatrix(skewsymm);} // Driver codepublic static void Main(){    // mat is the N * N    // square matrix.    float[,] mat = new float[,]{{ 2, -2, -4},                                {-1, 3, 4},                                {1, -2, -3}};    printDistribution(mat);}} // This code is contributed by mits.
<?php// PHP program to distribute a // square matrix into symmetric// and skew symmetric matrix.$N = 3; function printMatrix($mat){    global $N;    for ($i = 0; $i < $N; $i++)    {        for ($j = 0; $j < $N; $j++)            echo $mat[$i][$j]. " ";        echo "\n";    }} function printDistribution($mat){    global $N;         // tr is the transpose    // of matrix mat.    $tr;     // Find transpose of matrix.    for ($i = 0; $i < $N; $i++)        for ($j = 0; $j < $N; $j++)            $tr[$i][$j] = $mat[$j][$i];     // Declare two square    // matrix symm and    // skewsymm of size N.    $symm;    $skewsymm;     // Loop to find symmetric    // and skew symmetric and    // store it into symm and    // skewsymm matrix.    for ($i = 0; $i < $N; $i++)    {        for ($j = 0; $j < $N; $j++)        {            $symm[$i][$j] = ($mat[$i][$j] +                             $tr[$i][$j]) / 2;            $skewsymm[$i][$j] = ($mat[$i][$j] -                                 $tr[$i][$j]) / 2;        }    }     echo "Symmetric matrix-\n";    printMatrix($symm);     echo "Skew Symmetric matrix-\n";    printMatrix($skewsymm);} // Driver Code // mat is the N * N// square matrix.$mat = array(array(2, -2, -4),             array(-1, 3, 4),             array(1, -2, -3));printDistribution($mat); // This code is contributed by mits.?>
<script> // javascript program for distribute// a square matrix into// symmetric and skew symmetric// matrix.function printMatrix(mat){    for (var i = 0; i < mat.length; i++) {        for (var j = 0; j < mat[i].length; j++)            document.write(mat[i][j] + "   ");        document.write('<br>');    }}  function printDistribution(mat){     // tr is the transpose of matrix mat.    var N=mat.length;    var tr = Array(N).fill(0).map(x => Array(N).fill(0));      // Find transpose of matrix.    for (var i = 0; i < N; i++)        for (var j = 0; j < N; j++)            tr[i][j] = mat[j][i];      // Declare two square matrix symm and    // skewsymm of size N.    var symm=Array(N).fill(0).map(x => Array(N).fill(0));    var skewsymm=Array(N).fill(0).map(x => Array(N).fill(0));      // Loop to find symmetric and skew symmetric    // and store it into symm and skewsymm matrix.    for (var i = 0; i < N; i++) {        for (var j = 0; j < N; j++) {            symm[i][j] = (mat[i][j] + tr[i][j]) / 2;            skewsymm[i][j] = (mat[i][j] - tr[i][j]) / 2;        }    }      document.write("Symmetric matrix-<br>" );    printMatrix(symm);      document.write("Skew Symmetric matrix-<br>" );    printMatrix(skewsymm);}           // mat is the N * N square matrix.    var mat = [ [ 2, -2, -4 ],                        [ -1, 3, 4 ],                        [ 1, -2, -3 ] ];    printDistribution(mat);  // This code is contributed by Princi Singh</script>
Output :  
Symmetric matrix-
2 -1.5 -1.5 
-1.5 3 1 
-1.5 1 -3 
Skew Symmetric matrix-
0 -0.5 -2.5 
0.5 0 3 
2.5 -3 0 
 
Mithun Kumar
princi singh
Mathematical
Matrix
Mathematical
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Program to multiply two matrices
Matrix Chain Multiplication | DP-8
Program to find largest element in an array
Print a given matrix in spiral form
Sudoku | Backtracking-7
Rat in a Maze | Backtracking-2