Numpy.linalg.solve() method is used to solve a linear matrix equation or a system of linear scalar equations.
It calculates the exact solution of the equation Ax = B, where A is a square matrix, and B can be either a vector or a matrix.
Matrix A must be square (the number of rows equals the number of columns) and non-singular (i.e., it must have an inverse).
Syntax
numpy.linalg.solve(A, B)
Parameters
Name | Description |
A | A square, N-by-N matrix |
B | Right-hand side matrix or vector. The shape must be (N,) or (N, M), where M is the number of columns in B. |
Return Value
It returns the solution to the equation as a vector or matrix. The output’s shape is the same as that of B.
It also returns LinAlgError if our first matrix (a) is singular or not square.
Visual Representation
Example 1: Solving a simple system of linear equations
Here are two simple systems of linear equations:
- 3x + y = 9
- x + 2y = 8
Now, we want to solve this equation by finding the value of “x” and “y”.
We will define a 2×2 matrix A with elements [[3, 1], [1, 2]]. This matrix represents the coefficients of the linear equations.
# Define the matrix A
A = np.array([[3, 1], [1, 2]])
We will define a vector B with elements [9, 8]. This vector represents the constants on the right-hand side of the equations.
# Define a vector B
B = np.array([9, 8])
Now, we use numpy.linalg.solve(A, B) method to solve for the vector x in the equation Ax = B.
Here is the complete code:
import numpy as np
# Define the matrix A
A = np.array([[3, 1], [1, 2]])
# Define a vector B
B = np.array([9, 8])
output = np.linalg.solve(A, B)
print(output)
Output
[2. 3.]
The solution [2., 3.] means that x = 2 and y = 3 satisfy both equations in the system.
You can verify this by plugging these values into the original equations:
3(2) + 3 = 6 + 3 = 9
2 + 2(3) = 2 + 6 = 8
That means our solution is correct!
Example 2: Solving multiple systems with a shared coefficient matrix
import numpy as np
# Define the matrix A
A = np.array([[3, 1], [1, 2]])
print(A)
# Define a matrix B
B = np.array([[9, 8], [6, 4]])
print(B)
output = np.linalg.solve(A, B)
print(output)
Output
[[2.4 2.4]
[1.8 0.8]]
The first column of the output [2.4, 1.8]
It corresponds to matrix B’s first set of constants [9, 6].
It implies that x = 2.4 and y = 1.8 are the solutions for the system of equations formed by matrix A and the first column of B:
3x + 1y = 9
1x + 2y = 6
Substituting x = 2.4 and y = 1.8 into the equations confirms that they satisfy both.
The second column of the output [2.4, 0.8]
It corresponds to matrix B’s second set of constants [8, 4].
Indicates that x = 2.4 and y = 0.8 are the solutions for the system of equations formed by matrix A and the second column of B:
3x + 1y = 8
1x + 2y = 4
Substituting x = 2.4 and y = 0.8 into the equations confirms that they satisfy both.
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.