SOLVE
Computes the exact solution of the linear system Ax = b for the unknown x. The matrix A must be square and non-singular. The function supports various matrix types (general, symmetric, Hermitian, positive definite) which can be specified to use optimized solvers.
Excel Usage
=SOLVE(a_matrix, b_vector, assume_a)
a_matrix(list[list], required): Square 2D array of numeric coefficients.b_vector(list[list], required): 2D array representing the right-hand side (can be a vector or matrix).assume_a(str, optional, default: “gen”): Type of matrix A to assume (allows using optimized solvers).
Returns (list[list]): 2D array representing the solution x.
Example 1: Solve 2x2 system
Inputs:
| a_matrix | b_vector | |
|---|---|---|
| 3 | 1 | 9 |
| 1 | 2 | 8 |
Excel formula:
=SOLVE({3,1;1,2}, {9;8})
Expected output:
| Result |
|---|
| 2 |
| 3 |
Example 2: Solve symmetric system assumption
Inputs:
| a_matrix | b_vector | assume_a | |
|---|---|---|---|
| 4 | 1 | 1 | sym |
| 1 | 3 | 2 |
Excel formula:
=SOLVE({4,1;1,3}, {1;2}, "sym")
Expected output:
| Result |
|---|
| 0.0909091 |
| 0.636364 |
Example 3: Solve with positive definite assumption
Inputs:
| a_matrix | b_vector | assume_a | ||
|---|---|---|---|---|
| 2 | -1 | 0 | 1 | pos |
| -1 | 2 | -1 | 0 | |
| 0 | -1 | 2 | 1 |
Excel formula:
=SOLVE({2,-1,0;-1,2,-1;0,-1,2}, {1;0;1}, "pos")
Expected output:
| Result |
|---|
| 1 |
| 1 |
| 1 |
Example 4: Solve with matrix right-hand side
Inputs:
| a_matrix | b_vector | ||
|---|---|---|---|
| 1 | 2 | 5 | 6 |
| 3 | 4 | 7 | 8 |
Excel formula:
=SOLVE({1,2;3,4}, {5,6;7,8})
Expected output:
| Result | |
|---|---|
| -3 | -4 |
| 4 | 5 |
Python Code
import numpy as np
from scipy.linalg import solve as scipy_solve
def solve(a_matrix, b_vector, assume_a='gen'):
"""
Solve a linear matrix equation, or system of linear scalar equations.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve.html
This example function is provided as-is without any representation of accuracy.
Args:
a_matrix (list[list]): Square 2D array of numeric coefficients.
b_vector (list[list]): 2D array representing the right-hand side (can be a vector or matrix).
assume_a (str, optional): Type of matrix $A$ to assume (allows using optimized solvers). Valid options: General, Symmetric, Hermitian, Positive Definite. Default is 'gen'.
Returns:
list[list]: 2D array representing the solution x.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
a_matrix = to2d(a_matrix)
b_vector = to2d(b_vector)
if not isinstance(a_matrix, list) or not a_matrix or not all(isinstance(row, list) for row in a_matrix):
return "Error: a_matrix must be a non-empty 2D list"
n = len(a_matrix)
if any(len(row) != n for row in a_matrix):
return "Error: a_matrix must be square (n x n)"
try:
a = np.array(a_matrix, dtype=float)
b = np.array(b_vector, dtype=float)
except (ValueError, TypeError):
return "Error: matrix entries must contain numeric values"
if not np.all(np.isfinite(a)) or not np.all(np.isfinite(b)):
return "Error: Input must contain only finite numbers"
try:
x = scipy_solve(a, b, assume_a=assume_a.lower())
except np.linalg.LinAlgError as e:
return f"Error: Linear algebra solver failed: {str(e)}"
except Exception as e:
return f"Error: {str(e)}"
return x.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Square 2D array of numeric coefficients.
2D array representing the right-hand side (can be a vector or matrix).
Type of matrix $A$ to assume (allows using optimized solvers).