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

[wpedon id="7041" align="center"]

One thought on “Matrix Operations in Python using SciPy

  1. Hello Manas Sharma, I am an engineering professor in Brazil, and I really enjoyed your Blog. I am interested in making applications in the robotics area using Python.
    I’m trying to apply concepts from Kinematics theory based on Denavit-Hartenberg using python.
    If you have any applications on this subject and can show it I would appreciate it.

    Thanks,

    Cristian Duarte
    [email protected]

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.