[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy

Introduction

Solve the N-dimensional simultaneous linear equations $ A x = b $ with A as a matrix for $ x $.

Contents

(1) Calculate the inverse matrix $ A ^ -1 $ of A by numpy's linalg.inv method. Find the solution vector $ x = A ^ {-1} b $.

(2) Calculate $ x $ directly from the np.linalg.solve method. ** This is generally faster. ** **


Code (1)

%%time

import numpy as np
"""
A x =The x vector that satisfies b is linalg.Ask using solve
"""
#Generation of matrix A
a1_lis = [1, 0, 0]
a2_lis = [0, 2, 0]
a3_lis = [0, 0, 4] 
A_matrix=np.array([a1_lis, a2_lis, a3_lis])

# b vector
b = np.array([4,5,6]) #Generate b as a row vector and use it as a transposed matrix"Generate column vector "
b_vec = b.T
A_inv = np.linalg.inv(A_matrix)
x_vec= np.dot(A_inv,b_vec) #Calculate the x vector. In matrix multiplication@Using operators(reference)

print(x_vec)

Or you can write below using "matrix". Readability is high. However, it is not recommended because it is often inconvenient.

import numpy as np
"""
A x =x vector that satisfies b x= A^-1 Calculate by calculation of b
"""
#Generation of 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])

# b vector
bb = np.matrix([4,5,6]) #Generate b as a row vector and use it as a transposed matrix"Generate column vector "
b_vec = bb.T

x_vec=A_matrix.I@b_vec #Calculate the x vector. In matrix multiplication@Using operators(reference)

print(x_vec)

Result (1)

[[ 4. ]
 [ 2.5]
 [ 1.5]]

(2) When using numpy.linalg.solve (recommended)


import numpy as np
"""
A x =The x vector that satisfies b is linalg.Ask using solve
"""
#Generation of matrix A
a1_lis = [1, 0, 0]
a2_lis = [0, 2, 0]
a3_lis = [0, 0, 4] 
A_matrix=np.array([a1_lis, a2_lis, a3_lis])

# b vector
b = np.array([4,5,6]) #Generate b as a row vector and use it as a transposed matrix"Generate column vector "

x_vec= np.linalg.solve(A_matrix,b) #Calculate the x vector. In matrix multiplication@Using operators(reference)

print(x_vec)

Result (2) [ 4. 2.5 1.5]


Addendum

** The numpy.linalg.solve method is internally a Lapack LAPACK dgesv It is a wrapper for [1] and zgesv ]. ** **

● In computational physics, etc., simultaneous linear equations are often obtained. There are various schemes for finding the inverse matrix depending on the nature of the matrix $ A $, but ** I think it's enough to use the solve method when writing code ... ** ** Gaussian elimination method, Jacobi method, Gauss-Seidel method, conjugate gradient method, etc. are available as rudimentary numerical calculation methods for solving simultaneous linear equations [2]. You may want to look it up when you need it.


References

[1] Description of numpy.linalg.solve: https://docs.scipy.org/doc/numpy-1.7.0/reference/generated/numpy.linalg.solve.html

[2] Ichiro Kawakami, ["Numerical Calculation"](https://www.amazon.co.jp/%E6%95%B0%E5%80%A4%E8%A8%88%E7%AE%97- % E7% 90% 86% E5% B7% A5% E7% B3% BB% E3% 81% AE% E6% 95% B0% E5% AD% A6% E5% 85% A5% E9% 96% 80% E3 % 82% B3% E3% 83% BC% E3% 82% B9-8-% E5% B7% 9D% E4% B8% 8A-% E4% B8% 80% E9% 83% 8E / dp / 4000077783), Iwanami Shoten, 1989.

About @ operator: [[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy](Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 After that, numpy)

Recommended Posts

[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Numerical calculation method, python] Solving ordinary differential equations by Eular method
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[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] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation
[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
Solving simultaneous linear equations in Python (sweeping method and fractional representation)
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
Let's solve simultaneous linear equations with Python sympy!
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Solving one-dimensional Schrodinger equation in stationary state by shooting method (1), well-type potential, quantum mechanics
[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.
I tried it with Wolfram Alpha by referring to "Solving simultaneous linear equations with Python (sweeping method and fractional expression)".
python numpy array calculation
[Scientific / technical calculation by Python] Solving one-dimensional Schrodinger equation in stationary state by shooting method (2), harmonic oscillator potential, quantum mechanics
Numerical calculation with Python
[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] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Python] Calculation method with numpy
[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
[Scientific / technical calculation by Python] Numerical solution of two-dimensional Laplace-Poisson equation by Jacobi method for electrostatic potential, elliptic partial differential equation, boundary value problem
[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
Introduction to Python Numerical Library NumPy
How to solve simultaneous linear equations
[Scientific / technical calculation by Python] Solving the Schrodinger equation in the steady state in the 3D isotropic harmonic oscillator potential by the matrix method, boundary value problem, quantum mechanics