Linear algebra: Difference between revisions
From Master of Neuroscience Wiki
Created page with "== The goal == Overview over the linear algebra functions of Numpy. Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''There more functions in the [https://docs.scipy.org/doc/scipy/reference/linalg.html scipy linalg package]!!!''' == The @ operator == @ => Matrix product * => outer product A[i] * B[i] The @ operator is preferable to other methods when computing the matrix product between 2d arrays. The numpy.matmul function implements the @ oper..." |
(No difference)
|
Revision as of 12:57, 17 October 2025
The goal
Overview over the linear algebra functions of Numpy.
Questions to David Rotermund
There more functions in the scipy linalg package!!!
The @ operator
@ => Matrix product
- => outer product A[i] * B[i]
The @ operator is preferable to other methods when computing the matrix product between 2d arrays. The numpy.matmul function implements the @ operator.
import numpy as np
a = np.arange(0, 3).reshape(3, 1)
b = np.arange(4, 7).reshape(1, 3)
print(a)
print()
print(b)
print()
print(a @ b)
print()
print(b @ a)
Output:
[[0]
[1]
[2]]
[[4 5 6]]
[[ 0 0 0]
[ 4 5 6]
[ 8 10 12]]
[[17]]
For comparison the outer product:
import numpy as np
a = np.arange(0, 3).reshape(3, 1)
b = np.arange(4, 7).reshape(1, 3)
print(a * b)
print()
print(b * a)
Output:
[[ 0 0 0]
[ 4 5 6]
[ 8 10 12]]
[[ 0 0 0]
[ 4 5 6]
[ 8 10 12]]
numpy.linalg
Matrix and vector products
| dot(a, b[, out)] | Dot product of two arrays. |
| linalg.multi_dot(arrays, *[, out)] | Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order. |
| vdot(a, b, /) | Return the dot product of two vectors. |
| inner(a, b, /) | Inner product of two arrays. |
| outer(a, b[, out)] | Compute the outer product of two vectors. |
| matmul(x1, x2, /[, out, casting, order, …)] | Matrix product of two arrays. |
| tensordot(a, b[, axes)] | Compute tensor dot product along specified axes. |
| einsum(subscripts, *operands[, out, dtype, …)] | Evaluates the Einstein summation convention on the operands. |
| einsum_path(subscripts, *operands[, optimize)] | Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays. |
| linalg.matrix_power(a, n) | Raise a square matrix to the (integer) power n. |
| kron(a, b) | Kronecker product of two arrays. |
Decompositions
| linalg.cholesky(a) | Cholesky decomposition. |
| linalg.qr(a[, mode)] | Compute the qr factorization of a matrix. |
| linalg.svd(a[, full_matrices, compute_uv, …)] | Singular Value Decomposition. |
Matrix eigenvalues
| linalg.eig(a) | Compute the eigenvalues and right eigenvectors of a square array. |
| linalg.eigh(a[, UPLO)] | Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix. |
| linalg.eigvals(a) | Compute the eigenvalues of a general matrix. |
| linalg.eigvalsh(a[, UPLO)] | Compute the eigenvalues of a complex Hermitian or real symmetric matrix. |
Norms and other numbers
| linalg.norm(x[, ord, axis, keepdims)] | Matrix or vector norm. |
| linalg.cond(x[, p)] | Compute the condition number of a matrix. |
| linalg.det(a) | Compute the determinant of an array. |
| linalg.matrix_rank(A[, tol, hermitian)] | Return matrix rank of array using SVD method |
| linalg.slogdet(a) | Compute the sign and (natural) logarithm of the determinant of an array. |
| trace(a[, offset, axis1, axis2, dtype, out)] | Return the sum along diagonals of the array. |
Solving equations and inverting matrices
| linalg.solve(a, b) | Solve a linear matrix equation, or system of linear scalar equations. |
| linalg.tensorsolve(a, b[, axes)] | Solve the tensor equation a x = b for x. |
| linalg.lstsq(a, b[, rcond)] | Return the least-squares solution to a linear matrix equation. |
| linalg.inv(a) | Compute the (multiplicative) inverse of a matrix. |
| linalg.pinv(a[, rcond, hermitian)] | Compute the (Moore-Penrose) pseudo-inverse of a matrix. |
| linalg.tensorinv(a[, ind)] | Compute the ‘inverse’ of an N-dimensional array. |