Linear Algebra functions
The NumPy package has numpy.linalg
module which provides linear algebra functionalities like calculating vector products, matrix eigenvalues, solving linear equations, and inverse of a matrix.
- dot()
- multi_dot()
- vdot()
- inner()
- outer()
- matmul()
- matrix_power()
- eig()
- eigh()
- det()
- matrix_rank()
- trace()
- solve()
- inv()
dot():
The dot() function of NumPy returns the dot product of two the input arrays.
Syntax:
dot(a, b)
The a and b parameters are the input arrays.
import numpy as np
a = np.arange(1, 5).reshape(2, 2)
b = np.arange(2, 9, 2).reshape(2, 2)
print(np.dot(a, b))
#Output:
[[14 20]
[30 44]]
multi_dot():
The multi_dot of numpy.linalg
module computes the dot product of two or more input arrays while automatically selecting the fastest evaluation order.
Syntax:
linalg.multi_dot(arrays)
The arrays parameter is the sequence of input arrays.
a = np.arange(1, 5).reshape(2, 2)
b = np.arange(2, 9, 2).reshape(2, 2)
c = np.arange(10, 50, 10).reshape(2, 2)
print(np.linalg.multi_dot([a, b, c]))
#Output:
[[ 740 1080]
[1620 2360]]
vdot():
The vdot() function returns the dot product of two input vectors.
Syntax:
vdot(a, b)
The a and b parameters are the input vector arrays. If the first vector is complex the complex conjugate of the first vector is used for the calculation of the dot product.
a = np.array([7+2j, 9+4j])
b = np.array([3+6j, 2+8j])
print(np.vdot(a, b))
#Output:
(83+100j)
print(np.vdot(b, a))
#Output:
(83-100j)
inner():
The inner() function returns the inner product of two arrays. The inner product of vectors for 1-D arrays (without complex conjugation) is a scalar dot product, in higher dimensions the inner product is a sum of product over the last axes.
Syntax:
inner(a, b)
The a and b parameters are the input arrays. If a and b are nonscalar, their last dimensions must match.
a = np.array([1,2,3])
b = np.array([4,0,0])
c = np.arange(2, 16, 4).reshape(2,2)
d = np.array([2, 3])
print(np.inner(a, b))
#Output:
4
print(np.inner(c, d))
#Output:
[22 62]
outer():
The outer() function calculates the outer product of two vectors. The outer product is the multiplication of each vector element of the first vector with all elements of another vector.
Syntax:
outer(a, b)
The a and b parameters are the input vector arrays and are flattened(if not 1-dimensional) before computing the outer product.
a = np.array([1,2,3])
b = np.array([4,0,0])
c = np.arange(2, 16, 4).reshape(2,2)
d = np.array([2, 3])
print(np.outer(a, b))
#Output:
[[ 4 0 0]
[ 8 0 0]
[12 0 0]]
print(np.outer(c, d))
#Output:
[[ 4 6]
[12 18]
[20 30]
[28 42]]
matmul():
The matmul() function returns the matrix product of two arrays.
Syntax:
matmul(x1, x2)
The x1 and x2 parameters are the input arrays.
x = np.arange(9).reshape(3,3)
y = np.array([[1, 2, 3], [0, 2, 4], [1, 0, 1]])
print(np.matmul(x, y))
#Output:
[[ 2 2 6]
[ 8 14 30]
[14 26 54]]
matrix_power():
The matrix_power() function of numpy.linalg module computes an input square matrix raised to a given integer value.
Syntax:
linalg.matrix_power(a, n)
The a parameter is the matrix array to be powered and the n parameter is the exponent.
a = np.array([[0, 1], [2, 3]])
print(np.linalg.matrix_power(a, 3)) #computes a*a*a
#Output:
[[ 6 11]
[22 39]]
eig():
The eig() function is used to calculate the eigenvalues and right eigenvectors of a given square array.
Syntax:
linalg.eig(a)
The a parameter is the square matrix array for which the eigenvalues and right eigenvectors are computed.
a = np.array([[1, 2], [3, 4]])
print(np.linalg.eig(a))
#Output:
(array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))
eigh():
The eigh() function returns the eigenvalues and eigenvectors of a complex hermitian (conjugate symmetric) or a real symmetric matrix.
Syntax:
linalg.eigh(a)
The a parameter is the square hermitian or real symmetric matrix.
a = np.array([[1+2j, 3-4j], [5-6j, 7+8j]])
print(np.linalg.eigh(a))
#Output:
(array([-4.36660027, 12.36660027]), array([[-0.82418705+0.j , -0.56631767+0.j ],
[ 0.36254774-0.43505729j, -0.52763169+0.63315803j]]))
det():
The det() function returns the determinant of an input square matrix array.
Syntax:
linalg.det(a)
The a parameter is the input square matrix.
a = np.array([[3, 1, 2], [4, 5, 6], [9,7, 1]])
print(np.linalg.det(a))
#Output:
-95.0
matrix_rank():
The matrix_rank() function of numpy.linalg module calculates the matrix rank of the input array using the singular value decomposition(SVD) method.
Syntax:
linalg.matrix_rank(M)
The M parameter is the input matrix.
a = np.array([[3, 1, 2], [4, 2, 6], [3,0, 1]])
print(np.linalg.matrix_rank(a))
#Output:
3
trace():
The trace() function returns the sum along the diagonals of the input matrix.
Syntax:
trace(a)
The a parameter is the input matrix.
a = np.array([[3, 1, 2], [4, 8, 6], [3,0, 1]])
print(np.trace(a))
#Output:
12
solve():
The solve() function solves a linear matrix equation or system of linear scalar equations given as the input matrices.
Syntax:
linalg.solve(a, b)
The a and b parameters are the input matrices, the first matrix is a coefficient matrix, and the other matrix is the ordinate or “dependent variable” values.
a = np.array([[1, 2, 4], [3, 5, 2], [8, 0, 6]])
b = np.array([4, 7, 11])
print(np.linalg.solve(a, b))
#Output:
[1.04477612 0.59701493 0.44029851]
inv():
The inv() function computes the inverse(multiplicative inverse) of the input matrix.
Syntax:
linalg.inv(a)
The a parameter is the input matrix.
a = np.array([[3, 1, 2], [4, 8, 6], [3,0, 1]])
print(np.linalg.inv(a))
#Output:
[[-0.8 0.1 1. ]
[-1.4 0.3 1. ]
[ 2.4 -0.3 -2. ]]
Matrix functions
The NumPy package has the following matrix functionalities in the numpy.matlib
module.
- mat()
- eye()
- identity()
- repmat()
- rand()
- randn()
mat():
The mat() function is used to interpret the input array as a matrix. The mat()
function does not make a copy of the input if the input is already a matrix or a ndarray.
Syntax:
mat(data, dtype=None)
The data parameter is the input array and the dtype parameter is the datatype of the output matrix.
a = np.arange(1, 10).reshape(3, 3)
print(np.mat(a))
#Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
eye():
The eye() function of numpy.matlib module returns a matrix with ones on the diagonal and zeros elsewhere.
Syntax:
matlib.eye(n)
The n parameter is the number of rows in the output.
import numpy.matlib
print(np.matlib.eye(4))
#Output:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
identity():
The identity() function returns the square identity matrix of a given size. The identity()
function is similar to eye()
function.
Syntax:
matlib.identity(n)
The n parameter is the size of the output identity matrix.
print(np.matlib.identity(2))
#Output:
[[1. 0.]
[0. 1.]]
repmat():
The repmat() function is used to repeat a 0-dimensional array to 2-dimensional array(or matrix m x n times).
Syntax:
matlib.repmat(a, m, n)
The a parameter is the array or matrix to be repeated. The m and n parameters are the number of times a is repeated along the first and second axes.
a = np.array(1)
print(np.matlib.repmat(a, 2, 4))
#Output:
[[1 1 1 1]
[1 1 1 1]]
rand():
The rand() function returns a matrix of random values with a given shape.
Syntax:
matlib.rand(*args)
The *args parameter is the shape of the output matrix.
print(np.matlib.rand((2, 2)))
#Output:
[[0.24949472 0.43162869]
[0.49085065 0.65858901]]
randn():
The randn() function returns a random matrix with data from the “standard normal” distribution. The randn()
function generates a matrix filled with random float values sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1.
Syntax:
matlib.randn(*args)
The *args parameter is the shape of the output matrix.
print(np.matlib.randn((4, 2)))
#Output:
[[ 1.62749415 -0.48324226]
[ 0.60811452 -0.46034121]
[-1.48062959 0.88876053]
[ 0.15761015 0.19533977]]