Symmetric Matrices | Linear Algebra using Python

Linear Algebra using Python | Symmetric Matrices: Here, we are going to learn about the symmetric matrices and its implementation in Python.
Submitted by Anuj Singh, on May 26, 2020

Prerequisites:

In linear algebra, if the matrix and its transpose are equal, then the matrix is symmetric (MT = M).

In terms of elements of matrices: M(i, j) = M(j, i)

Following is a python code for demonstrating how to check for Symmetric Matrix.

Method:

Syntax:
    M = numpy.array( )
    transpose_M = M.T
    if transpose_M == M:
        Transpose = True 

Return:
    MT

Python code for symmetric matrices

# Linear Algebra Learning Sequence
# Transpose using different Method

import numpy as np

M = np.array([[2,3,4], [3,45,8], [4,8,78]])
print("---Matrix M---\n", M)

# Transposing the Matrix M
print('\n\nTranspose as M.T----\n', M.T)

if M.T.all() == M.all():
    print("--------> Transpose is eqaul to M")
    
M = np.array([[2,3,4], [3,45,8]])
print("\n\n---Matrix B---\n", M)

# Transposing the Matrix M
print('\n\nTranspose as B.T----\n', M.T)

if M.T.all() == M.all() and np.shape(M) == np.shape(M.T):
    print("---------> Transpose is eqaul to B")
    
else:
    print("---------> Not Transpose!!")

Output:

---Matrix M---
 [[ 2  3  4]
 [ 3 45  8]
 [ 4  8 78]]


Transpose as M.T----
 [[ 2  3  4]
 [ 3 45  8]
 [ 4  8 78]]
--------> Transpose is eqaul to M


---Matrix B---
 [[ 2  3  4]
 [ 3 45  8]]


Transpose as B.T----
 [[ 2  3]
 [ 3 45]
 [ 4  8]]
---------> Not Transpose!!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.