Matrix functions¶
The group of matrix functions comprises functions for vector and matrix operations and also functions for sparse matrix handling. In Python, these operations are primarily handled by NumPy arrays and SciPy sparse matrices. NumPy provides dense arrays while SciPy provides sparse matrix functionality. Unlike MATLAB, Python distinguishes between different sparse matrix formats (CSR, CSC, COO, etc.) for different use cases.
If you want use the functions described in this chapter, you need to import the NumPy and SciPy libraries. It is common practice to import these libraries with the following aliases:
import numpy as np
import scipy.sparse as sp
import matplotlib.pyplot as plt
The following functions are described in this chapter:
Vector and matrix operations
|
Special characters |
|
Special characters |
|
Create arrays and do array indexing |
|
Array arithmetic |
|
Absolute value |
|
Matrix determinant |
|
Diagonal arrays and diagonals of an array |
|
Matrix inverse |
|
Array length |
|
Maximum element(s) of an array |
|
Minimum element(s) of an array |
|
Generate an array of all ones |
|
Array dimensions |
|
Square root |
|
Sum of the elements of an array |
|
Generate a zero array |
Sparse matrix handling
|
Convert sparse matrix to dense array |
|
Create sparse matrix |
|
Visualize sparsity structure |
Special characters [ ] ( ) = ‘ , ;¶
- Purpose:
Special characters for array operations.
- Syntax:
[ ] ( ) = ' , ;
- Description:
- [ ]
Brackets are used to create lists and arrays, and for indexing.
- ( )
Parentheses are used to indicate precedence in arithmetic expressions, for function calls, and to create tuples.
- =
Used in assignment statements.
- ‘
String delimiter. For matrix transpose, use
.Tornp.transpose().- ,
Comma. Used to separate array indices and function arguments.
- ;
Semicolon. Used to separate statements on the same line (not commonly used in Python).
- Examples:
By the statement:
a = 2the scalar
ais assigned a value of 2. An element in an array may be assigned a value according to:A[1, 4] = 3 # Note: Python uses 0-based indexingThe statement:
D = np.array([[1, 2], [3, 4]])results in array:
\[\begin{split}\mathbf{D} = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\end{split}\]stored in memory. To copy the contents of the array
Dto an arrayE, use:E = D.copy() # or E = np.copy(D)The transpose of array
Ais stored in a new arrayFusing:F = A.T # or F = np.transpose(A)- Note:
These are Python built-in syntax elements. NumPy must be imported as
import numpy as np.
: (slice operator) and np.arange¶
- Purpose:
Create arrays and do array indexing/slicing.
- Description:
In Python with NumPy, array creation and slicing use different syntax:
- np.arange(j, k)
creates array [
j,j + 1, ... , k-1] (note: k is excluded)- np.arange(j, k, i)
creates array [
j,j + i, j + 2i, ... `` ] up to but not including ``k
The colon notation is used for array slicing:
- A[ : , j ]
is the
j:th column ofA- A[ i , : ]
is the
i:th row ofA
- Examples:
To create a sequence similar to MATLAB’s
1:4:d = np.arange(1, 5) # Creates [1, 2, 3, 4]results in a 1D array:
\[\mathbf{d} = \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix}\]stored in memory.
Array slicing is used to access selected rows and columns. For example, if we have created a 3×4 array
D:D = np.array([d, 2*d, 3*d])resulting in:
\[\begin{split}\mathbf{D} = \begin{bmatrix} 1 & 2 & 3 & 4 \\ 2 & 4 & 6 & 8 \\ 3 & 6 & 9 & 12 \end{bmatrix}\end{split}\]columns two and three (indices 2:4) are accessed by:
D[:, 2:4] # Note: 0-based indexing, end index excludedresulting in:
\[\begin{split}\mathbf{D}[:, 2:4] = \begin{bmatrix} 3 & 4 \\ 6 & 8 \\ 9 & 12 \end{bmatrix}\end{split}\]To copy parts of array
Dinto another array, use slicing:E[2:4, 1:3] = D[0:2, 2:4] # 0-based indexingAssuming array
Ewas a zero array before the statement, the result will be:\[\begin{split}\mathbf{E} = \begin{bmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 3 & 4 & 0 \\ 0 & 6 & 8 & 0 \end{bmatrix}\end{split}\]- Note:
These are Python/NumPy built-in operations. Note the 0-based indexing and exclusive end indices.
/ @ (arithmetic operators)
- Purpose:
Array arithmetic operations.
- Syntax:
A + B
A - B
A * B # element-wise multiplication
A @ B # matrix multiplication
A / s # element-wise division
A // s # floor division
- Description:
NumPy array operations can be element-wise or matrix operations. Use
@for matrix multiplication and*for element-wise multiplication.- Examples:
An example of a sequence of array-to-array operations is:
D = A + B - CA matrix-to-vector multiplication followed by a vector-to-vector subtraction may be defined by the statement:
b = c - A @ x # Use @ for matrix multiplicationand finally, to scale an array by a scalar
swe may use:B = A / s- Note:
These are NumPy array operations. For matrix multiplication, use
@ornp.dot().
np.abs¶
- Purpose:
Absolute value.
- Syntax:
B = np.abs(A)
- Description:
B = np.abs(A)computes the absolute values of the elements of arrayAand stores them in arrayB.- Examples:
Assume the array:
\[\begin{split}\mathbf{C} = \begin{bmatrix} -7 & 4 \\ -3 & -8 \end{bmatrix}\end{split}\]The statement:
D = np.abs(C)results in an array:
\[\begin{split}\mathbf{D} = \begin{bmatrix} 7 & 4 \\ 3 & 8 \end{bmatrix}\end{split}\]stored in memory.
- Note:
This is a NumPy function. Alternative:
np.absolute(). For more information, see NumPy documentation.
np.linalg.det¶
- Purpose:
Matrix determinant.
- Syntax:
a = np.linalg.det(A)
- Description:
a = np.linalg.det(A)computes the determinant of the square arrayAand stores it in the scalara.- Note:
This is a NumPy linear algebra function. For more information, see NumPy documentation.
np.diag¶
- Purpose:
Diagonal arrays and diagonals of an array.
- Syntax:
M = np.diag(v)
v = np.diag(M)
- Description:
For a 1D array
vwith n elements, the statementM = np.diag(v)results in an n × n arrayMwith the elements ofvas the main diagonal.For a 2D array
M, the statementv = np.diag(M)results in a 1D arrayvformed by the main diagonal ofM.- Note:
This is a NumPy function. Use
np.diagflat()for a more general version that works with multi-dimensional input.
.toarray() / .todense()¶
- Purpose:
Convert sparse matrices to dense arrays.
- Syntax:
A = S.toarray() # Convert to NumPy array
A = S.todense() # Convert to matrix (deprecated)
- Description:
A = S.toarray()converts a sparse matrixSto a dense NumPy arrayA.A = S.todense()converts to a matrix object (deprecated, usetoarray()).- Note:
These are methods of scipy.sparse matrices. Use
toarray()for better compatibility with NumPy operations.
np.linalg.inv¶
- Purpose:
Matrix inverse.
- Syntax:
B = np.linalg.inv(A)
- Description:
B = np.linalg.inv(A)computes the inverse of the square arrayAand stores the result in the arrayB.- Note:
This is a NumPy linear algebra function. For better numerical stability, consider using
np.linalg.pinv()for pseudo-inverse orscipy.linalg.solve()for solving linear systems.
len / np.size¶
- Purpose:
Array length.
- Syntax:
n = len(x) # For 1D arrays
n = np.size(x) # Total number of elements
n = x.size # Total number of elements
- Description:
n = len(x)returns the length of the first dimension of arrayx.n = np.size(x)returns the total number of elements in arrayx.- Note:
len()is a Python built-in function.np.size()is a NumPy function that’s more similar to MATLAB’slength().
np.max¶
- Purpose:
Maximum element(s) of an array.
- Syntax:
b = np.max(A) # Maximum of entire array
b = np.max(A, axis=0) # Maximum along columns
b = np.max(A, axis=1) # Maximum along rows
- Description:
For a 1D array
a,b = np.max(a)returns the maximum element.For a 2D array
A,b = np.max(A, axis=0)returns an array containing the maximum elements found in each column.The maximum element in an array may be found by
c = np.max(A).- Examples:
Assume the array
Bis defined as:\[\begin{split}\mathbf{B} = \begin{bmatrix} -7 & 4 \\ -3 & -8 \end{bmatrix}\end{split}\]The statement:
d = np.max(B, axis=0)results in an array:
\[\mathbf{d} = \begin{bmatrix} -3 & 4 \end{bmatrix}\]The maximum element in array
Bmay be found bye = np.max(B)which results in the scalare = 4.- Note:
This is a NumPy function. Alternative:
np.amax()orA.max().
np.min¶
- Purpose:
Minimum element(s) of an array.
- Syntax:
b = np.min(A) # Minimum of entire array
b = np.min(A, axis=0) # Minimum along columns
b = np.min(A, axis=1) # Minimum along rows
- Description:
For a 1D array
a,b = np.min(a)returns the minimum element.For a 2D array
A,b = np.min(A, axis=0)returns an array containing the minimum elements found in each column.The minimum element in an array may be found by
c = np.min(A).- Examples:
Assume the array
Bis defined as:\[\begin{split}\mathbf{B} = \begin{bmatrix} -7 & 4 \\ -3 & -8 \end{bmatrix}\end{split}\]The statement:
d = np.min(B, axis=0)results in an array:
\[\mathbf{d} = \begin{bmatrix} -7 & -8 \end{bmatrix}\]The minimum element in array
Bis found bye = np.min(B), which results in the scalare = -8.- Note:
This is a NumPy function. Alternative:
np.amin()orA.min().
np.ones¶
- Purpose:
Generate an array of all ones.
- Syntax:
A = np.ones((m, n))
A = np.ones(shape)
- Description:
A = np.ones((m, n))results in an \(m \times n\) array \(A\) with all ones.A = np.ones(shape)creates an array with the specified shape filled with ones.- Note:
This is a NumPy function. Note the tuple syntax for shape
(m, n).
np.shape / .shape¶
- Purpose:
Array dimensions.
- Syntax:
d = np.shape(A)
d = A.shape
m, n = A.shape
- Description:
d = np.shape(A)ord = A.shapereturns a tuple with the dimensions of arrayA.m, n = A.shapeunpacks the dimensionsmandnof the 2D arrayA.- Note:
.shapeis an array attribute.np.shape()is the function equivalent.
scipy.sparse¶
- Purpose:
Create sparse matrices.
- Syntax:
import scipy.sparse as sp
S = sp.csr_matrix(A) # Convert dense to sparse (CSR format)
S = sp.coo_matrix(A) # Convert to COO format
S = sp.csr_matrix((m, n)) # Create sparse zero matrix
- Description:
S = sp.csr_matrix(A)converts a dense array to sparse CSR (Compressed Sparse Row) format.S = sp.csr_matrix((m, n))creates an m×n sparse zero matrix in CSR format.Other formats available:
csc_matrix(CSC),coo_matrix(COO),lil_matrix(LIL), etc.- Note:
This requires SciPy. Different sparse formats are optimized for different operations (CSR for row operations, CSC for column operations, etc.).
plt.spy¶
- Purpose:
Visualize matrix sparsity structure.
- Syntax:
import matplotlib.pyplot as plt
plt.spy(S)
plt.show()
- Description:
plt.spy(S)plots the sparsity pattern of array or sparse matrixS. Nonzero elements are displayed as markers.Additional parameters like
markersize,marker, etc., can be used to customize the plot.- Note:
This is a matplotlib function. Works with both dense and sparse arrays. For sparse matrices, it’s more efficient than converting to dense first.
np.sqrt¶
- Purpose:
Square root.
- Syntax:
B = np.sqrt(A)
- Description:
B = np.sqrt(A)computes the element-wise square root of arrayAand stores the result in arrayB.- Note:
This is a NumPy function. For complex inputs, it computes the principal square root.
np.sum¶
- Purpose:
Sum of the elements of an array.
- Syntax:
b = np.sum(A) # Sum of all elements
b = np.sum(A, axis=0) # Sum along columns
b = np.sum(A, axis=1) # Sum along rows
- Description:
For a 1D array
a,b = np.sum(a)returns the sum of all elements.For a 2D array
A,b = np.sum(A, axis=0)returns an array containing the sum of elements in each column.The sum of all elements in an array is determined by
c = np.sum(A).- Note:
This is a NumPy function. Alternative:
A.sum()method.
np.zeros¶
- Purpose:
Generate a zero array.
- Syntax:
A = np.zeros((m, n))
A = np.zeros(shape)
- Description:
A = np.zeros((m, n))results in an \(m \times n\) array \(A\) of zeros.A = np.zeros(shape)creates an array with the specified shape filled with zeros.- Note:
This is a NumPy function. Note the tuple syntax for shape
(m, n).