Matrix Operations in Python using SciPy

In this blog post, I demonstrate a Python code, that shows how to perform various matrix operations such as:
1. Defining a matrix,
2. Adding matrices
3. Multiplying two matrices,
4. Transposing a Matrix
5. Determinant of a matrix,
6. Inverse of a matrix,
7. Eigenvalues and eigenvectors of a matrix,

using the SciPy package and the lining module within it.

The documentation for SciPy lining is: https://docs.scipy.org/doc/scipy-0.14.0/reference/linalg.html

The code is pretty much self-explanatory, although you can also watch the YouTube video below it where I walkthrough the code.

CODE:

import numpy as np 
from scipy import linalg as lg


#Defining a matrix A
A = np.array([  [1, 2] , [3, 4]  ])

#Defining matrix B
B = np.array([ [6, 1], [5, 1] ])

#Addition
sum1 = A+B
#Subtraction
diff = A-B
#Multiplication
prod = A.dot(B)
#Transpose
transpose = A.T
#Determinant
determinantB = lg.det(B)
#Inverse (if non-singular)
inverse = lg.inv(B)
#Eigenvalues, Eigenvectors of square matrix
values, vectors = lg.eig(B)
#Print Matrix A
print(A)
#Print Matrix B
print(B)
#Print A+B
print(sum1)
#Print A-B
print(diff)
#Print A*B
print(prod)
#Print A'
print(transpose)
#Print det(B)
print(determinantB)

print(inverse)

print(values)

print(vectors)

YouTube Tutorial

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



Leave a Reply

Your email address will not be published. Required fields are marked *