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
Ph.D. researcher at Friedrich-Schiller University Jena, Germany. I’m a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
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]