Linear Algebra
In addition to (and as part of) its support for multi-dimensional arrays, Julia provides native implementations of many common and useful linear algebra operations which can be loaded with using LinearAlgebra. Basic operations, such as tr, det, and inv are all supported:
1. julia> A = [1 2 3; 4 1 6; 7 8 1]
2. 3×3 Matrix{Int64}:
3. 1 2 3
4. 4 1 6
5. 7 8 1
7. julia> tr(A)
8. 3
10. julia> det(A)
11. 104.0
13. julia> inv(A)
14. 3×3 Matrix{Float64}:
15. -0.451923 0.211538 0.0865385
16. 0.365385 -0.192308 0.0576923
17. 0.240385 0.0576923 -0.0673077
As well as other useful operations, such as finding eigenvalues or eigenvectors:
1. julia> A = [-4. -17.; 2. 2.]
2. 2×2 Matrix{Float64}:
3. -4.0 -17.0
4. 2.0 2.0
6. julia> eigvals(A)
7. 2-element Vector{ComplexF64}:
8. -1.0 - 5.0im
9. -1.0 + 5.0im
11. julia> eigvecs(A)
12. 2×2 Matrix{ComplexF64}:
13. 0.945905-0.0im 0.945905+0.0im
14. -0.166924+0.278207im -0.166924-0.278207im
In addition, Julia provides many factorizations which can be used to speed up problems such as linear solve or matrix exponentiation by pre-factorizing a matrix into a form more amenable (for performance or memory reasons) to the problem. See the documentation on factorize for more information. As an example:
1. julia> A = [1.5 2 -4; 3 -1 -6; -10 2.3 4]
2. 3×3 Matrix{Float64}:
3. 1.5 2.0 -4.0
4. 3.0 -1.0 -6.0
5. -10.0 2.3 4.0
7. julia> factorize(A)
8. LU{Float64, Matrix{Float64}, Vector{Int64}}
9. L factor:
10. 3×3 Matrix{Float64}:
11. 1.0 0.0 0.0
12. -0.15 1.0 0.0
13. -0.3 -0.132196 1.0
14. U factor:
15. 3×3 Matrix{Float64}:
16. -10.0 2.3 4.0
17. 0.0 2.345 -3.4
18. 0.0 0.0 -5.24947
Since A is not Hermitian, symmetric, triangular, tridiagonal, or bidiagonal, an LU factorization may be the best we can do. Compare with:
1. julia> B = [1.5 2 -4; 2 -1 -3; -4 -3 5]
2. 3×3 Matrix{Float64}:
3. 1.5 2.0 -4.0
4. 2.0 -1.0 -3.0
5. -4.0 -3.0 5.0
7. julia> factorize(B)
8. BunchKaufman{Float64, Matrix{Float64}, Vector{Int64}}
9. D factor:
10. 3×3 Tridiagonal{Float64, Vector{Float64}}:
11. -1.64286 0.0 ⋅
12. 0.0 -2.8 0.0
13. ⋅ 0.0 5.0
14. U factor:
15. 3×3 UnitUpperTriangular{Float64, Matrix{Float64}}:
16. 1.0 0.142857 -0.8
17. ⋅ 1.0 -0.6
18. ⋅ ⋅ 1.0
19. permutation:
20. 3-element Vector{Int64}:
21. 1
22. 2
23. 3
Here, Julia was able to detect that B is in fact symmetric, and used a more appropriate factorization. Often it’s possible to write more efficient code for a matrix that is known to have certain properties e.g. it is symmetric, or tridiagonal. Julia provides some special types so that you can “tag” matrices as having these properties. For instance:
1. julia> B = [1.5 2 -4; 2 -1 -3; -4 -3 5]
2. 3×3 Matrix{Float64}:
3. 1.5 2.0 -4.0
4. 2.0 -1.0 -3.0
5. -4.0 -3.0 5.0
7. julia> sB = Symmetric(B)
8. 3×3 Symmetric{Float64, Matrix{Float64}}:
9. 1.5 2.0 -4.0
10. 2.0 -1.0 -3.0
11. -4.0 -3.0 5.0
sB has been tagged as a matrix that’s (real) symmetric, so for later operations we might perform on it, such as eigenfactorization or computing matrix-vector products, efficiencies can be found by only referencing half of it. For example:
1. julia> B = [1.5 2 -4; 2 -1 -3; -4 -3 5]
2. 3×3 Matrix{Float64}:
3. 1.5 2.0 -4.0
4. 2.0 -1.0 -3.0
5. -4.0 -3.0 5.0
7. julia> sB = Symmetric(B)
8. 3×3 Symmetric{Float64, Matrix{Float64}}:
9. 1.5 2.0 -4.0
10. 2.0 -1.0 -3.0
11. -4.0 -3.0 5.0
13. julia> x = [1; 2; 3]
14. 3-element Vector{Int64}:
15. 1
16. 2
17. 3
19. julia> sB\x
20. 3-element Vector{Float64}:
21. -1.7391304347826084
22. -1.1086956521739126
23. -1.4565217391304346
The \ operation here performs the linear solution. The left-division operator is pretty powerful and it’s easy to write compact, readable code that is flexible enough to solve all sorts of systems of linear equations.
Special matrices
Matrices with special symmetries and structures arise often in linear algebra and are frequently associated with various matrix factorizations. Julia features a rich collection of special matrix types, which allow for fast computation with specialized routines that are specially developed for particular matrix types.
The following tables summarize the types of special matrices that have been implemented in Julia, as well as whether hooks to various optimized methods for them in LAPACK are available.
| Type | Description |
|---|---|
| Symmetric | Symmetric matrix |
| Hermitian | Hermitian matrix |
| UpperTriangular | Upper triangular matrix |
| UnitUpperTriangular | Upper triangular matrix with unit diagonal |
| LowerTriangular | Lower triangular matrix |
| UnitLowerTriangular | Lower triangular matrix with unit diagonal |
| UpperHessenberg | Upper Hessenberg matrix |
| Tridiagonal | Tridiagonal matrix |
| SymTridiagonal | Symmetric tridiagonal matrix |
| Bidiagonal | Upper/lower bidiagonal matrix |
| Diagonal | Diagonal matrix |
| UniformScaling | Uniform scaling operator |
Elementary operations
| Matrix type | + |
- |
* |
\ |
Other functions with optimized methods |
|---|---|---|---|---|---|
| Symmetric | MV | inv, sqrt, exp | |||
| Hermitian | MV | inv, sqrt, exp | |||
| UpperTriangular | MV | MV | inv, det, logdet | ||
| UnitUpperTriangular | MV | MV | inv, det, logdet | ||
| LowerTriangular | MV | MV | inv, det, logdet | ||
| UnitLowerTriangular | MV | MV | inv, det, logdet | ||
| UpperHessenberg | MM | inv, det | |||
| SymTridiagonal | M | M | MS | MV | eigmax, eigmin |
| Tridiagonal | M | M | MS | MV | |
| Bidiagonal | M | M | MS | MV | |
| Diagonal | M | M | MV | MV | inv, det, logdet, / |
| UniformScaling | M | M | MVS | MVS | / |
Legend:
| Key | Description |
|---|---|
| M (matrix) | An optimized method for matrix-matrix operations is available |
| V (vector) | An optimized method for matrix-vector operations is available |
| S (scalar) | An optimized method for matrix-scalar operations is available |
Matrix factorizations
| Matrix type | LAPACK | eigen | eigvals | eigvecs | svd | svdvals |
|---|---|---|---|---|---|---|
| Symmetric | SY | ARI | ||||
| Hermitian | HE | ARI | ||||
| UpperTriangular | TR | A | A | A | ||
| UnitUpperTriangular | TR | A | A | A | ||
| LowerTriangular | TR | A | A | A | ||
| UnitLowerTriangular | TR | A | A | A | ||
| SymTridiagonal | ST | A | ARI | AV | ||
| Tridiagonal | GT | |||||
| Bidiagonal | BD | A | A | |||
| Diagonal | DI | A |
Legend:
| Key | Description | Example |
|---|---|---|
| A (all) | An optimized method to find all the characteristic values and/or vectors is available | e.g. eigvals(M) |
| R (range) | An optimized method to find the ilth through the ihth characteristic values are available |
eigvals(M, il, ih) |
| I (interval) | An optimized method to find the characteristic values in the interval [vl, vh] is available |
eigvals(M, vl, vh) |
| V (vectors) | An optimized method to find the characteristic vectors corresponding to the characteristic values x=[x1, x2,…] is available |
eigvecs(M, x) |
The uniform scaling operator
A UniformScaling operator represents a scalar times the identity operator, λ*I. The identity operator I is defined as a constant and is an instance of UniformScaling. The size of these operators are generic and match the other matrix in the binary operations +, -, * and [](/read/julia-1.10-en/5750f1ec061207b6.md#Base.:\-Tuple{Any, Any}). For A+I and A-I this means that A must be square. Multiplication with the identity operator I is a noop (except for checking that the scaling factor is one) and therefore almost without overhead.
To see the UniformScaling operator in action:
1. julia> U = UniformScaling(2);
3. julia> a = [1 2; 3 4]
4. 2×2 Matrix{Int64}:
5. 1 2
6. 3 4
8. julia> a + U
9. 2×2 Matrix{Int64}:
10. 3 2
11. 3 6
13. julia> a * U
14. 2×2 Matrix{Int64}:
15. 2 4
16. 6 8
18. julia> [a U]
19. 2×4 Matrix{Int64}:
20. 1 2 2 0
21. 3 4 0 2
23. julia> b = [1 2 3; 4 5 6]
24. 2×3 Matrix{Int64}:
25. 1 2 3
26. 4 5 6
28. julia> b - U
29. ERROR: DimensionMismatch: matrix is not square: dimensions are (2, 3)
30. Stacktrace:
31. [...]
If you need to solve many systems of the form (A+μI)x = b for the same A and different μ, it might be beneficial to first compute the Hessenberg factorization F of A via the hessenberg function. Given F, Julia employs an efficient algorithm for (F+μ*I) \ b (equivalent to (A+μ*I)x \ b) and related operations like determinants.
Matrix factorizations
Matrix factorizations (a.k.a. matrix decompositions) compute the factorization of a matrix into a product of matrices, and are one of the central concepts in (numerical) linear algebra.
The following table summarizes the types of matrix factorizations that have been implemented in Julia. Details of their associated methods can be found in the Standard functions section of the Linear Algebra documentation.
| Type | Description |
|---|---|
BunchKaufman |
Bunch-Kaufman factorization |
Cholesky |
Cholesky factorization |
CholeskyPivoted |
Pivoted Cholesky factorization |
LDLt |
LDL(T) factorization |
LU |
LU factorization |
QR |
QR factorization |
QRCompactWY |
Compact WY form of the QR factorization |
QRPivoted |
Pivoted QR factorization |
LQ |
QR factorization of transpose(A) |
Hessenberg |
Hessenberg decomposition |
Eigen |
Spectral decomposition |
GeneralizedEigen |
Generalized spectral decomposition |
SVD |
Singular value decomposition |
GeneralizedSVD |
Generalized SVD |
Schur |
Schur decomposition |
GeneralizedSchur |
Generalized Schur decomposition |
Adjoints and transposes of Factorization objects are lazily wrapped in AdjointFactorization and TransposeFactorization objects, respectively. Generically, transpose of real Factorizations are wrapped as AdjointFactorization.
Orthogonal matrices (AbstractQ)
Some matrix factorizations generate orthogonal/unitary “matrix” factors. These factorizations include QR-related factorizations obtained from calls to qr, i.e., QR, QRCompactWY and QRPivoted, the Hessenberg factorization obtained from calls to hessenberg, and the LQ factorization obtained from lq. While these orthogonal/unitary factors admit a matrix representation, their internal representation is, for performance and memory reasons, different. Hence, they should be rather viewed as matrix-backed, function-based linear operators. In particular, reading, for instance, a column of its matrix representation requires running “matrix”-vector multiplication code, rather than simply reading out data from memory (possibly filling parts of the vector with structural zeros). Another clear distinction from other, non-triangular matrix types is that the underlying multiplication code allows for in-place modification during multiplication. Furthermore, objects of specific AbstractQ subtypes as those created via qr, hessenberg and lq can behave like a square or a rectangular matrix depending on context:
1. julia> using LinearAlgebra
3. julia> Q = qr(rand(3,2)).Q
4. 3×3 LinearAlgebra.QRCompactWYQ{Float64, Matrix{Float64}, Matrix{Float64}}
6. julia> Matrix(Q)
7. 3×2 Matrix{Float64}:
8. -0.320597 0.865734
9. -0.765834 -0.475694
10. -0.557419 0.155628
12. julia> Q*I
13. 3×3 Matrix{Float64}:
14. -0.320597 0.865734 -0.384346
15. -0.765834 -0.475694 -0.432683
16. -0.557419 0.155628 0.815514
18. julia> Q*ones(2)
19. 3-element Vector{Float64}:
20. 0.5451367118802273
21. -1.241527373086654
22. -0.40179067589600226
24. julia> Q*ones(3)
25. 3-element Vector{Float64}:
26. 0.16079054743832022
27. -1.674209978965636
28. 0.41372375588835797
30. julia> ones(1,2) * Q'
31. 1×3 Matrix{Float64}:
32. 0.545137 -1.24153 -0.401791
34. julia> ones(1,3) * Q'
35. 1×3 Matrix{Float64}:
36. 0.160791 -1.67421 0.413724
Due to this distinction from dense or structured matrices, the abstract AbstractQ type does not subtype AbstractMatrix, but instead has its own type hierarchy. Custom types that subtype AbstractQ can rely on generic fallbacks if the following interface is satisfied. For example, for
1. struct MyQ{T} <: LinearAlgebra.AbstractQ{T}
2. # required fields
3. end
provide overloads for
1. Base.size(Q::MyQ) # size of corresponding square matrix representation
2. Base.convert(::Type{AbstractQ{T}}, Q::MyQ) # eltype promotion [optional]
3. LinearAlgebra.lmul!(Q::MyQ, x::AbstractVecOrMat) # left-multiplication
4. LinearAlgebra.rmul!(A::AbstractMatrix, Q::MyQ) # right-multiplication
If eltype promotion is not of interest, the convert method is unnecessary, since by default convert(::Type{AbstractQ{T}}, Q::AbstractQ{T}) returns Q itself. Adjoints of AbstractQ-typed objects are lazily wrapped in an AdjointQ wrapper type, which requires its own LinearAlgebra.lmul! and LinearAlgebra.rmul! methods. Given this set of methods, any Q::MyQ can be used like a matrix, preferably in a multiplicative context: multiplication via * with scalars, vectors and matrices from left and right, obtaining a matrix representation of Q via Matrix(Q) (or Q*I) and indexing into the matrix representation all work. In contrast, addition and subtraction as well as more generally broadcasting over elements in the matrix representation fail because that would be highly inefficient. For such use cases, consider computing the matrix representation up front and cache it for future reuse.
Standard functions
Linear algebra functions in Julia are largely implemented by calling functions from LAPACK. Sparse matrix factorizations call functions from SuiteSparse. Other sparse solvers are available as Julia packages.
Base.:* — Method
1. *(A::AbstractMatrix, B::AbstractMatrix)
Matrix multiplication.
Examples
1. julia> [1 1; 0 1] * [1 0; 1 1]
2. 2×2 Matrix{Int64}:
3. 2 1
4. 1 1
[Base.:](#Base.:\-Tuple{AbstractMatrix, AbstractVecOrMat}) — Method
1. \(A, B)
Matrix division using a polyalgorithm. For input matrices A and B, the result X is such that A*X == B when A is square. The solver that is used depends upon the structure of A. If A is upper or lower triangular (or diagonal), no factorization of A is required and the system is solved with either forward or backward substitution. For non-triangular square matrices, an LU factorization is used.
For rectangular A the result is the minimum-norm least squares solution computed by a pivoted QR factorization of A and a rank estimate of A based on the R factor.
When A is sparse, a similar polyalgorithm is used. For indefinite matrices, the LDLt factorization does not use pivoting during the numerical factorization and therefore the procedure can fail even for invertible matrices.
Examples
1. julia> A = [1 0; 1 -2]; B = [32; -4];
3. julia> X = A \ B
4. 2-element Vector{Float64}:
5. 32.0
6. 18.0
8. julia> A * X == B
9. true
Base.:/ — Method
1. A / B
Matrix right-division: A / B is equivalent to (B' \ A')' where [](#Base.:\-Tuple{Any, Any}) is the left-division operator. For square matrices, the result X is such that A == X*B.
See also: rdiv!.
Examples
1. julia> A = Float64[1 4 5; 3 9 2]; B = Float64[1 4 2; 3 4 2; 8 7 1];
3. julia> X = A / B
4. 2×3 Matrix{Float64}:
5. -0.65 3.75 -1.2
6. 3.25 -2.75 1.0
8. julia> isapprox(A, X*B)
9. true
11. julia> isapprox(X, A*pinv(B))
12. true
LinearAlgebra.SingularException — Type
1. SingularException
Exception thrown when the input matrix has one or more zero-valued eigenvalues, and is not invertible. A linear solve involving such a matrix cannot be computed. The info field indicates the location of (one of) the singular value(s).
LinearAlgebra.PosDefException — Type
1. PosDefException
Exception thrown when the input matrix was not positive definite. Some linear algebra functions and factorizations are only applicable to positive definite matrices. The info field indicates the location of (one of) the eigenvalue(s) which is (are) less than/equal to 0.
LinearAlgebra.ZeroPivotException — Type
1. ZeroPivotException <: Exception
Exception thrown when a matrix factorization/solve encounters a zero in a pivot (diagonal) position and cannot proceed. This may not mean that the matrix is singular: it may be fruitful to switch to a different factorization such as pivoted LU that can re-order variables to eliminate spurious zero pivots. The info field indicates the location of (one of) the zero pivot(s).
LinearAlgebra.dot — Function
1. dot(x, y)
2. x ⋅ y
Compute the dot product between two vectors. For complex vectors, the first vector is conjugated.
dot also works on arbitrary iterable objects, including arrays of any dimension, as long as dot is defined on the elements.
dot is semantically equivalent to sum(dot(vx,vy) for (vx,vy) in zip(x, y)), with the added restriction that the arguments must have equal lengths.
x ⋅ y (where ⋅ can be typed by tab-completing \cdot in the REPL) is a synonym for dot(x, y).
Examples
1. julia> dot([1; 1], [2; 3])
2. 5
4. julia> dot([im; im], [1; 1])
5. 0 - 2im
7. julia> dot(1:5, 2:6)
8. 70
10. julia> x = fill(2., (5,5));
12. julia> y = fill(3., (5,5));
14. julia> dot(x, y)
15. 150.0
LinearAlgebra.dot — Method
1. dot(x, A, y)
Compute the generalized dot product dot(x, A*y) between two vectors x and y, without storing the intermediate result of A*y. As for the two-argument dot(_,_), this acts recursively. Moreover, for complex vectors, the first vector is conjugated.
Julia 1.4
Three-argument dot requires at least Julia 1.4.
Examples
1. julia> dot([1; 1], [1 2; 3 4], [2; 3])
2. 26
4. julia> dot(1:5, reshape(1:25, 5, 5), 2:6)
5. 4850
7. julia> ⋅(1:5, reshape(1:25, 5, 5), 2:6) == dot(1:5, reshape(1:25, 5, 5), 2:6)
8. true
LinearAlgebra.cross — Function
1. cross(x, y)
2. ×(x,y)
Compute the cross product of two 3-vectors.
Examples
1. julia> a = [0;1;0]
2. 3-element Vector{Int64}:
3. 0
4. 1
5. 0
7. julia> b = [0;0;1]
8. 3-element Vector{Int64}:
9. 0
10. 0
11. 1
13. julia> cross(a,b)
14. 3-element Vector{Int64}:
15. 1
16. 0
17. 0
LinearAlgebra.axpy! — Function
1. axpy!(α, x::AbstractArray, y::AbstractArray)
Overwrite y with x * α + y and return y. If x and y have the same axes, it’s equivalent with y .+= x .* a.
Examples
1. julia> x = [1; 2; 3];
3. julia> y = [4; 5; 6];
5. julia> axpy!(2, x, y)
6. 3-element Vector{Int64}:
7. 6
8. 9
9. 12
LinearAlgebra.axpby! — Function
1. axpby!(α, x::AbstractArray, β, y::AbstractArray)
Overwrite y with x * α + y * β and return y. If x and y have the same axes, it’s equivalent with y .= x .* a .+ y .* β.
Examples
1. julia> x = [1; 2; 3];
3. julia> y = [4; 5; 6];
5. julia> axpby!(2, x, 2, y)
6. 3-element Vector{Int64}:
7. 10
8. 14
9. 18
LinearAlgebra.rotate! — Function
1. rotate!(x, y, c, s)
Overwrite x with c*x + s*y and y with -conj(s)*x + c*y. Returns x and y.
Julia 1.5
rotate! requires at least Julia 1.5.
LinearAlgebra.reflect! — Function
1. reflect!(x, y, c, s)
Overwrite x with c*x + s*y and y with conj(s)*x - c*y. Returns x and y.
Julia 1.5
reflect! requires at least Julia 1.5.
LinearAlgebra.factorize — Function
1. factorize(A)
Compute a convenient factorization of A, based upon the type of the input matrix. factorize checks A to see if it is symmetric/triangular/etc. if A is passed as a generic matrix. factorize checks every element of A to verify/rule out each property. It will short-circuit as soon as it can rule out symmetry/triangular structure. The return value can be reused for efficient solving of multiple systems. For example: A=factorize(A); x=A\b; y=A\C.
Properties of A |
type of factorization |
|---|---|
| Positive-definite | Cholesky (see cholesky) |
| Dense Symmetric/Hermitian | Bunch-Kaufman (see bunchkaufman) |
| Sparse Symmetric/Hermitian | LDLt (see ldlt) |
| Triangular | Triangular |
| Diagonal | Diagonal |
| Bidiagonal | Bidiagonal |
| Tridiagonal | LU (see lu) |
| Symmetric real tridiagonal | LDLt (see ldlt) |
| General square | LU (see lu) |
| General non-square | QR (see qr) |
If factorize is called on a Hermitian positive-definite matrix, for instance, then factorize will return a Cholesky factorization.
Examples
1. julia> A = Array(Bidiagonal(fill(1.0, (5, 5)), :U))
2. 5×5 Matrix{Float64}:
3. 1.0 1.0 0.0 0.0 0.0
4. 0.0 1.0 1.0 0.0 0.0
5. 0.0 0.0 1.0 1.0 0.0
6. 0.0 0.0 0.0 1.0 1.0
7. 0.0 0.0 0.0 0.0 1.0
9. julia> factorize(A) # factorize will check to see that A is already factorized
10. 5×5 Bidiagonal{Float64, Vector{Float64}}:
11. 1.0 1.0 ⋅ ⋅ ⋅
12. ⋅ 1.0 1.0 ⋅ ⋅
13. ⋅ ⋅ 1.0 1.0 ⋅
14. ⋅ ⋅ ⋅ 1.0 1.0
15. ⋅ ⋅ ⋅ ⋅ 1.0
This returns a 5×5 Bidiagonal{Float64}, which can now be passed to other linear algebra functions (e.g. eigensolvers) which will use specialized methods for Bidiagonal types.
LinearAlgebra.Diagonal — Type
1. Diagonal(V::AbstractVector)
Construct a lazy matrix with V as its diagonal.
See also UniformScaling for the lazy identity matrix I, diagm to make a dense matrix, and diag to extract diagonal elements.
Examples
1. julia> d = Diagonal([1, 10, 100])
2. 3×3 Diagonal{Int64, Vector{Int64}}:
3. 1 ⋅ ⋅
4. ⋅ 10 ⋅
5. ⋅ ⋅ 100
7. julia> diagm([7, 13])
8. 2×2 Matrix{Int64}:
9. 7 0
10. 0 13
12. julia> ans + I
13. 2×2 Matrix{Int64}:
14. 8 0
15. 0 14
17. julia> I(2)
18. 2×2 Diagonal{Bool, Vector{Bool}}:
19. 1 ⋅
20. ⋅ 1
Note that a one-column matrix is not treated like a vector, but instead calls the method Diagonal(A::AbstractMatrix) which extracts 1-element diag(A):
1. julia> A = transpose([7.0 13.0])
2. 2×1 transpose(::Matrix{Float64}) with eltype Float64:
3. 7.0
4. 13.0
6. julia> Diagonal(A)
7. 1×1 Diagonal{Float64, Vector{Float64}}:
8. 7.0
1. Diagonal(A::AbstractMatrix)
Construct a matrix from the diagonal of A.
Examples
1. julia> A = permutedims(reshape(1:15, 5, 3))
2. 3×5 Matrix{Int64}:
3. 1 2 3 4 5
4. 6 7 8 9 10
5. 11 12 13 14 15
7. julia> Diagonal(A)
8. 3×3 Diagonal{Int64, Vector{Int64}}:
9. 1 ⋅ ⋅
10. ⋅ 7 ⋅
11. ⋅ ⋅ 13
13. julia> diag(A, 2)
14. 3-element Vector{Int64}:
15. 3
16. 9
17. 15
1. Diagonal{T}(undef, n)
Construct an uninitialized Diagonal{T} of length n. See undef.
LinearAlgebra.Bidiagonal — Type
1. Bidiagonal(dv::V, ev::V, uplo::Symbol) where V <: AbstractVector
Constructs an upper (uplo=:U) or lower (uplo=:L) bidiagonal matrix using the given diagonal (dv) and off-diagonal (ev) vectors. The result is of type Bidiagonal and provides efficient specialized linear solvers, but may be converted into a regular matrix with convert(Array, _) (or Array(_) for short). The length of ev must be one less than the length of dv.
Examples
1. julia> dv = [1, 2, 3, 4]
2. 4-element Vector{Int64}:
3. 1
4. 2
5. 3
6. 4
8. julia> ev = [7, 8, 9]
9. 3-element Vector{Int64}:
10. 7
11. 8
12. 9
14. julia> Bu = Bidiagonal(dv, ev, :U) # ev is on the first superdiagonal
15. 4×4 Bidiagonal{Int64, Vector{Int64}}:
16. 1 7 ⋅ ⋅
17. ⋅ 2 8 ⋅
18. ⋅ ⋅ 3 9
19. ⋅ ⋅ ⋅ 4
21. julia> Bl = Bidiagonal(dv, ev, :L) # ev is on the first subdiagonal
22. 4×4 Bidiagonal{Int64, Vector{Int64}}:
23. 1 ⋅ ⋅ ⋅
24. 7 2 ⋅ ⋅
25. ⋅ 8 3 ⋅
26. ⋅ ⋅ 9 4
1. Bidiagonal(A, uplo::Symbol)
Construct a Bidiagonal matrix from the main diagonal of A and its first super- (if uplo=:U) or sub-diagonal (if uplo=:L).
Examples
1. julia> A = [1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4]
2. 4×4 Matrix{Int64}:
3. 1 1 1 1
4. 2 2 2 2
5. 3 3 3 3
6. 4 4 4 4
8. julia> Bidiagonal(A, :U) # contains the main diagonal and first superdiagonal of A
9. 4×4 Bidiagonal{Int64, Vector{Int64}}:
10. 1 1 ⋅ ⋅
11. ⋅ 2 2 ⋅
12. ⋅ ⋅ 3 3
13. ⋅ ⋅ ⋅ 4
15. julia> Bidiagonal(A, :L) # contains the main diagonal and first subdiagonal of A
16. 4×4 Bidiagonal{Int64, Vector{Int64}}:
17. 1 ⋅ ⋅ ⋅
18. 2 2 ⋅ ⋅
19. ⋅ 3 3 ⋅
20. ⋅ ⋅ 4 4
LinearAlgebra.SymTridiagonal — Type
1. SymTridiagonal(dv::V, ev::V) where V <: AbstractVector
Construct a symmetric tridiagonal matrix from the diagonal (dv) and first sub/super-diagonal (ev), respectively. The result is of type SymTridiagonal and provides efficient specialized eigensolvers, but may be converted into a regular matrix with convert(Array, _) (or Array(_) for short).
For SymTridiagonal block matrices, the elements of dv are symmetrized. The argument ev is interpreted as the superdiagonal. Blocks from the subdiagonal are (materialized) transpose of the corresponding superdiagonal blocks.
Examples
1. julia> dv = [1, 2, 3, 4]
2. 4-element Vector{Int64}:
3. 1
4. 2
5. 3
6. 4
8. julia> ev = [7, 8, 9]
9. 3-element Vector{Int64}:
10. 7
11. 8
12. 9
14. julia> SymTridiagonal(dv, ev)
15. 4×4 SymTridiagonal{Int64, Vector{Int64}}:
16. 1 7 ⋅ ⋅
17. 7 2 8 ⋅
18. ⋅ 8 3 9
19. ⋅ ⋅ 9 4
21. julia> A = SymTridiagonal(fill([1 2; 3 4], 3), fill([1 2; 3 4], 2));
23. julia> A[1,1]
24. 2×2 Symmetric{Int64, Matrix{Int64}}:
25. 1 2
26. 2 4
28. julia> A[1,2]
29. 2×2 Matrix{Int64}:
30. 1 2
31. 3 4
33. julia> A[2,1]
34. 2×2 Matrix{Int64}:
35. 1 3
36. 2 4
1. SymTridiagonal(A::AbstractMatrix)
Construct a symmetric tridiagonal matrix from the diagonal and first superdiagonal of the symmetric matrix A.
Examples
1. julia> A = [1 2 3; 2 4 5; 3 5 6]
2. 3×3 Matrix{Int64}:
3. 1 2 3
4. 2 4 5
5. 3 5 6
7. julia> SymTridiagonal(A)
8. 3×3 SymTridiagonal{Int64, Vector{Int64}}:
9. 1 2 ⋅
10. 2 4 5
11. ⋅ 5 6
13. julia> B = reshape([[1 2; 2 3], [1 2; 3 4], [1 3; 2 4], [1 2; 2 3]], 2, 2);
15. julia> SymTridiagonal(B)
16. 2×2 SymTridiagonal{Matrix{Int64}, Vector{Matrix{Int64}}}:
17. [1 2; 2 3] [1 3; 2 4]
18. [1 2; 3 4] [1 2; 2 3]
LinearAlgebra.Tridiagonal — Type
1. Tridiagonal(dl::V, d::V, du::V) where V <: AbstractVector
Construct a tridiagonal matrix from the first subdiagonal, diagonal, and first superdiagonal, respectively. The result is of type Tridiagonal and provides efficient specialized linear solvers, but may be converted into a regular matrix with convert(Array, _) (or Array(_) for short). The lengths of dl and du must be one less than the length of d.
Examples
1. julia> dl = [1, 2, 3];
3. julia> du = [4, 5, 6];
5. julia> d = [7, 8, 9, 0];
7. julia> Tridiagonal(dl, d, du)
8. 4×4 Tridiagonal{Int64, Vector{Int64}}:
9. 7 4 ⋅ ⋅
10. 1 8 5 ⋅
11. ⋅ 2 9 6
12. ⋅ ⋅ 3 0
1. Tridiagonal(A)
Construct a tridiagonal matrix from the first sub-diagonal, diagonal and first super-diagonal of the matrix A.
Examples
1. julia> A = [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4]
2. 4×4 Matrix{Int64}:
3. 1 2 3 4
4. 1 2 3 4
5. 1 2 3 4
6. 1 2 3 4
8. julia> Tridiagonal(A)
9. 4×4 Tridiagonal{Int64, Vector{Int64}}:
10. 1 2 ⋅ ⋅
11. 1 2 3 ⋅
12. ⋅ 2 3 4
13. ⋅ ⋅ 3 4
LinearAlgebra.Symmetric — Type
1. Symmetric(A, uplo=:U)
Construct a Symmetric view of the upper (if uplo = :U) or lower (if uplo = :L) triangle of the matrix A.
Symmetric views are mainly useful for real-symmetric matrices, for which specialized algorithms (e.g. for eigenproblems) are enabled for Symmetric types. More generally, see also Hermitian(A) for Hermitian matrices A == A', which is effectively equivalent to Symmetric for real matrices but is also useful for complex matrices. (Whereas complex Symmetric matrices are supported but have few if any specialized algorithms.)
To compute the symmetric part of a real matrix, or more generally the Hermitian part (A + A') / 2 of a real or complex matrix A, use hermitianpart.
Examples
1. julia> A = [1 2 3; 4 5 6; 7 8 9]
2. 3×3 Matrix{Int64}:
3. 1 2 3
4. 4 5 6
5. 7 8 9
7. julia> Supper = Symmetric(A)
8. 3×3 Symmetric{Int64, Matrix{Int64}}:
9. 1 2 3
10. 2 5 6
11. 3 6 9
13. julia> Slower = Symmetric(A, :L)
14. 3×3 Symmetric{Int64, Matrix{Int64}}:
15. 1 4 7
16. 4 5 8
17. 7 8 9
19. julia> hermitianpart(A)
20. 3×3 Hermitian{Float64, Matrix{Float64}}:
21. 1.0 3.0 5.0
22. 3.0 5.0 7.0
23. 5.0 7.0 9.0
Note that Supper will not be equal to Slower unless A is itself symmetric (e.g. if A == transpose(A)).
LinearAlgebra.Hermitian — Type
1. Hermitian(A, uplo=:U)
Construct a Hermitian view of the upper (if uplo = :U) or lower (if uplo = :L) triangle of the matrix A.
To compute the Hermitian part of A, use hermitianpart.
Examples
1. julia> A = [1 2+2im 3-3im; 4 5 6-6im; 7 8+8im 9]
2. 3×3 Matrix{Complex{Int64}}:
3. 1+0im 2+2im 3-3im
4. 4+0im 5+0im 6-6im
5. 7+0im 8+8im 9+0im
7. julia> Hupper = Hermitian(A)
8. 3×3 Hermitian{Complex{Int64}, Matrix{Complex{Int64}}}:
9. 1+0im 2+2im 3-3im
10. 2-2im 5+0im 6-6im
11. 3+3im 6+6im 9+0im
13. julia> Hlower = Hermitian(A, :L)
14. 3×3 Hermitian{Complex{Int64}, Matrix{Complex{Int64}}}:
15. 1+0im 4+0im 7+0im
16. 4+0im 5+0im 8-8im
17. 7+0im 8+8im 9+0im
19. julia> hermitianpart(A)
20. 3×3 Hermitian{ComplexF64, Matrix{ComplexF64}}:
21. 1.0+0.0im 3.0+1.0im 5.0-1.5im
22. 3.0-1.0im 5.0+0.0im 7.0-7.0im
23. 5.0+1.5im 7.0+7.0im 9.0+0.0im
Note that Hupper will not be equal to Hlower unless A is itself Hermitian (e.g. if A == adjoint(A)).
All non-real parts of the diagonal will be ignored.
1. Hermitian(fill(complex(1,1), 1, 1)) == fill(1, 1, 1)
LinearAlgebra.LowerTriangular — Type
1. LowerTriangular(A::AbstractMatrix)
Construct a LowerTriangular view of the matrix A.
Examples
1. julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
2. 3×3 Matrix{Float64}:
3. 1.0 2.0 3.0
4. 4.0 5.0 6.0
5. 7.0 8.0 9.0
7. julia> LowerTriangular(A)
8. 3×3 LowerTriangular{Float64, Matrix{Float64}}:
9. 1.0 ⋅ ⋅
10. 4.0 5.0 ⋅
11. 7.0 8.0 9.0
LinearAlgebra.UpperTriangular — Type
1. UpperTriangular(A::AbstractMatrix)
Construct an UpperTriangular view of the matrix A.
Examples
1. julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
2. 3×3 Matrix{Float64}:
3. 1.0 2.0 3.0
4. 4.0 5.0 6.0
5. 7.0 8.0 9.0
7. julia> UpperTriangular(A)
8. 3×3 UpperTriangular{Float64, Matrix{Float64}}:
9. 1.0 2.0 3.0
10. ⋅ 5.0 6.0
11. ⋅ ⋅ 9.0
LinearAlgebra.UnitLowerTriangular — Type
1. UnitLowerTriangular(A::AbstractMatrix)
Construct a UnitLowerTriangular view of the matrix A. Such a view has the oneunit of the eltype of A on its diagonal.
Examples
1. julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
2. 3×3 Matrix{Float64}:
3. 1.0 2.0 3.0
4. 4.0 5.0 6.0
5. 7.0 8.0 9.0
7. julia> UnitLowerTriangular(A)
8. 3×3 UnitLowerTriangular{Float64, Matrix{Float64}}:
9. 1.0 ⋅ ⋅
10. 4.0 1.0 ⋅
11. 7.0 8.0 1.0
LinearAlgebra.UnitUpperTriangular — Type
1. UnitUpperTriangular(A::AbstractMatrix)
Construct an UnitUpperTriangular view of the matrix A. Such a view has the oneunit of the eltype of A on its diagonal.
Examples
1. julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
2. 3×3 Matrix{Float64}:
3. 1.0 2.0 3.0
4. 4.0 5.0 6.0
5. 7.0 8.0 9.0
7. julia> UnitUpperTriangular(A)
8. 3×3 UnitUpperTriangular{Float64, Matrix{Float64}}:
9. 1.0 2.0 3.0
10. ⋅ 1.0 6.0
11. ⋅ ⋅ 1.0
LinearAlgebra.UpperHessenberg — Type
1. UpperHessenberg(A::AbstractMatrix)
Construct an UpperHessenberg view of the matrix A. Entries of A below the first subdiagonal are ignored.
Julia 1.3
This type was added in Julia 1.3.
Efficient algorithms are implemented for H \ b, det(H), and similar.
See also the hessenberg function to factor any matrix into a similar upper-Hessenberg matrix.
If F::Hessenberg is the factorization object, the unitary matrix can be accessed with F.Q and the Hessenberg matrix with F.H. When Q is extracted, the resulting type is the HessenbergQ object, and may be converted to a regular matrix with convert(Array, _) (or Array(_) for short).
Iterating the decomposition produces the factors F.Q and F.H.
Examples
1. julia> A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
2. 4×4 Matrix{Int64}:
3. 1 2 3 4
4. 5 6 7 8
5. 9 10 11 12
6. 13 14 15 16
8. julia> UpperHessenberg(A)
9. 4×4 UpperHessenberg{Int64, Matrix{Int64}}:
10. 1 2 3 4
11. 5 6 7 8
12. ⋅ 10 11 12
13. ⋅ ⋅ 15 16
LinearAlgebra.UniformScaling — Type
1. UniformScaling{T<:Number}
Generically sized uniform scaling operator defined as a scalar times the identity operator, λ*I. Although without an explicit size, it acts similarly to a matrix in many cases and includes support for some indexing. See also I.
Julia 1.6
Indexing using ranges is available as of Julia 1.6.
Examples
1. julia> J = UniformScaling(2.)
2. UniformScaling{Float64}
3. 2.0*I
5. julia> A = [1. 2.; 3. 4.]
6. 2×2 Matrix{Float64}:
7. 1.0 2.0
8. 3.0 4.0
10. julia> J*A
11. 2×2 Matrix{Float64}:
12. 2.0 4.0
13. 6.0 8.0
15. julia> J[1:2, 1:2]
16. 2×2 Matrix{Float64}:
17. 2.0 0.0
18. 0.0 2.0
LinearAlgebra.I — Constant
1. I
An object of type UniformScaling, representing an identity matrix of any size.
Examples
1. julia> fill(1, (5,6)) * I == fill(1, (5,6))
2. true
4. julia> [1 2im 3; 1im 2 3] * I
5. 2×3 Matrix{Complex{Int64}}:
6. 1+0im 0+2im 3+0im
7. 0+1im 2+0im 3+0im
LinearAlgebra.UniformScaling — Method
1. (I::UniformScaling)(n::Integer)
Construct a Diagonal matrix from a UniformScaling.
Julia 1.2
This method is available as of Julia 1.2.
Examples
1. julia> I(3)
2. 3×3 Diagonal{Bool, Vector{Bool}}:
3. 1 ⋅ ⋅
4. ⋅ 1 ⋅
5. ⋅ ⋅ 1
7. julia> (0.7*I)(3)
8. 3×3 Diagonal{Float64, Vector{Float64}}:
9. 0.7 ⋅ ⋅
10. ⋅ 0.7 ⋅
11. ⋅ ⋅ 0.7
LinearAlgebra.Factorization — Type
1. LinearAlgebra.Factorization
Abstract type for matrix factorizations a.k.a. matrix decompositions. See online documentation for a list of available matrix factorizations.
LinearAlgebra.LU — Type
1. LU <: Factorization
Matrix factorization type of the LU factorization of a square matrix A. This is the return type of lu, the corresponding matrix factorization function.
The individual components of the factorization F::LU can be accessed via getproperty:
| Component | Description |
|---|---|
F.L |
L (unit lower triangular) part of LU |
F.U |
U (upper triangular) part of LU |
F.p |
(right) permutation Vector |
F.P |
(right) permutation Matrix |
Iterating the factorization produces the components F.L, F.U, and F.p.
Examples
1. julia> A = [4 3; 6 3]
2. 2×2 Matrix{Int64}:
3. 4 3
4. 6 3
6. julia> F = lu(A)
7. LU{Float64, Matrix{Float64}, Vector{Int64}}
8. L factor:
9. 2×2 Matrix{Float64}:
10. 1.0 0.0
11. 0.666667 1.0
12. U factor:
13. 2×2 Matrix{Float64}:
14. 6.0 3.0
15. 0.0 1.0
17. julia> F.L * F.U == A[F.p, :]
18. true
20. julia> l, u, p = lu(A); # destructuring via iteration
22. julia> l == F.L && u == F.U && p == F.p
23. true
LinearAlgebra.lu — Function
1. lu(A::AbstractSparseMatrixCSC; check = true, q = nothing, control = get_umfpack_control()) -> F::UmfpackLU
Compute the LU factorization of a sparse matrix A.
For sparse A with real or complex element type, the return type of F is UmfpackLU{Tv, Ti}, with Tv = Float64 or ComplexF64 respectively and Ti is an integer type (Int32 or Int64).
When check = true, an error is thrown if the decomposition fails. When check = false, responsibility for checking the decomposition’s validity (via issuccess) lies with the user.
The permutation q can either be a permutation vector or nothing. If no permutation vector is provided or q is nothing, UMFPACK’s default is used. If the permutation is not zero-based, a zero-based copy is made.
The control vector defaults to the package’s default configuration for UMFPACK, but can be changed by passing a vector of length UMFPACK_CONTROL. See the UMFPACK manual for possible configurations. The corresponding variables are named JL_UMFPACK_ since Julia uses one-based indexing.
The individual components of the factorization F can be accessed by indexing:
| Component | Description |
|---|---|
L |
L (lower triangular) part of LU |
U |
U (upper triangular) part of LU |
p |
right permutation Vector |
q |
left permutation Vector |
Rs |
Vector of scaling factors |
: |
(L,U,p,q,Rs) components |
The relation between F and A is
F.L*F.U == (F.Rs .* A)[F.p, F.q]
F further supports the following functions:
- [](/read/julia-1.10-en/5750f1ec061207b6.md#Base.:\-Tuple{Any, Any})
- det
See also lu!
Note
lu(A::AbstractSparseMatrixCSC) uses the UMFPACK[ACM832] library that is part of SuiteSparse. As this library only supports sparse matrices with [Float64](/
(本文超出站点单文档体积上限,已截断)
