[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy

Since python3.5, the product AB of matrix A and matrix B is calculated using the @ operator. A@B I came to be able to write. By using this @ operator, readability in coding is improved. However, as you can see in the code below, you need to ** generate matrices A and B as matrix objects instead of array objects **.

Contents Calculate the matrix product using the @ operator.

import numpy as np

"""
Matrix multiplication
C = A B
"""

#Make a matrix A
a1_lis = [1, 0, 0]
a2_lis = [0, 2, 0]
a3_lis = [0, 0, 4] 
A_matrix=np.matrix([a1_lis, a2_lis, a3_lis])# "np.array"not,"np.matrix"Generate a matrix object as

#Make a matrix B
b1_lis = [3, 0, 0]
b2_lis = [1, 2, 0]
b3_lis = [1, 0, 4] 
B_matrix=np.matrix([b1_lis, b2_lis, b3_lis]) #


C_matrix = A_matrix @ B_matrix  C =Calculation of A B
print(C_matrix)

result

[[ 3 0 0] [ 2 4 0] [ 4 0 16]]


Addendum When not using the @ operator

When the matrices A and B are generated as array objects instead of matrix objects (let's say np.array ()), Of the code above C_matrix = A_matrix @ B_matrix

The part of should be written as follows.

C_matrix = A_matrix.dot(B_matrix)

Recommended Posts

[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
Visualization of matrix created by numpy
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
Non-logical operator usage of or in python
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
Calculation of technical indicators by TA-Lib and pandas
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
[Scientific / technical calculation by Python] Generation of non-uniform random numbers giving a given probability density function, Monte Carlo simulation
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Calculation of homography matrix by least squares method (DLT method)
python numpy array calculation
[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
Benchmark by matrix product: NumPy, Numba, Cython, Swig, OpenCL, intelMKL
Python 3.4 or later standard pip
[Python] Calculation method with numpy
Matrix multiplication in python numpy
Calculation of similarity by MinHash
[Scientific / technical calculation by Python] Numerical solution of one-dimensional unsteady heat conduction equation by Crank-Nicholson method (implicit method) and FTCS method (positive solution method), parabolic partial differential equation
Python bitwise operator and OR
Ruby's `` like Python. 2.6 or later
Python or and and operator trap