线性代数

除了(且作为一部分)对多维数组的支持,Julia 还提供了许多常见和实用的线性代数运算的本地实现。基本的运算,比如 tr,det 和 inv 都是支持的:

1. julia> A = [1 2 3; 4 1 6; 7 8 1]
2. 3×3 Array{Int64,2}:
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 Array{Float64,2}:
15. -0.451923   0.211538    0.0865385
16. 0.365385  -0.192308    0.0576923
17. 0.240385   0.0576923  -0.0673077

还有其它实用的运算,比如寻找特征值或特征向量:

1. julia> A = [-4. -17.; 2. 2.]
2. 2×2 Array{Float64,2}:
3. -4.0  -17.0
4. 2.0    2.0

6. julia> eigvals(A)
7. 2-element Array{Complex{Float64},1}:
8. -1.0 + 5.0im
9. -1.0 - 5.0im

11. julia> eigvecs(A)
12. 2×2 Array{Complex{Float64},2}:
13. 0.945905+0.0im        0.945905-0.0im
14. -0.166924-0.278207im  -0.166924+0.278207im

此外,Julia 提供了多种矩阵分解,它们可用于加快问题的求解,比如线性求解或矩阵或矩阵求幂,这通过将矩阵预先分解成更适合问题的形式(出于性能或内存上的原因)。有关的更多信息,请参阅文档 factorize。举个例子:

1. julia> A = [1.5 2 -4; 3 -1 -6; -10 2.3 4]
2. 3×3 Array{Float64,2}:
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,Array{Float64,2}}
9. L factor:
10. 3×3 Array{Float64,2}:
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 Array{Float64,2}:
16. -10.0  2.3     4.0
17. 0.0  2.345  -3.4
18. 0.0  0.0    -5.24947

因为 A 不是埃尔米特、对称、三角、三对角或双对角矩阵,LU 分解也许是我们能做的最好分解。与之相比:

1. julia> B = [1.5 2 -4; 2 -1 -3; -4 -3 5]
2. 3×3 Array{Float64,2}:
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,Array{Float64,2}}
9. D factor:
10. 3×3 Tridiagonal{Float64,Array{Float64,1}}:
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,Array{Float64,2}}:
16. 1.0  0.142857  -0.8
17. ⋅   1.0       -0.6
18. ⋅    ⋅         1.0
19. permutation:
20. 3-element Array{Int64,1}:
21. 1
22. 2
23. 3

在这里,Julia 能够发现 B 确实是对称矩阵,并且使用一种更适当的分解。针对一个具有某些属性的矩阵,比如一个对称或三对角矩阵,往往有可能写出更高效的代码。Julia 提供了一些特殊的类型好让你可以根据矩阵所具有的属性「标记」它们。例如:

1. julia> B = [1.5 2 -4; 2 -1 -3; -4 -3 5]
2. 3×3 Array{Float64,2}:
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,Array{Float64,2}}:
9. 1.5   2.0  -4.0
10. 2.0  -1.0  -3.0
11. -4.0  -3.0   5.0

sB 已经被标记成(实)对称矩阵,所以对于之后可能在它上面执行的操作,例如特征因子化或矩阵-向量乘积,只引用矩阵的一半可以提高效率。举个例子:

1. julia> B = [1.5 2 -4; 2 -1 -3; -4 -3 5]
2. 3×3 Array{Float64,2}:
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,Array{Float64,2}}:
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 Array{Int64,1}:
15. 1
16. 2
17. 3

19. julia> sB\x
20. 3-element Array{Float64,1}:
21. -1.7391304347826084
22. -1.1086956521739126
23. -1.4565217391304346

\ 运算在这里执行线性求解。左除运算符相当强大,很容易写出紧凑、可读的代码,它足够灵活,可以求解各种线性方程组。

特殊矩阵

具有特殊对称性和结构的矩阵经常在线性代数中出现并且与各种矩阵分解相关。Julia 具有丰富的特殊矩阵类型,可以快速计算专门为特定矩阵类型开发的专用例程。

下表总结了在 Julia 中已经实现的特殊矩阵类型,以及为它们提供各种优化方法的钩子在 LAPACK 中是否可用。

类型 描述
Symmetric 对称矩阵
Hermitian 埃尔米特矩阵
UpperTriangular 上三角矩阵
LowerTriangular 下三角矩阵
Tridiagonal 三对角矩阵
SymTridiagonal 对称三对角矩阵
Bidiagonal 上/下双对角矩阵
Diagonal 对角矩阵
UniformScaling 等比缩放运算符

基本运算

矩阵类型 + - * \ 其它具有优化方法的函数
Symmetric MV inv, sqrt, exp
Hermitian MV inv, sqrt, exp
UpperTriangular MV MV inv, det
LowerTriangular MV MV 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 /

图例:

键 描述
M (matrix) 针对矩阵与矩阵运算的优化方法可用
V (vector) 针对矩阵与向量运算的优化方法可用
S (scalar) 针对矩阵与标量运算的优化方法可用

矩阵分解

矩阵类型 LAPACK eigen eigvals eigvecs svd svdvals
Symmetric SY ARI
Hermitian HE ARI
UpperTriangular TR A A A
LowerTriangular TR A A A
SymTridiagonal ST A ARI AV
Tridiagonal GT
Bidiagonal BD A A
Diagonal DI A

图例:

键 描述 例子
A (all) 找到所有特征值和/或特征向量的优化方法可用 例如,eigvals(M)
R (range) 通过第 ih 个特征值寻找第 il 个特征值的优化方法可用 eigvals(M, il, ih)
I (interval) 寻找在区间 [vl, vh] 内的特征值的优化方法可用 eigvals(M, vl, vh)
V (vectors) 寻找对应于特征值 x=[x1, x2,…] 的特征向量的优化方法可用 eigvecs(M, x)

等比缩放运算符

UniformScaling 运算符代表一个标量乘以恒同运算符,λ*I。恒同运算符 I 定义为常量和 UniformScaling 的实例。这些运算符是通用的,并且会在二元运算符 +,-,* 和 \ 中与另一个矩阵相匹配。对于 A+I 和 A-I ,这意味着 A 必须是个方阵。与恒同运算符 I 相乘是一个空操作(除了检查比例因子是一),因此几乎没有开销。

来查看 UniformScaling 运算符的运行结果:

1. julia> U = UniformScaling(2);

3. julia> a = [1 2; 3 4]
4. 2×2 Array{Int64,2}:
5. 1  2
6. 3  4

8. julia> a + U
9. 2×2 Array{Int64,2}:
10. 3  2
11. 3  6

13. julia> a * U
14. 2×2 Array{Int64,2}:
15. 2  4
16. 6  8

18. julia> [a U]
19. 2×4 Array{Int64,2}:
20. 1  2  2  0
21. 3  4  0  2

23. julia> b = [1 2 3; 4 5 6]
24. 2×3 Array{Int64,2}:
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. [...]

矩阵分解

矩阵分解将矩阵分解成矩阵乘积,是线性代数的中心概念。

下表总结了在 Julia 中已经实现了的矩阵分解的类型。其相关方法的细节可以在线性代数文档中的标准函数这一节中找到。

类型 描述
Cholesky Cholesky 分解
CholeskyPivoted Pivoted Cholesky 分解
LU LU 分解
LUTridiagonal 针对 Tridiagonal 矩阵的 LU 分解
QR QR 分解
QRCompactWY QR 分解的紧凑 WY 形式
QRPivoted Pivoted QR 分解
Hessenberg Hessenberg 分解
Eigen 谱分解
SVD 奇异值分解
GeneralizedSVD 广义 SVD

标准函数

Julia 中的线性代数函数主要通过调用 LAPACK 中的函数来实现。稀疏分解则调用 SuiteSparse 中的函数。

Base.:* — Method

1. *(A::AbstractMatrix, B::AbstractMatrix)

Matrix multiplication.

Examples

1. julia> [1 1; 0 1] * [1 0; 1 1]
2. 2×2 Array{Int64,2}:
3. 2  1
4. 1  1

Base.:\ — 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 Array{Float64,1}:
5. 32.0
6. 18.0

8. julia> A * X == B
9. true

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.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 Array{Int64,1}:
3. 0
4. 1
5. 0

7. julia> b = [0;0;1]
8. 3-element Array{Int64,1}:
9. 0
10. 0
11. 1

13. julia> cross(a,b)
14. 3-element Array{Int64,1}:
15. 1
16. 0
17. 0

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 Array{Float64,2}:
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,Array{Float64,1}}:
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(A::AbstractMatrix)

Construct a matrix from the diagonal of A.

Examples

1. julia> A = [1 2 3; 4 5 6; 7 8 9]
2. 3×3 Array{Int64,2}:
3. 1  2  3
4. 4  5  6
5. 7  8  9

7. julia> Diagonal(A)
8. 3×3 Diagonal{Int64,Array{Int64,1}}:
9. 1  ⋅  ⋅
10. ⋅  5  ⋅
11. ⋅  ⋅  9
1. Diagonal(V::AbstractVector)

Construct a matrix with V as its diagonal.

Examples

1. julia> V = [1, 2]
2. 2-element Array{Int64,1}:
3. 1
4. 2

6. julia> Diagonal(V)
7. 2×2 Diagonal{Int64,Array{Int64,1}}:
8. 1  ⋅
9. ⋅  2

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 Array{Int64,1}:
3. 1
4. 2
5. 3
6. 4

8. julia> ev = [7, 8, 9]
9. 3-element Array{Int64,1}:
10. 7
11. 8
12. 9

14. julia> Bu = Bidiagonal(dv, ev, :U) # ev is on the first superdiagonal
15. 4×4 Bidiagonal{Int64,Array{Int64,1}}:
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,Array{Int64,1}}:
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 Array{Int64,2}:
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,Array{Int64,1}}:
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,Array{Int64,1}}:
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).

Examples

1. julia> dv = [1, 2, 3, 4]
2. 4-element Array{Int64,1}:
3. 1
4. 2
5. 3
6. 4

8. julia> ev = [7, 8, 9]
9. 3-element Array{Int64,1}:
10. 7
11. 8
12. 9

14. julia> SymTridiagonal(dv, ev)
15. 4×4 SymTridiagonal{Int64,Array{Int64,1}}:
16. 1  7  ⋅  ⋅
17. 7  2  8  ⋅
18. ⋅  8  3  9
19. ⋅  ⋅  9  4
1. SymTridiagonal(A::AbstractMatrix)

Construct a symmetric tridiagonal matrix from the diagonal and first sub/super-diagonal, of the symmetric matrix A.

Examples

1. julia> A = [1 2 3; 2 4 5; 3 5 6]
2. 3×3 Array{Int64,2}:
3. 1  2  3
4. 2  4  5
5. 3  5  6

7. julia> SymTridiagonal(A)
8. 3×3 SymTridiagonal{Int64,Array{Int64,1}}:
9. 1  2  ⋅
10. 2  4  5
11. ⋅  5  6

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,Array{Int64,1}}:
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 Array{Int64,2}:
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,Array{Int64,1}}:
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.

Examples

1. julia> A = [1 0 2 0 3; 0 4 0 5 0; 6 0 7 0 8; 0 9 0 1 0; 2 0 3 0 4]
2. 5×5 Array{Int64,2}:
3. 1  0  2  0  3
4. 0  4  0  5  0
5. 6  0  7  0  8
6. 0  9  0  1  0
7. 2  0  3  0  4

9. julia> Supper = Symmetric(A)
10. 5×5 Symmetric{Int64,Array{Int64,2}}:
11. 1  0  2  0  3
12. 0  4  0  5  0
13. 2  0  7  0  8
14. 0  5  0  1  0
15. 3  0  8  0  4

17. julia> Slower = Symmetric(A, :L)
18. 5×5 Symmetric{Int64,Array{Int64,2}}:
19. 1  0  6  0  2
20. 0  4  0  9  0
21. 6  0  7  0  3
22. 0  9  0  1  0
23. 2  0  3  0  4

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.

Examples

1. julia> A = [1 0 2+2im 0 3-3im; 0 4 0 5 0; 6-6im 0 7 0 8+8im; 0 9 0 1 0; 2+2im 0 3-3im 0 4];

3. julia> Hupper = Hermitian(A)
4. 5×5 Hermitian{Complex{Int64},Array{Complex{Int64},2}}:
5. 1+0im  0+0im  2+2im  0+0im  3-3im
6. 0+0im  4+0im  0+0im  5+0im  0+0im
7. 2-2im  0+0im  7+0im  0+0im  8+8im
8. 0+0im  5+0im  0+0im  1+0im  0+0im
9. 3+3im  0+0im  8-8im  0+0im  4+0im

11. julia> Hlower = Hermitian(A, :L)
12. 5×5 Hermitian{Complex{Int64},Array{Complex{Int64},2}}:
13. 1+0im  0+0im  6+6im  0+0im  2-2im
14. 0+0im  4+0im  0+0im  9+0im  0+0im
15. 6-6im  0+0im  7+0im  0+0im  3+3im
16. 0+0im  9+0im  0+0im  1+0im  0+0im
17. 2+2im  0+0im  3-3im  0+0im  4+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 Array{Float64,2}:
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,Array{Float64,2}}:
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 Array{Float64,2}:
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,Array{Float64,2}}:
9. 1.0  2.0  3.0
10. ⋅   5.0  6.0
11. ⋅    ⋅   9.0

LinearAlgebra.UniformScaling — Type

1. UniformScaling{T<:Number}

Generically sized uniform scaling operator defined as a scalar times the identity operator, λ*I. See also I.

Examples

1. julia> J = UniformScaling(2.)
2. UniformScaling{Float64}
3. 2.0*I

5. julia> A = [1. 2.; 3. 4.]
6. 2×2 Array{Float64,2}:
7. 1.0  2.0
8. 3.0  4.0

10. julia> J*A
11. 2×2 Array{Float64,2}:
12. 2.0  4.0
13. 6.0  8.0

LinearAlgebra.lu — Function

1. lu(A, pivot=Val(true); check = true) -> F::LU

Compute the LU factorization of A.

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.

In most cases, if A is a subtype S of AbstractMatrix{T} with an element type T supporting +, -, * and /, the return type is LU{T,S{T}}. If pivoting is chosen (default) the element type should also support abs and <.

The individual components of the factorization F can be accessed via getproperty:

Component Description
F.L L (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.

The relationship between F and A is

F.L*F.U == A[F.p, :]

F further supports the following functions:

Supported function LU LU{T,Tridiagonal{T}}
/ ✓
\ ✓ ✓
inv ✓ ✓
det ✓ ✓
logdet ✓ ✓
logabsdet ✓ ✓
size ✓ ✓

Examples

1. julia> A = [4 3; 6 3]
2. 2×2 Array{Int64,2}:
3. 4  3
4. 6  3

6. julia> F = lu(A)
7. LU{Float64,Array{Float64,2}}
8. L factor:
9. 2×2 Array{Float64,2}:
10. 1.0       0.0
11. 0.666667  1.0
12. U factor:
13. 2×2 Array{Float64,2}:
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, pivot=Val(true); check = true) -> LU

lu! is the same as lu, but saves space by overwriting the input A, instead of creating a copy. An InexactError exception is thrown if the factorization produces a number not representable by the element type of A, e.g. for integer types.

Examples

1. julia> A = [4. 3.; 6. 3.]
2. 2×2 Array{Float64,2}:
3. 4.0  3.0
4. 6.0  3.0

6. julia> F = lu!(A)
7. LU{Float64,Array{Float64,2}}
8. L factor:
9. 2×2 Array{Float64,2}:
10. 1.0       0.0
11. 0.666667  1.0
12. U factor:
13. 2×2 Array{Float64,2}:
14. 6.0  3.0
15. 0.0  1.0

17. julia> iA = [4 3; 6 3]
18. 2×2 Array{Int64,2}:
19. 4  3
20. 6  3

22. julia> lu!(iA)
23. ERROR: InexactError: Int64(0.6666666666666666)
24. Stacktrace:
25. [...]

LinearAlgebra.cholesky — Function

1. cholesky(A, Val(false); check = true) -> Cholesky

Compute the Cholesky factorization of a dense symmetric positive definite matrix A and return a Cholesky factorization. The matrix A can either be a Symmetric or Hermitian StridedMatrix or a perfectly symmetric or Hermitian StridedMatrix. The triangular Cholesky factor can be obtained from the factorization F with: F.L and F.U. The following functions are available for Cholesky objects: size, \, inv, det, logdet and isposdef.

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.

Examples

1. julia> A = [4. 12. -16.; 12. 37. -43.; -16. -43. 98.]
2. 3×3 Array{Float64,2}:
3. 4.0   12.0  -16.0
4. 12.0   37.0  -43.0
5. -16.0  -43.0   98.0

7. julia> C = cholesky(A)
8. Cholesky{Float64,Array{Float64,2}}
9. U factor:
10. 3×3 UpperTriangular{Float64,Array{Float64,2}}:
11. 2.0  6.0  -8.0
12. ⋅   1.0   5.0
13. ⋅    ⋅    3.0

15. julia> C.U
16. 3×3 UpperTriangular{Float64,Array{Float64,2}}:
17. 2.0  6.0  -8.0
18. ⋅   1.0   5.0
19. ⋅    ⋅    3.0

21. julia> C.L
22. 3×3 LowerTriangular{Float64,Array{Float64,2}}:
23. 2.0   ⋅    ⋅
24. 6.0  1.0   ⋅
25. -8.0  5.0  3.0

27. julia> C.L * C.U == A
28. true
1. cholesky(A, Val(true); tol = 0.0, check = true) -> CholeskyPivoted

Compute the pivoted Cholesky factorization of a dense symmetric positive semi-definite matrix A and return a CholeskyPivoted factorization. The matrix A can either be a Symmetric or Hermitian StridedMatrix or a perfectly symmetric or Hermitian StridedMatrix. The triangular Cholesky factor can be obtained from the factorization F with: F.L and F.U. The following functions are available for CholeskyPivoted objects: size, \, inv, det, and rank. The argument tol determines the tolerance for determining the rank. For negative values, the tolerance is the machine precision.

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.

LinearAlgebra.cholesky! — Function

1. cholesky!(A, Val(false); check = true) -> Cholesky

The same as cholesky, but saves space by overwriting the input A, instead of creating a copy. An InexactError exception is thrown if the factorization produces a number not representable by the element type of A, e.g. for integer types.

Examples

1. julia> A = [1 2; 2 50]
2. 2×2 Array{Int64,2}:
3. 1   2
4. 2  50

6. julia> cholesky!(A)
7. ERROR: InexactError: Int64(6.782329983125268)
8. Stacktrace:
9. [...]
1. cholesky!(A, Val(true); tol = 0.0, check = true) -> CholeskyPivoted

The same as cholesky, but saves space by overwriting the input A, instead of creating a copy. An InexactError exception is thrown if the factorization produces a number not representable by the element type of A, e.g. for integer types.

LinearAlgebra.lowrankupdate — Function

1. lowrankupdate(C::Cholesky, v::StridedVector) -> CC::Cholesky

Update a Cholesky factorization C with the vector v. If A = C.U'C.U then CC = cholesky(C.U'C.U + v*v') but the computation of CC only uses O(n^2) operations.

LinearAlgebra.lowrankdowndate — Function

1. lowrankdowndate(C::Cholesky, v::StridedVector) -> CC::Cholesky

Downdate a Cholesky factorization C with the vector v. If A = C.U'C.U then CC = cholesky(C.U'C.U - v*v') but the computation of CC only uses O(n^2) operations.

LinearAlgebra.lowrankupdate! — Function

1. lowrankupdate!(C::Cholesky, v::StridedVector) -> CC::Cholesky

Update a Cholesky factorization C with the vector v. If A = C.U'C.U then CC = cholesky(C.U'C.U + v*v') but the computation of CC only uses O(n^2) operations. The input factorization C is updated in place such that on exit C == CC. The vector v is destroyed during the computation.

LinearAlgebra.lowrankdowndate! — Function

1. lowrankdowndate!(C::Cholesky, v::StridedVector) -> CC::Cholesky

Downdate a Cholesky factorization C with the vector v. If A = C.U'C.U then CC = cholesky(C.U'C.U - v*v') but the computation of CC only uses O(n^2) operations. The input factorization C is updated in place such that on exit C == CC. The vector v is destroyed during the computation.

LinearAlgebra.ldlt — Function

1. ldlt(S::SymTridiagonal) -> LDLt

Compute an LDLt factorization of the real symmetric tridiagonal matrix S such that S = L*Diagonal(d)*L' where L is a unit lower triangular matrix and d is a vector. The main use of an LDLt factorization F = ldlt(S) is to solve the linear system of equations Sx = b with F\b.

Examples

1. julia> S = SymTridiagonal([3., 4., 5.], [1., 2.])
2. 3×3 SymTridiagonal{Float64,Array{Float64,1}}:
3. 3.0  1.0   ⋅
4. 1.0  4.0  2.0
5. ⋅   2.0  5.0

7. julia> ldltS = ldlt(S);

9. julia> b = [6., 7., 8.];

11. julia> ldltS \ b
12. 3-element Array{Float64,1}:
13. 1.7906976744186047
14. 0.627906976744186
15. 1.3488372093023255

17. julia> S \ b
18. 3-element Array{Float64,1}:
19. 1.7906976744186047
20. 0.627906976744186
21. 1.3488372093023255

LinearAlgebra.ldlt! — Function

1. ldlt!(S::SymTridiagonal) -> LDLt

Same as ldlt, but saves space by overwriting the input S, instead of creating a copy.

Examples

1. julia> S = SymTridiagonal([3., 4., 5.], [1., 2.])
2. 3×3 SymTridiagonal{Float64,Array{Float64,1}}:
3. 3.0  1.0   ⋅
4. 1.0  4.0  2.0
5. ⋅   2.0  5.0

7. julia> ldltS = ldlt!(S);

9. julia> ldltS === S
10. false

12. julia> S
13. 3×3 SymTridiagonal{Float64,Array{Float64,1}}:
14. 3.0       0.333333   ⋅
15. 0.333333  3.66667   0.545455
16. ⋅        0.545455  3.90909

LinearAlgebra.qr — Function

1. qr(A, pivot=Val(false)) -> F

Compute the QR factorization of the matrix A: an orthogonal (or unitary if A is complex-valued) matrix Q, and an upper triangular matrix R such that

[A = Q R]

The returned object F stores the factorization in a packed format:

  • if pivot == Val(true) then F is a QRPivoted object,
  • otherwise if the element type of A is a BLAS type (Float32, Float64, ComplexF32 or ComplexF64), then F is a QRCompactWY object,
  • otherwise F is a QR object.

The individual components of the decomposition F can be retrieved via property accessors:

  • F.Q: the orthogonal/unitary matrix Q
  • F.R: the upper triangular matrix R
  • F.p: the permutation vector of the pivot (QRPivoted only)
  • F.P: the permutation matrix of the pivot (QRPivoted only)

Iterating the decomposition produces the components Q, R, and if extant p.

The following functions are available for the QR objects: inv, size, and \. When A is rectangular, \ will return a least squares solution and if the solution is not unique, the one with smallest norm is returned. When A is not full rank, factorization with (column) pivoting is required to obtain a minimum norm solution.

Multiplication with respect to either full/square or non-full/square Q is allowed, i.e. both F.Q*F.R and F.Q*A are supported. A Q matrix can be converted into a regular matrix with Matrix. This operation returns the “thin” Q factor, i.e., if A is m×n with m>=n, then Matrix(F.Q) yields an m×n matrix with orthonormal columns. To retrieve the “full” Q factor, an m×m orthogonal matrix, use F.Q*Matrix(I,m,m). If m<=n, then Matrix(F.Q) yields an m×m orthogonal matrix.

Examples

1. julia> A = [3.0 -6.0; 4.0 -8.0; 0.0 1.0]
2. 3×2 Array{Float64,2}:
3. 3.0  -6.0
4. 4.0  -8.0
5. 0.0   1.0

7. julia> F = qr(A)
8. LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}
9. Q factor:
10. 3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:
11. -0.6   0.0   0.8
12. -0.8   0.0  -0.6
13. 0.0  -1.0   0.0
14. R factor:
15. 2×2 Array{Float64,2}:
16. -5.0  10.0
17. 0.0  -1.0

19. julia> F.Q * F.R == A
20. true

Note

qr returns multiple types because LAPACK uses several representations that minimize the memory storage requirements of products of Householder elementary reflectors, so that the Q and R matrices can be stored compactly rather as two separate dense matrices.

LinearAlgebra.qr! — Function

1. qr!(A, pivot=Val(false))

qr! is the same as qr when A is a subtype of StridedMatrix, but saves space by overwriting the input A, instead of creating a copy. An InexactError exception is thrown if the factorization produces a number not representable by the element type of A, e.g. for integer types.

Examples

1. julia> a = [1. 2.; 3. 4.]
2. 2×2 Array{Float64,2}:
3. 1.0  2.0
4. 3.0  4.0

6. julia> qr!(a)
7. LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}
8. Q factor:
9. 2×2 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:
10. -0.316228  -0.948683
11. -0.948683   0.316228
12. R factor:
13. 2×2 Array{Float64,2}:
14. -3.16228  -4.42719
15. 0.0      -0.632456

17. julia> a = [1 2; 3 4]
18. 2×2 Array{Int64,2}:
19. 1  2
20. 3  4

22. julia> qr!(a)
23. ERROR: InexactError: Int64(-3.1622776601683795)
24. Stacktrace:
25. [...]

LinearAlgebra.QR — Type

1. QR <: Factorization

A QR matrix factorization stored in a packed format, typically obtained from qr. If $A$ is an m×n matrix, then

[A = Q R]

where $Q$ is an orthogonal/unitary matrix and $R$ is upper triangular. The matrix $Q$ is stored as a sequence of Householder reflectors $v_i$ and coefficients $\tau_i$ where:

[Q = \prod_{i=1}^{\min(m,n)} (I - \tau_i v_i v_i^T).]

Iterating the decomposition produces the components Q and R.

The object has two fields:

  • factors is an m×n matrix.

  • The upper triangular part contains the elements of $R$, that is R = triu(F.factors) for a QR object F.

  • The subdiagonal part contains the reflectors $v_i$ stored in a packed format where $v_i$ is the $i$th column of the matrix V = I + tril(F.factors, -1).
  • τ is a vector of length min(m,n) containing the coefficients $au_i$.

LinearAlgebra.QRCompactWY — Type

1. QRCompactWY <: Factorization

A QR matrix factorization stored in a compact blocked format, typically obtained from qr. If $A$ is an m×n matrix, then

[A = Q R]

where $Q$ is an orthogonal/unitary matrix and $R$ is upper triangular. It is similar to the QR format except that the orthogonal/unitary matrix $Q$ is stored in Compact WY format [Schreiber1989], as a lower trapezoidal matrix $V$ and an upper triangular matrix $T$ where

[Q = \prod_{i=1}^{\min(m,n)} (I - \tau_i v_i v_i^T) = I - V T V^T]

such that $v_i$ is the $i$th column of $V$, and $au_i$ is the $i$th diagonal element of $T$.

Iterating the decomposition produces the components Q and R.

The object has two fields:

  • factors, as in the QR type, is an m×n matrix.

  • The upper triangular part contains the elements of $R$, that is R = triu(F.factors) for a QR object F.

  • The subdiagonal part contains the reflectors $v_i$ stored in a packed format such that V = I + tril(F.factors, -1).
  • T is a square matrix with min(m,n) columns, whose upper triangular part gives the matrix $T$ above (the subdiagonal elements are ignored).

Note

This format should not to be confused with the older WY representation [Bischof1987].

LinearAlgebra.QRPivoted — Type

1. QRPivoted <: Factorization

A QR matrix factorization with column pivoting in a packed format, typically obtained from qr. If $A$ is an m×n matrix, then

[A P = Q R]

where $P$ is a permutation matrix, $Q$ is an orthogonal/unitary matrix and $R$ is upper triangular. The matrix $Q$ is stored as a sequence of Householder reflectors:

[Q = \prod_{i=1}^{\min(m,n)} (I - \tau_i v_i v_i^T).]

Iterating the decomposition produces the components Q, R, and p.

The object has three fields:

  • factors is an m×n matrix.

  • The upper triangular part contains the elements of $R$, that is R = triu(F.factors) for a QR object F.

  • The subdiagonal part contains the reflectors $v_i$ stored in a packed format where $v_i$ is the $i$th column of the matrix V = I + tril(F.factors, -1).
  • τ is a vector of length min(m,n) containing the coefficients $au_i$.
  • jpvt is an integer vector of length n corresponding to the permutation $P$.

LinearAlgebra.lq! — Function

1. lq!(A) -> LQ

Compute the LQ factorization of A, using the input matrix as a workspace. See also lq.

LinearAlgebra.lq — Function

1. lq(A) -> S::LQ

Compute the LQ decomposition of A. The decomposition’s lower triangular component can be obtained from the LQ object S via S.L, and the orthogonal/unitary component via S.Q, such that A ≈ S.L*S.Q.

Iterating the decomposition produces the components S.L and S.Q.

The LQ decomposition is the QR decomposition of transpose(A).

Examples

1. julia> A = [5. 7.; -2. -4.]
2. 2×2 Array{Float64,2}:
3. 5.0   7.0
4. -2.0  -4.0

6. julia> S = lq(A)
7. LQ{Float64,Array{Float64,2}} with factors L and Q:
8. [-8.60233 0.0; 4.41741 -0.697486]
9. [-0.581238 -0.813733; -0.813733 0.581238]

11. julia> S.L * S.Q
12. 2×2 Array{Float64,2}:
13. 5.0   7.0
14. -2.0  -4.0

16. julia> l, q = S; # destructuring via iteration

18. julia> l == S.L &&  q == S.Q
19. true

LinearAlgebra.bunchkaufman — Function

1. bunchkaufman(A, rook::Bool=false; check = true) -> S::BunchKaufman

Compute the Bunch-Kaufman [Bunch1977] factorization of a symmetric or Hermitian matrix A as P'*U*D*U'*P or P'*L*D*L'*P, depending on which triangle is stored in A, and return a BunchKaufman object. Note that if A is complex symmetric then U' and L' denote the unconjugated transposes, i.e. transpose(U) and transpose(L).

Iterating the decomposition produces the components S.D, S.U or S.L as appropriate given S.uplo, and S.p.

If rook is true, rook pivoting is used. If rook is false, rook pivoting is not used.

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 following functions are available for BunchKaufman objects: size, \, inv, issymmetric, ishermitian, getindex.

Examples

1. julia> A = [1 2; 2 3]
2. 2×2 Array{Int64,2}:
3. 1  2
4. 2  3

6. julia> S = bunchkaufman(A) # A gets wrapped internally by Symmetric(A)
7. BunchKaufman{Float64,Array{Float64,2}}
8. D factor:
9. 2×2 Tridiagonal{Float64,Array{Float64,1}}:
10. -0.333333  0.0
11. 0.0       3.0
12. U factor:
13. 2×2 UnitUpperTriangular{Float64,Array{Float64,2}}:
14. 1.0  0.666667
15. ⋅   1.0
16. permutation:
17. 2-element Array{Int64,1}:
18. 1
19. 2

21. julia> d, u, p = S; # destructuring via iteration

23. julia> d == S.D && u == S.U && p == S.p
24. true

26. julia> S = bunchkaufman(Symmetric(A, :L))
27. BunchKaufman{Float64,Array{Float64,2}}
28. D factor:
29. 2×2 Tridiagonal{Float64,Array{Float64,1}}:
30. 3.0   0.0
31. 0.0  -0.333333
32. L factor:
33. 2×2 UnitLowerTriangular{Float64,Array{Float64,2}}:
34. 1.0        ⋅
35. 0.666667  1.0
36. permutation:
37. 2-element Array{Int64,1}:
38. 2
39. 1

LinearAlgebra.bunchkaufman! — Function

1. bunchkaufman!(A, rook::Bool=false; check = true) -> BunchKaufman

bunchkaufman! is the same as bunchkaufman, but saves space by overwriting the input A, instead of creating a copy.

LinearAlgebra.eigvals — Function

1. eigvals(A; permute::Bool=true, scale::Bool=true, sortby) -> values

Return the eigenvalues of A.

For general non-symmetric matrices it is possible to specify how the matrix is balanced before the eigenvalue calculation. The permute, scale, and sortby keywords are the same as for eigen!.

Examples

1. julia> diag_matrix = [1 0; 0 4]
2. 2×2 Array{Int64,2}:
3. 1  0
4. 0  4

6. julia> eigvals(diag_matrix)
7. 2-element Array{Float64,1}:
8. 1.0
9. 4.0

For a scalar input, eigvals will return a scalar.

Example

1. julia> eigvals(-2)
2. -2
1. eigvals(A, B) -> values

Computes the generalized eigenvalues of A and B.

Examples

1. julia> A = [1 0; 0 -1]
2. 2×2 Array{Int64,2}:
3. 1   0
4. 0  -1

6. julia> B = [0 1; 1 0]
7. 2×2 Array{Int64,2}:
8. 0  1
9. 1  0

11. julia> eigvals(A,B)
12. 2-element Array{Complex{Float64},1}:
13. 0.0 - 1.0im
14. 0.0 + 1.0im
1. eigvals(A::Union{SymTridiagonal, Hermitian, Symmetric}, irange::UnitRange) -> values

Returns the eigenvalues of A. It is possible to calculate only a subset of the eigenvalues by specifying a UnitRange irange covering indices of the sorted eigenvalues, e.g. the 2nd to 8th eigenvalues.

1. julia> A = SymTridiagonal([1.; 2.; 1.], [2.; 3.])
2. 3×3 SymTridiagonal{Float64,Array{Float64,1}}:
3. 1.0  2.0   ⋅
4. 2.0  2.0  3.0
5. ⋅   3.0  1.0

7. julia> eigvals(A, 2:2)
8. 1-element Array{Float64,1}:
9. 0.9999999999999996

11. julia> eigvals(A)
12. 3-element Array{Float64,1}:
13. -2.1400549446402604
14. 1.0000000000000002
15. 5.140054944640259
1. eigvals(A::Union{SymTridiagonal, Hermitian, Symmetric}, vl::Real, vu::Real) -> values

Returns the eigenvalues of A. It is poss

(本文超出站点单文档体积上限,已截断)