numpy - CBLAS/LAPACK 与 Python 中的矩阵求逆

标签 numpy matrix lapack matrix-inverse cblas

我试图反转的矩阵是:

    [ 1  0  1]
A = [ 2  0  1]
    [-1  1  1] 

真正的逆是:

       [-1  1  0]
A^-1 = [-3  2  1]
       [ 2 -1  0]

使用Python的numpy.linalg.inv,我得到了正确的答案。我的矩阵求逆例程之一使用 dgetri_,它是:

void compute_matrix_inverse_dbl( double* matrix,
                                 int order,
                                 double * inverse )
{

    int N, lwork;
    int success;
    int *pivot;
    double* workspace;

    //===Allocate Space===//
    pivot = malloc(order * order * order * sizeof(*pivot));
    workspace = malloc(order * order * sizeof(*workspace));

    //===Run Setup===//
    N = order;
    copy_array_dbl(matrix, order*order, inverse);
    lwork = order*order;

    //===Factor Matrix===//
    dgetrf_(&N,&N,inverse,&N,pivot,&success);

    //===Compute Inverse===//
    dgetri_(&N, inverse, &N, pivot, workspace, &lwork, &success);

    //===Clean Up===//
    free(workspace);
    free(pivot);

    return;
  }

使用这个例程,我得到:

       [-1   1 +-e1 ]
A^-1 = [-3   2   1  ]
       [ 2  -1 +-e2 ]

其中 e1 和 e2 以及机器精度 1e-16 量级上的小数。现在也许 dgetri_ 不是最好使用的。然而,当我通过 zgeqrf_ 和 zungqr_ 使用 QR 分解进行反转时,我得到了类似的答案。当我使用 dgesvd_ 使用 SVD 进行逆时,我也得到了类似的答案。

Python 似乎使用一个名为 _umath_linalg.inv 的例程。所以我有几个问题:

  • 该例程的作用是什么?
  • 我可以使用什么 CBLAS/LAPACK 例程来反转该矩阵并获得类似 CBLAS/LAPACK 的结果(这样 e1 和 e2 就会被正确的零替换)?

最佳答案

看来numpy.linalg.invscipy.linalg.inv 的精简版根据描述:

This module is a lite version of the linalg.py module in SciPy which contains high-level Python interface to the LAPACK library.

查看scipy.linalg.inv ,它调用 getrf,然后调用 getri

关于numpy - CBLAS/LAPACK 与 Python 中的矩阵求逆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378307/

相关文章:

python - curve_fit 包含 numpy 数组的 2D 函数 --> 形状 (3,3,9) 和 (3,1) 未对齐 : 9 (dim 2) ! = 3 (dim 0)

javascript 如何使对象内联

python - 将 sympy 表达式分解为矩阵系数?

c - Scalapack中处理器子集的不相交网格及其通信

Python单杠双图

python - Torch 张量非连续索引(类似于 Numpy)

python - Seaborn regplot 使用 datetime64 作为 x 轴

arrays - 从 csv 文件加载矩阵 - golang

math - 如何在 Julia 中调用 LAPACK 代码 (cpbtrf)

lisp - 通过 Scheme 的 Lapack