📜  对称矩阵和偏对称矩阵之和的平方矩阵

📅  最后修改于: 2021-04-25 00:45:24             🧑  作者: Mango

A为包含所有实数项的方阵。找出两个对称矩阵P偏斜对称矩阵Q ,使P + Q = A.

对称矩阵:-如果矩阵的转置与原始矩阵相同,则称其为对称矩阵。
斜对称矩阵:-如果矩阵的负转置与原始矩阵相同,则称方矩阵为斜对称矩阵。

例子 :

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

设A为方阵,则
A =(1/2)*(A + A’)+(1/2)*(A – A’)其中A’是A的转置矩阵。在上式(1/2)*(A + A’)表示对称矩阵,(1/2)*(A – A’)表示偏斜对称矩阵。如果仔细研究,我们会注意到这两个矩阵是对称和偏斜的(我们基本上是将两个像元值的一半分配给两个)。

C++
// C++ program for distribute a square matrix into
// symmetric and skew symmetric matrix.
#include 
#define N 3
using 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
// 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
# 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#
// 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 code
public 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


输出 :

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