A square matrix as sum of symmetric and skew-symmetric matrices
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).
Implementation:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
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])
{
float tr[N][N];
for ( int i = 0; i < N; i++)
for ( int j = 0; j < N; j++)
tr[i][j] = mat[j][i];
float symm[N][N], skewsymm[N][N];
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);
}
int main()
{
float mat[N][N] = { { 2, -2, -4 },
{ -1, 3, 4 },
{ 1, -2, -3 } };
printDistribution(mat);
return 0;
}
|
Java
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[][])
{
int N=mat.length;
float [][] tr = new float [N][N];
for ( int i = 0 ; i < N; i++)
for ( int j = 0 ; j < N; j++)
tr[i][j] = mat[j][i];
float [][] symm= new float [N][N];
float [][] skewsymm= new float [N][N];
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) {
float mat[][] = { { 2 , - 2 , - 4 },
{ - 1 , 3 , 4 },
{ 1 , - 2 , - 3 } };
printDistribution(mat);
}
}
|
Python3
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 = [[ 0 for x in range (N)]
for y in range (N)];
for i in range (N):
for j in range (N):
tr[i][j] = mat[j][i];
symm = [[ 0 for x in range (N)]
for y in range (N)] ;
skewsymm = [[ 0 for x in range (N)]
for y in range (N)];
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);
mat = [[ 2 , - 2 , - 4 ], [ - 1 , 3 , 4 ], [ 1 , - 2 , - 3 ]];
printDistribution(mat);
|
C#
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)
{
float [,] tr = new float [N, N];
for ( int i = 0; i < N; i++)
for ( int j = 0; j < N; j++)
tr[i, j] = mat[j, i];
float [,] symm = new float [N, N];
float [,] skewsymm = new float [N, N];
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);
}
public static void Main()
{
float [,] mat = new float [,]{{ 2, -2, -4},
{-1, 3, 4},
{1, -2, -3}};
printDistribution(mat);
}
}
|
PHP
<?php
$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 ;
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
$tr [ $i ][ $j ] = $mat [ $j ][ $i ];
$symm ;
$skewsymm ;
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 );
}
$mat = array ( array (2, -2, -4),
array (-1, 3, 4),
array (1, -2, -3));
printDistribution( $mat );
?>
|
Javascript
<script>
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)
{
var N=mat.length;
var tr = Array(N).fill(0).map(x => Array(N).fill(0));
for ( var i = 0; i < N; i++)
for ( var j = 0; j < N; j++)
tr[i][j] = mat[j][i];
var symm=Array(N).fill(0).map(x => Array(N).fill(0));
var skewsymm=Array(N).fill(0).map(x => Array(N).fill(0));
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);
}
var mat = [ [ 2, -2, -4 ],
[ -1, 3, 4 ],
[ 1, -2, -3 ] ];
printDistribution(mat);
</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
Time Complexity: O(N*N), as nested loops are used
Auxiliary Space: O(N*N), as extra space of (N*N) is used to create matrix
Last Updated :
01 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...