Linear algebra is a field of mathematics where we use linear representations in vector spaces. These representations involve matrices, equations, vectors, determinants, and their respective linear transformations.
We use the techniques and concepts of linear algebra in many fields, including computer engineering, data science and machine learning, economics, physics, geometry, and other disciplines that involve mathematical computations.
A matrix is one such important concept in linear algebra. Matrices consist of numbers arranged in rows and columns that represent transformations to ease computations.
Since matrices are numbers arranged in rows and columns, their dimensions sometimes become large and complex.
To solve such complexities in matrices, we need factorization.
Factorization, also known as decomposition, is a technique we use to break down a matrix into smaller components or matrices to speed up computations and, sometimes, even save on memory.
There are different types of factorization techniques depending on the type of matrix. Julia can automatically apply the right kind of factorization technique depending on the matrix type or method specified.
Let’s discuss some matrix factorization types to understand which underlying techniques to apply and when.
There are many types of factorization techniques applicable to matrices. For simplicity, let’s cover three:
LU
matrix factorization techniqueQR
matrix factorization techniqueLU
matrix factorization techniqueThis is applicable to square (nxn) matrices. It decomposes a matrix A
into L
and U
components.
A = LU
QR
matrix factorization techniqueQR
is applicable to non-square (mxn) matrices. It decomposes the matrix A
into Q
and R
components.
A = QR
This technique is applicable to the square but symmetric matrix. It decomposes the matrix A
into into L
, the lower triangular matrix, and L^T
, the transpose of L
.
This technique is especially useful in data science and machine learning, with applications in model optimization.
A = L.L^T
Some practical applications of matrices in Julia are as follows:
factorize
methodJulia automatically checks the best method to apply to a matrix, given its characteristics, and applies it when the factorize
method is used.
using LinearAlgebra
A = randn(3,3)
print(A)
B = factorize(A)
print(B)
MatrixFactorizations
libraryWe use the MatrixFactorizations
library for non-standard matrix factorization techniques, such as QL. It also currently implements other non-standard factorization techniques. These include RQ
(a variant of QR
), UL
(a variant of LU
), and a combined Cholesky factorization with inverse and polar decompositions.
using MatrixFactorizations
A = randn(3,3)
print(A)
print("--------------------------------------------")
B = ql(A)
print(B)
Let's implement code in Julia using linear algebra's factorize
method:
using LinearAlgebraA = randn(3,4)print(A)print("--------------------------------------------")B=factorize(A)print(B)
Let's implement code in Julia using the MatrixFactorizations
library:
using MatrixFactorizationsA = randn(3,4)print(A)print("--------------------------------------------")B = rq(A)print(B)
QR
technique to decompose the matrix.Note: We can experiment with other nxn matrices to see which techniques are used.
MatrixFactorization
library to factorize an mxn matrix using the RQ
technique. RQ
is a variant of QR
, with a difference in the order in which the matrices are decomposed.Note: We can experiment with the
UL
andLQ
techniques and see the results.
Free Resources