12. Factorizations and other fun (한글)
Factorizations and other fun¶
Andreas Noack님의 작성에 기반했습니다.
출처¶
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)
Topics:
함께보기¶
- http://deepstat.tistory.com/45 (01. Getting started)(in English)
- http://deepstat.tistory.com/46 (01. Getting started(한글))
- http://deepstat.tistory.com/47 (02. Strings)(in English)
- http://deepstat.tistory.com/48 (02. Strings(한글))
- http://deepstat.tistory.com/49 (03. Data structures)(in English)
- http://deepstat.tistory.com/50 (03. Data structures(한글))
- http://deepstat.tistory.com/51 (04. Loops)(in English)
- http://deepstat.tistory.com/52 (04. Loops(한글))
- http://deepstat.tistory.com/53 (05. Conditionals)(in English)
- http://deepstat.tistory.com/54 (05. Conditionals(한글))
- http://deepstat.tistory.com/55 (06. Functions)(in English)
- http://deepstat.tistory.com/56 (06. Functions(한글))
- http://deepstat.tistory.com/57 (07. Packages)(in English)
- http://deepstat.tistory.com/58 (07. Packages(한글))
- http://deepstat.tistory.com/59 (08. Plotting)(in English)
- http://deepstat.tistory.com/60 (08. Plotting(한글))
- http://deepstat.tistory.com/61 (09. Julia is fast)(in English)
- http://deepstat.tistory.com/62 (09. Julia is fast(한글))
- http://deepstat.tistory.com/63 (10. Multiple dispatch)(in English)
- http://deepstat.tistory.com/64 (10. Multiple dispatch(한글))
- http://deepstat.tistory.com/65 (11. Basic linear algebra in Julia)(in English)
- http://deepstat.tistory.com/66 (11. Basic linear algebra in Julia(한글))
- http://deepstat.tistory.com/67 (12. Factorizations and other fun)(in English)
시작하기 전에, 선형계를 설정하고, 행렬 분해나 특별한 행렬 구조를 사용하기 위해서 LinearAlgebra
를 불러온다.
using LinearAlgebra
A = rand(3, 3)
x = fill(1, (3,))
b = A * x
행렬 분해 (Factorizations)
LU 분해 (LU factorizations)¶
Julia에서 LU 분해(LU factorizations)를 하려면,
PA = LU
여기서 P
는 순열행렬(permutation matrix), L
는 하삼각 단위 대각 행렬 (lower triangular unit diagonal matrix), U
는 상삼각행렬(upper triangular matrix)이다.
Julia는 LU 분해를 계산하고, 이를 저장할 때 type을 composite factorization type으로 정의한다.
Alu = lu(A)
typeof(Alu)
특수 속성에 접근해서 분해결과를 각각 추출해낼 수 있다.
Alu.P
Alu.L
Alu.U
Julia는 분해 object에 있는 method를 빠르게 처리할 수 있다.
예를 들어 원래의 행렬 또는 분해 object를 사용해서 선형계를 풀 수 있다.
A\b
Alu\b
비슷하게, A
의 행렬식(determinant)도 계산할 수 있다.
det(A) ≈ det(Alu)
QR 분해(QR factorizations)¶
Julia에서 QR 분해 또한 할 수 있다.
A=QR
여기서 Q
는 단위(unitary)/직각(orthogonal) 행렬이고, R
은 상삼각(upper triangular) 행렬이다.
Aqr = qr(A)
LU 분해와 마찬가지로, 행렬 Q
와 R
은 QR 분해 object로부터 추출해낼 수 있다.
Aqr.Q
Aqr.R
고유분해(Eigendecompositions)¶
고유분해(eigendecompositions), 특이값 분해(singular value decompositions), Hessenberg 분해(Hessenberg factorizations), Schur 분해(Schur decompositions) 등 4가지 분해의 결과는 모두 Factorization
type으로 저장된다.
고유분해(eigendecompositions)는 다음과 같이 계산된다.
Asym = A + A'
AsymEig = eigen(Asym)
고유값(eigen values)과 고유벡터(eigen vectors)는 Eigen 타입에 특별한 인덱싱(indexing)을 이용해서 추출할 수 있다.
AsymEig.values
AsymEig.vectors
또 언급하자면, 분해가 type에 저장되면, 분해의 특성을 이용하는 특수한 method를 작성할 수 있다. 예를 들면, $$A^{-1}=(V\Lambda V^{-1})^{-1}=V\Lambda^{-1}V^{-1}$$ 같은 것이다.
inv(AsymEig)*Asym
n = 1000
A = randn(n,n);
Julia는 종종 특별한 행렬 구조(special matrix structure)를 추론한다.
Asym = A + A'
issymmetric(Asym)
그러나 가끔 부동소수점 오류가 발생할 수도 있다.
Asym_noisy = copy(Asym)
Asym_noisy[1,2] += 5eps()
issymmetric(Asym_noisy)
다행히도 우리는 이런 구조를 명시적으로 선언할 수도 있는데, Diagonal
, Triangular
, Symmetric
, Hermitian
, Tridiagonal
, SymTridiagonal
같은 것들이 그 예다.
Asym_explicit = Symmetric(Asym_noisy);
Julia가 Asym
, Asym_noisy
, Asym_explicit
세 행렬의 eigenvalue를 계산하는데 얼마나 차이가 나는지 비교하자.
@time eigvals(Asym);
@time eigvals(Asym_noisy);
@time eigvals(Asym_explicit);
이 예에서 Asym_noisy
에 Symmetric()
을 씌운 것이 5x
나 더 효율적이다.
큰 문제¶
삼중행렬(tridiagonal matrix)를 저장할 때, Tridiagonal
나 SymTridiagonal
type을 이용하면 잠재적으로 엄청나게 큰 삼중행렬(tridiagonal matrix)도 계산할 수 있다. 다음의 예제는 Matrix
type으로 저장된 object라면 일반 PC로 계산할 수 없을 것이다.
n = 1_000_000;
A = SymTridiagonal(randn(n), randn(n-1));
@time eigmax(A)
유리수(Rational numbers)¶
Julia는 유리수가 기본내장돼있다. 유리수를 입력하기 위해서, 정방향 slash를 2개 쓴다.
1//2
예제 : 유리선형계 방정식(Rational linear system of equations)¶
다음의 예제는 유리선형계 방정식(rational linear system of equations)을 부동 소수점 type으로 바꾸지 않고 해결할 수 있는 방법을 보여준다. 유리수로 작업 할 때 오버플로(overflow)가 문제가 쉽게 일어날 수 있으므로 BigInt
를 사용한다.
Arational = Matrix{Rational{BigInt}}(rand(1:10, 3, 3))/10
x = fill(1, 3)
b = Arational*x
Arational\b
lu(Arational)
연습문제
11.1¶
다음 행렬 A의 고유값(eigen values)이 뭔지 계산해보자.
A =
[
140 97 74 168 131
97 106 89 131 36
74 89 152 144 71
168 131 144 54 142
131 36 71 142 36
]
그리고 계산한 값을 A_eigv
에 저장하자.
using LinearAlgebra
A =[140 97 74 168 131;
97 106 89 131 36;
74 89 152 144 71;
168 131 144 54 142;
131 36 71 142 36]
A_eigv = eigen(A).values
@assert A_eigv == [-128.49322764802145, -55.887784553056875, 42.7521672793189, 87.16111477514521, 542.4677301466143]
11.2¶
A
의 고유값(eigenvalues)를 이용해서 대각행렬(Diagonal
matrix)를 만들자.
A_diag = Diagonal(A_eigv)
@assert A_diag == [-128.493 0.0 0.0 0.0 0.0;
0.0 -55.8878 0.0 0.0 0.0;
0.0 0.0 42.7522 0.0 0.0;
0.0 0.0 0.0 87.1611 0.0;
0.0 0.0 0.0 0.0 542.468]
11.3¶
A
의 하삼각(LowerTriangular
) 행렬을 만들고, 이를 A_lowertri
에 저장하자.
A_lowertri = LowerTriangular(A)
@assert A_lowertri == [140 0 0 0 0;
97 106 0 0 0;
74 89 152 0 0;
168 131 144 54 0;
131 36 71 142 36]