如何知道何时使用numpy.linalg而不是scipy.linalg?

时间:2021-04-20 21:20:25

Received wisdom is to prefer scipy.linalg over numpy.linalg functions. For doing linear algebra, ideally (and conveniently) I would like to combine the functionalities of numpy.array and scipy.linalg without ever looking towards numpy.linalg. This is not always possible and may become too frustrating.

收到的智慧是喜欢scipy.linalg而不是numpy.linalg函数。对于线性代数,理想情况下(和方便)我想结合numpy.array和scipy.linalg的功能,而不是看numpy.linalg。这并不总是可行的,可能会变得太令人沮丧。

Is there a comparative checklist of equivalent functions from these two modules to quickly determine when to use numpy.linalg in case a function is absent in scipy.linalg?

是否存在来自这两个模块的等效函数的比较清单,以便在scipy.linalg中缺少函数时快速确定何时使用numpy.linalg?

e.g. There are scipy.linalg.norm() and numpy.linalg.norm(), but there seem to be no scipy equivalents of numpy.linalg.matrix_rank() and numpy.linalg.cond().

例如有scipy.linalg.norm()和numpy.linalg.norm(),但似乎没有numpy.linalg.matrix_rank()和numpy.linalg.cond()的scipy等价物。

1 个解决方案

#1


8  

So, the normal rule is to just use scipy.linalg as it generally supports all of the numpy.linalg functionality and more. The documentation says this:

因此,通常的规则是只使用scipy.linalg,因为它通常支持所有numpy.linalg功能等等。文档说明了这一点:

See also

numpy.linalg for more linear algebra functions. Note that although scipy.linalg imports most of them, identically named functions from scipy.linalg may offer more or slightly differing functionality.

numpy.linalg用于更多线性代数函数。请注意,尽管scipy.linalg导入了大部分内容,但scipy.linalg中具有相同名称的函数可能会提供更多或略有不同的功能。

However, matrix_rank() is only in NumPy.

但是,matrix_rank()仅在NumPy中。

Here we can see the differences between the functions provided by both libraries, and how SciPy is more complete:

在这里,我们可以看到两个库提供的功能之间的差异,以及SciPy如何更完整:

In [2]: from scipy import linalg as scipy_linalg
In [3]: from numpy import linalg as numpy_linalg
In [4]: dir(scipy_linalg)
Out[4]:
[
 ...
 'absolute_import',
 'basic',
 'bench',
 'blas',
 'block_diag',
 'cho_factor',
 'cho_solve',
 'cho_solve_banded',
 'cholesky',
 'cholesky_banded',
 'circulant',
 'companion',
 'coshm',
 'cosm',
 'cython_blas',
 'cython_lapack',
 'decomp',
 'decomp_cholesky',
 'decomp_lu',
 'decomp_qr',
 'decomp_schur',
 'decomp_svd',
 'det',
 'dft',
 'diagsvd',
 'division',
 'eig',
 'eig_banded',
 'eigh',
 'eigvals',
 'eigvals_banded',
 'eigvalsh',
 'expm',
 'expm2',
 'expm3',
 'expm_cond',
 'expm_frechet',
 'find_best_blas_type',
 'flinalg',
 'fractional_matrix_power',
 'funm',
 'get_blas_funcs',
 'get_lapack_funcs',
 'hadamard',
 'hankel',
 'helmert',
 'hessenberg',
 'hilbert',
 'inv',
 'invhilbert',
 'invpascal',
 'kron',
 'lapack',
 'leslie',
 'linalg_version',
 'logm',
 'lstsq',
 'lu',
 'lu_factor',
 'lu_solve',
 'matfuncs',
 'misc',
 'norm',
 'ordqz',
 'orth',
 'orthogonal_procrustes',
 'pascal',
 'pinv',
 'pinv2',
 'pinvh',
 'polar',
 'print_function',
 'qr',
 'qr_delete',
 'qr_insert',
 'qr_multiply',
 'qr_update',
 'qz',
 'rq',
 'rsf2csf',
 's',
 'schur',
 'signm',
 'sinhm',
 'sinm',
 'solve',
 'solve_banded',
 'solve_circulant',
 'solve_continuous_are',
 'solve_discrete_are',
 'solve_discrete_lyapunov',
 'solve_lyapunov',
 'solve_sylvester',
 'solve_toeplitz',
 'solve_triangular',
 'solveh_banded',
 'special_matrices',
 'sqrtm',
 'svd',
 'svdvals',
 'tanhm',
 'tanm',
 'test',
 'toeplitz',
 'tri',
 'tril',
 'triu']

In [5]: dir(numpy_linalg)
Out[5]:
[
 ...
 'absolute_import',
 'bench',
 'cholesky',
 'cond',
 'det',
 'division',
 'eig',
 'eigh',
 'eigvals',
 'eigvalsh',
 'info',
 'inv',
 'lapack_lite',
 'linalg',
 'lstsq',
 'matrix_power',
 'matrix_rank',
 'multi_dot',
 'norm',
 'pinv',
 'print_function',
 'qr',
 'slogdet',
 'solve',
 'svd',
 'tensorinv',
 'tensorsolve',
 'test']

In [6]:

Note that not all of these are functions.

请注意,并非所有这些都是功能。

SciPy does provide scipy.linalg.expm_cond(), but this only returns the condition in the Frobenius norm, whereas numpy.linalg.cond() supports multiple norms.

SciPy确实提供了scipy.linalg.expm_cond(),但这只返回Frobenius规范中的条件,而numpy.linalg.cond()支持多个规范。

#1


8  

So, the normal rule is to just use scipy.linalg as it generally supports all of the numpy.linalg functionality and more. The documentation says this:

因此,通常的规则是只使用scipy.linalg,因为它通常支持所有numpy.linalg功能等等。文档说明了这一点:

See also

numpy.linalg for more linear algebra functions. Note that although scipy.linalg imports most of them, identically named functions from scipy.linalg may offer more or slightly differing functionality.

numpy.linalg用于更多线性代数函数。请注意,尽管scipy.linalg导入了大部分内容,但scipy.linalg中具有相同名称的函数可能会提供更多或略有不同的功能。

However, matrix_rank() is only in NumPy.

但是,matrix_rank()仅在NumPy中。

Here we can see the differences between the functions provided by both libraries, and how SciPy is more complete:

在这里,我们可以看到两个库提供的功能之间的差异,以及SciPy如何更完整:

In [2]: from scipy import linalg as scipy_linalg
In [3]: from numpy import linalg as numpy_linalg
In [4]: dir(scipy_linalg)
Out[4]:
[
 ...
 'absolute_import',
 'basic',
 'bench',
 'blas',
 'block_diag',
 'cho_factor',
 'cho_solve',
 'cho_solve_banded',
 'cholesky',
 'cholesky_banded',
 'circulant',
 'companion',
 'coshm',
 'cosm',
 'cython_blas',
 'cython_lapack',
 'decomp',
 'decomp_cholesky',
 'decomp_lu',
 'decomp_qr',
 'decomp_schur',
 'decomp_svd',
 'det',
 'dft',
 'diagsvd',
 'division',
 'eig',
 'eig_banded',
 'eigh',
 'eigvals',
 'eigvals_banded',
 'eigvalsh',
 'expm',
 'expm2',
 'expm3',
 'expm_cond',
 'expm_frechet',
 'find_best_blas_type',
 'flinalg',
 'fractional_matrix_power',
 'funm',
 'get_blas_funcs',
 'get_lapack_funcs',
 'hadamard',
 'hankel',
 'helmert',
 'hessenberg',
 'hilbert',
 'inv',
 'invhilbert',
 'invpascal',
 'kron',
 'lapack',
 'leslie',
 'linalg_version',
 'logm',
 'lstsq',
 'lu',
 'lu_factor',
 'lu_solve',
 'matfuncs',
 'misc',
 'norm',
 'ordqz',
 'orth',
 'orthogonal_procrustes',
 'pascal',
 'pinv',
 'pinv2',
 'pinvh',
 'polar',
 'print_function',
 'qr',
 'qr_delete',
 'qr_insert',
 'qr_multiply',
 'qr_update',
 'qz',
 'rq',
 'rsf2csf',
 's',
 'schur',
 'signm',
 'sinhm',
 'sinm',
 'solve',
 'solve_banded',
 'solve_circulant',
 'solve_continuous_are',
 'solve_discrete_are',
 'solve_discrete_lyapunov',
 'solve_lyapunov',
 'solve_sylvester',
 'solve_toeplitz',
 'solve_triangular',
 'solveh_banded',
 'special_matrices',
 'sqrtm',
 'svd',
 'svdvals',
 'tanhm',
 'tanm',
 'test',
 'toeplitz',
 'tri',
 'tril',
 'triu']

In [5]: dir(numpy_linalg)
Out[5]:
[
 ...
 'absolute_import',
 'bench',
 'cholesky',
 'cond',
 'det',
 'division',
 'eig',
 'eigh',
 'eigvals',
 'eigvalsh',
 'info',
 'inv',
 'lapack_lite',
 'linalg',
 'lstsq',
 'matrix_power',
 'matrix_rank',
 'multi_dot',
 'norm',
 'pinv',
 'print_function',
 'qr',
 'slogdet',
 'solve',
 'svd',
 'tensorinv',
 'tensorsolve',
 'test']

In [6]:

Note that not all of these are functions.

请注意,并非所有这些都是功能。

SciPy does provide scipy.linalg.expm_cond(), but this only returns the condition in the Frobenius norm, whereas numpy.linalg.cond() supports multiple norms.

SciPy确实提供了scipy.linalg.expm_cond(),但这只返回Frobenius规范中的条件,而numpy.linalg.cond()支持多个规范。