---
title: linalg.svd
description: "奇异值分解。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_1119.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_1119.html
indexId: e212592e4671b9241a0beff76bed6300ee9f4461f2daab10be2b9ff73faf696361
---
# linalg.svd

#### 功能描述

奇异值分解。

当a是二维数组，并且full_matrices=False时，它被分解为u @ np.diag(s) @ vh = (u * s) @ vh，其中u和vh的埃尔米特转置是具有正交列的二维数组，s是a奇异值的1维数组。当a是更高维度时，SVD（奇异值分解）以堆叠模式应用。


#### 必选输入参数

| 参数名 | 类型 | 说明 |
| --- | --- | --- |
| a | (…,M,N) array\_like | 具有a.ndim >= 2的实数组或复数组。 |
| b | array\_like | \- |


#### 可选输入参数

| 参数名 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| full\_matrices | bool | True | 如果是默认值True，u和vh分别具有形状(...,M,M)和(...,N,N)。否则，形状分别是(...,M,K)和(...,K,N)，其中K=min(M,N)。 |
| compute\_uv | bool | True | 除了s之外，是否计算u和vh。 |


#### 返回数据

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| u | {(…,M,M), (…,M,K) } array | 酉矩阵。前a.ndim\-2维度与输入a的尺寸相同，最后两个维度的大小取决于full\_matrices的值。仅在compute\_uv为True时返回。 |
| s | (…, K) array | 具有奇异值的向量，在每个向量中按降序排序。前a.ndim\-2维度与输入a的尺寸相同。 |
| vh | {(…,N,N), (…,K,N)} array | 酉矩阵。前a.ndim\-2维度与输入a的尺寸相同，最后两个维度的大小取决于full\_matrices的值。仅在compute\_uv为True时返回。 |


#### 示例

```
>>> import numpy as np
>>> a = np.random.randn(9, 6) + 1j * np.random.randn(9, 6)
>>> b = np.random.randn(2, 7, 8, 3) + 1j * np.random.randn(2, 7, 8, 3)
>>> 
>>> u, s, vh = np.linalg.svd(a, full_matrices=True)
>>> u.shape, s.shape, vh.shape
((9, 9), (6,), (6, 6))
>>> np.allclose(a, np.dot(u[:, :6] * s, vh))
True
>>> 
>>> smat = np.zeros((9,6), dtype=complex)
>>> smat[:6, :6] = np.diag(s)
>>> np.allclose(a, np.dot(u, np.dot(smat, vh)))
True
>>>
```
