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

np.abs

Absolute value

np.linalg.det

Matrix determinant

np.diag

Diagonal arrays and diagonals of an array

np.linalg.inv

Matrix inverse

len

Array length

np.max

Maximum element(s) of an array

np.min

Minimum element(s) of an array

np.ones

Generate an array of all ones

np.shape

Array dimensions

np.sqrt

Square root

np.sum

Sum of the elements of an array

np.zeros

Generate a zero array

Sparse matrix handling

sparse_matrix.toarray

Convert sparse matrix to dense array

scipy.sparse

Create sparse matrix

plt.spy

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 .T or np.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 = 2

the scalar a is 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 indexing

The 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 D to an array E, use:

E = D.copy()  # or E = np.copy(D)

The transpose of array A is stored in a new array F using:

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 of A

A[ i , : ]

is the i :th row of A

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 excluded

resulting in:

\[\begin{split}\mathbf{D}[:, 2:4] = \begin{bmatrix} 3 & 4 \\ 6 & 8 \\ 9 & 12 \end{bmatrix}\end{split}\]

To copy parts of array D into another array, use slicing:

E[2:4, 1:3] = D[0:2, 2:4]  # 0-based indexing

Assuming array E was 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 - C

A matrix-to-vector multiplication followed by a vector-to-vector subtraction may be defined by the statement:

b = c - A @ x  # Use @ for matrix multiplication

and finally, to scale an array by a scalar s we may use:

B = A / s
Note:

These are NumPy array operations. For matrix multiplication, use @ or np.dot().

np.abs

Purpose:

Absolute value.

Syntax:

B = np.abs(A)
Description:

B = np.abs(A) computes the absolute values of the elements of array A and stores them in array B.

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 array A and stores it in the scalar a.

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 v with n elements, the statement M = np.diag(v) results in an n × n array M with the elements of v as the main diagonal.

For a 2D array M, the statement v = np.diag(M) results in a 1D array v formed by the main diagonal of M.

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 matrix S to a dense NumPy array A.

A = S.todense() converts to a matrix object (deprecated, use toarray()).

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 array A and stores the result in the array B.

Note:

This is a NumPy linear algebra function. For better numerical stability, consider using np.linalg.pinv() for pseudo-inverse or scipy.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 array x.

n = np.size(x) returns the total number of elements in array x.

Note:

len() is a Python built-in function. np.size() is a NumPy function that’s more similar to MATLAB’s length().

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 B is 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 B may be found by e = np.max(B) which results in the scalar e = 4.

Note:

This is a NumPy function. Alternative: np.amax() or A.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 B is 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 B is found by e = np.min(B), which results in the scalar e = -8.

Note:

This is a NumPy function. Alternative: np.amin() or A.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) or d = A.shape returns a tuple with the dimensions of array A.

m, n = A.shape unpacks the dimensions m and n of the 2D array A.

Note:

.shape is 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 matrix S. 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 array A and stores the result in array B.

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).