線性代數#
The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient low level implementations of standard linear algebra algorithms. Those libraries may be provided by NumPy itself using C versions of a subset of their reference implementations but, when possible, highly optimized libraries that take advantage of specialized processor functionality are preferred. Examples of such libraries are OpenBLAS, MKL (TM), and ATLAS. Because those libraries are multithreaded and processor dependent, environmental variables and external packages such as threadpoolctl may be needed to control the number of threads or specify the processor architecture.
SciPy 函式庫也包含一個 linalg 子模組,SciPy 和 NumPy 子模組提供的功能有重疊。 SciPy 包含一些 numpy.linalg 中沒有的函數,例如與 LU 分解和 Schur 分解相關的函數、多種計算偽逆的方法以及矩陣超越函數(例如矩陣對數)。一些同時存在於兩個函式庫中的函數在 scipy.linalg 中得到了增強。例如,scipy.linalg.eig 可以接受第二個矩陣參數來解廣義特徵值問題。然而,NumPy 中的一些函數具有更靈活的廣播選項。例如,numpy.linalg.solve 可以處理「堆疊」數組,而 scipy.linalg.solve 僅接受單一方陣作為其第一個參數。
備註
本頁中使用的術語「矩陣」指的是二維 numpy.array 對象,而非 numpy.matrix 對象。後者已不再建議使用,即使在線性代數運算中也不建議使用。更多資訊請參閱矩陣物件文件。
@ 運算子#
Introduced in NumPy 1.10.0, the @ operator is preferable to
other methods when computing the matrix product between 2d arrays. The
numpy.matmul function implements the @ operator.
矩陣與向量乘積#
|
兩個陣列的點積。 |
|
在一次函數呼叫中計算兩個或多個陣列的點積,同時自動選擇最快的計算順序。 |
|
傳回兩個向量的點積。 |
|
兩個數組的向量點積。 |
|
計算向量點積。 |
|
兩個陣列的內積。 |
|
計算兩個陣列的外積。 |
|
計算兩個陣列的外積。 |
|
兩個陣列的矩陣乘積。 |
|
計算矩陣乘積。 |
|
Matrix-vector dot product of two arrays. |
|
Vector-matrix dot product of two arrays. |
|
Compute tensor dot product along specified axes. |
|
Compute tensor dot product along specified axes. |
|
Evaluates the Einstein summation convention on the operands. |
|
Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays. |
|
Raise a square matrix to the (integer) power n. |
|
Kronecker product of two arrays. |
|
傳回 3 元向量的叉積。 |
分解#
|
喬列斯基分解。 |
|
計算矩陣的QR分解。 |
|
奇異值分解。 |
|
傳回矩陣(或矩陣堆疊)「`x``的奇異值。 |
矩陣特徵值#
|
計算方陣的特徵值和右邊特徵向量。 |
|
Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix. |
計算一般矩陣的特徵值。 |
|
|
Compute the eigenvalues of a complex Hermitian or real symmetric matrix. |
範數與其他數值#
|
矩陣或向量範數。 |
|
計算矩陣(或一組矩陣堆疊)``x`` 的矩陣範數。 |
|
計算向量(或一組向量)``x`` 的向量範數。 |
|
計算矩陣的條件數。 |
|
計算數組的行列式。 |
|
使用奇異值分解 (SVD) 方法傳回陣列的矩陣秩 |
計算數組行列式的符號和(自然)對數。 |
|
|
傳回數組對角線上的所有元素總和。 |
|
傳回矩陣(或矩陣堆疊)「`x``沿著指定對角線的總和。 |
解方程式和求矩陣逆#
|
解線性矩陣方程式或線性標量方程組。 |
py:obj: |
解張量方程式「a x = b`,求出 x。 |
|
傳回線性矩陣方程式的最小平方法解。 |
|
求反向矩陣。 |
|
計算矩陣的(摩爾-彭羅斯)偽反向。 |
|
計算 N 維數組的「反向」。 |
其他矩陣運算#
|
傳回指定的對角線。 |
|
傳回矩陣(或矩陣堆疊)「`x``的指定對角線。 |
:py:obj:`linalg.matrix_transpose |
轉置矩陣(或矩陣堆疊)``x`` |
例外情況#
由 linalg 函數引發的通用 Python 異常派生物件。 |
同時處理多個矩陣的線性代數問題#
上面列出的幾個線性代數例程能夠一次計算多個矩陣的結果,前提是這些矩陣被堆疊到同一個陣列中。
文件中透過輸入參數規格(例如 a : (..., M, M) array_like)對此進行了說明。這意味著,例如,如果給定一個輸入數組 a.shape == (N, M, M),它將被解釋為 N 個大小為 M×M 的矩陣的「堆疊」。類似的規範也適用於傳回值,例如,行列式的 det : (...) 在這種情況下將傳回形狀為 det(a).shape == (N,) 的陣列。這可以推廣到對高維數組進行線性代數運算:多維數組的最後 1 或 2 個維度將根據每個運算的需要解釋為向量或矩陣。