python Simple Implementation Matrix Multiplication Addition Transposition and Inverse Operation Example

  • 2021-07-13 05:37:27
  • OfStack

In this paper, an example is given to describe the simple realization of matrix multiplication, addition, transposition and inverse operation by python. Share it for your reference, as follows:

Use python to multiply, add, transpose and invert the matrix:


# -*- coding:utf-8 -*-
# Multiplication, addition, transposition and inverse of matrices 
#numpy The library provides the function of matrix operation 
from numpy import *;
import numpy as np;
# Creation of Matrix ( Random ) data=mat(random.randint(10,size=(3,3)))
data=mat([ [3,4,4],
      [4,9,7],
      [2,3,3] ])
# Multiplication of matrix: 
data_1=mat([[1],[2],[3]]);# 3 x 3  Matrix and  3 x 1  Matrix multiplication  3 x 1  Matrix 
text1=data * data_1;
print(" Multiplication of Matrix :")
print(text1) # Verification result 
# Addition of matrix: 
data_2=mat([[3,4,4],[4,9,7],[2,3,3]])
text2=data + data_2
print(" Addition of Matrix :")
print(text2) # Matrix self-addition 
# Transposition of Matrix: mat.T
data_3=data.T
print(" Transposition of Matrix: ")
print(data_3) # Verify the transposition result 
# The inverse of a matrix: mat.I
data_4=data.I
print(" Inverse of matrix :")
print(data_4) # Verified inverse 

Run results:

Multiplication of matrix:
[[23]
[43]
[17]]
Addition of matrix:
[[ 6 8 8]
[ 8 18 14]
[ 4 6 6]]
Transposition of Matrix:
[[3 4 2]
[4 9 3]
[4 7 3]]
The inverse of a matrix:
[[ 3.00000000e+00 2.22044605e-16 -4.00000000e+00]
[ 1.00000000e+00 5.00000000e-01 -2.50000000e+00]
[ -3.00000000e+00 -5.00000000e-01 5.50000000e+00]]

For more readers interested in Python related contents, please check the topics of this site: "Summary of Python Mathematical Operation Skills", "Python Data Structure and Algorithm Tutorial", "Summary of Python Function Use Skills", "Summary of Python String Operation Skills" and "Introduction and Advanced Classic Tutorial of Python"

I hope this article is helpful to everyone's Python programming.


Related articles: