---
title: linalg.solve
description: "求解线性矩阵方程或线性标量方程组。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_1115.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_1115.html
indexId: 42d53d5a50615feadd4099c7b1d7d72e8f7fd2549e594359cd327cc00dfbc1d461
---
# linalg.solve

#### 功能描述

求解线性矩阵方程或线性标量方程组。

计算确定的“精确”解x，即满秩线性矩阵方程ax = b


#### 必选输入参数

| 参数名 | 类型 | 说明 |
| --- | --- | --- |
| a | (…,M,M) array\_like | 系数矩阵。 |
| b | {(…,M,),(…,M,K)}, array\_like | 纵坐标或“因变量”值。 |


#### 可选输入参数

无。


#### 返回数据

| 类型 | 说明 |
| --- | --- |
| {(…, M,), (…, M, K)} ndarray | 方程组ax = b的解，返回的大小与b相同。 |


#### 示例

```
>>> import numpy as np
>>> # 解方程 x0 + 2*x1 = 1, 3*x0 + 5*x1 = 2
>>> a = np.array([[1,2],[3,5]])
>>> a
array([[1, 2],
       [3, 5]])
>>> b = np.array([1,2])
>>> b
array([1, 2])
>>> 
>>> x = np.linalg.solve(a, b)
>>> x
array([-1.,  1.])
>>> 
>>> np.allclose(np.dot(a,x), b)
True
>>>
```
