---
title: ?posv
description: "求解线性方程组。其中A为实对称或共轭对称的正定矩阵；X和B为矩阵。计算矩阵A的Cholesky分解，并通过分解结果求解线性方程组。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0739.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0739.html
indexId: e58fc3c4d68f335395d8fedd668a187c49d146fb214369b57a0d11079c6dc66d61
---
# ?posv

求解线性方程组。其中A为实对称或共轭对称的正定矩阵；X和B为矩阵。计算矩阵A的Cholesky分解，并通过分解结果求解线性方程组。

#### 接口定义

C Interface：

void dposv_(const char *uplo, const int *n, const int *nrhs, double *a, const int *lda, double *b, const int *ldb, int *info);

void sposv_(const char *uplo, const int *n, const int *nrhs, float *a, const int *lda, float *b, const int *ldb, int *info);

void cposv_(const char *uplo, const int *n, const int *nrhs, float _Complex *a, const int *lda, float _Complex *b, const int *ldb, int *info);

void zposv_(const char *uplo, const int *n, const int *nrhs, double _Complex *a, const int *lda, double _Complex *b, const int *ldb, int *info);

Fortran Interface：

DPOSV(uplo, n, nrhs, a, lda, b, ldb, info);

SPOSV(uplo, n, nrhs, a, lda, b, ldb, info);

CPOSV(uplo, n, nrhs, a, lda, b, ldb, info);

ZPOSV(uplo, n, nrhs, a, lda, b, ldb, info);


#### 参数

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| uplo | 字符型 | 'U'：A中保存上三角矩阵。 'L'：A中保存下三角矩阵。 | 输入 |
| n | 整数型 | 对称矩阵A的行数或列数。 | 输入 |
| nrhs | 整数型 | 右侧项的数量，即矩阵B的列数，要求nrhs≥0。 | 输入 |
| a | 在sposv中为单精度浮点型数组。 在dposv中为双精度浮点型数组。 在cposv中为单精度复数型数组。 在zposv中为双精度复数型数组。 | 调用前：如果uplo='U'，那么矩阵A将包含上三角部分，下三角部分不会使用；如果uplo='L'，那么矩阵A将包含下三角部分，上三角部分不会使用。 调用后：保存A的Cholesky分解结果（U或L）。 | 输入/输出 |
| lda | 整数型 | A的leading dimension大小，要求lda≥max(1, n)。 | 输入 |
| b | 在sposv中为单精度浮点型数组。 在dposv中为双精度浮点型数组。 在cposv中为单精度复数型数组。 在zposv中为双精度复数型数组。 | 调用前：为右侧矩阵B。 调用后：为解矩阵X。 | 输入/输出 |
| ldb | 整数型 | B的leading dimension大小，要求ldb≥max(1, n)。 | 输入 |
| info | 整数型 | 执行结果： 等于0：成功。 小于0：第\-info个参数值不合法。 大于0：A中info大小的顺序主子式非正定，无法完成分解。 | 输出 |


#### 依赖

#include "klapack.h"


#### 示例

C Interface：```
char uplo = 'L'; 
int n = 4; 
int nrhs = 1; 
int lda = 4;
int ldb = 4; 
int info = 0; 
/* 
* A (stored in column-major): 
*  231.8009    33.9545    9.4153    11.1156    
*   33.9454    162.2902   6.6684    53.2023     
*   9.4153     6.6684     100.3220  71.5384   
*   11.1156    53.2023    71.5384   106.5638  
*/ 
 
double a[] = {231.8009,   33.9545,   9.4143,   11.1156, 
              33.9495,  162.2902,   6.6684,   53.2033, 
              9.4143,    6.6684,  100.322,   71.5384, 
              11.1156,   53.2033,  71.5384,  106.5638}; 
double b[] = {1.0, 2.0, 3.0, 4.0};
 
dposv_(&uplo, &n, &nrhs, a, &lda, b, &ldb, &info); 
/* 
 * Output X:  0.002361        0.000904        0.006432        0.032521
 */
```


Fortran Interface：

```
CHARACTER :: uplo = "L"
PARAMETER (n = 4)
PARAMETER (nrhs = 1)
PARAMETER (lda = 4)
PARAMETER (ldb = 4)
INTEGER :: info = 0
*
*  A (stored in column-major):
*  231.8009    33.9545    9.4153    11.1156
*   33.9454    162.2902   6.6684    53.2023
*   9.4153     6.6684     100.3220  71.5384
*   11.1156    53.2023    71.5384   106.5638
REAL(8) :: a(lda, n)
DATA a / 231.8009,   33.9545,   9.4143,   11.1156,
$        33.9495,  162.2902,   6.6684,   53.2033,
$        9.4143,    6.6684,  100.322,   71.5384,
$        11.1156,   53.2033,  71.5384,  106.5638  /
REAL(8) :: b(ldb, nrhs)
DATA b / 1.0, 2.0, 3.0, 4.0 /
EXTERNAL DPOSV
CALL DPOSV(uplo, n, nrhs, a, lda, b, ldb, info);
* Output X: 0.002361        0.000904        0.006432        0.032521
```
