티스토리 뷰
Basic linear algebra in Julia¶
원저자: Andreas Noack Jensen (MIT) (http://www.econ.ku.dk/phdstudent/noack/) (with edits from Jane Herriman)
출처¶
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)
먼저, 랜덤 행렬을 정의해보자.
A = rand(1:4,4,4)
1로 이루어진 벡터도 정의하자.
x = fill(1.0, (4,)) # = fill(1.0, 4)
A는 type이 Array{Int64,2}이고, x는 type이 Array{Float64,1} 라는 것을 볼 수 있다. Julia는 Vector{Type}=Array{Type,1}, Matrix{Type}=Array{Type,2} 로 별명(alias)을 정의하고있다.
행렬곱(Multiplication)¶
b = A*x
전치(Transpose)¶
다른 언어들처럼, A'
는 켤레전치(conjugate transpose)행렬이다.
A'
또는 아래처럼 전치(transpose)행렬을 구할 수 있다.
transpose(A)
전치 후 곱셈¶
Julia는 *를 쓰지 않아도 된다.
A'A
선형계(linear systems) 풀기¶
A 가 정방(square)행렬 일 때, Ax=b는 함수 \ 로 풀 수 있다.
A\b
A^-1 * b
만약 과결정선형계(overdetermined linear system) (A 가 아래로 긴 행렬)일 때, A\b
는 최소 제곱 해(least squares solution)를 도출한다.
Atall = rand(4, 2)
Atall\b
(Atall'Atall)^-1 * Atall'b
만일 계수 부족 최소제곱 문제(rank-deficient least squares problem)인 경우는, 최소 노름 최소 제곱 해(minimum norm least squares solution)를 도출한다.
v1 = rand(4)
v2 = rand(4)
rankdef = hcat(v1, v1, v2)
rankdef\b
또한, 결정미달계(underdetermined system)(A가 아래로 짧은 행렬) 인 경우, Julia는 최소 노름 해(minimum norm solution)를 도출한다.
bshort = rand(2)
Ashort = rand(2, 3)
Ashort\bshort
라이브러리 LinearAlgebra¶
선형 대수학의 대부분은 기본적으로 Julia에서 사용할 수 있지만 (위에서 본 것 처럼), LinearAlgebra
라는 표준 라이브러리가 있으며, 더 많은 관련된 이름과 함수를 제공한다. 특히, 인수 분해(factorization) 및 일부 구조화된 행렬(structured matrix) type을 제공한다. 다른 패키지와 마찬가지로, using LinearAlgebra
로 session에 이러한 추가 기능을 가져올 수 있다.
v = [1,2,3]
dot_v = v'v
using LinearAlgebra
dot_v = dot(v,v)
dot_v = v⋅v # \cdot<tab>
dot_v = ⋅(v,v) # \cdot<tab>
@assert dot_v == 14
10.2¶
v
와 자신의 외적(outer product)를 구하자. 그리고 그 값을 cross_v
에 저장하자.
using LinearAlgebra
cross_v = cross(v,v)
cross_v = v×v # \times<tab>
cross_v = ×(v,v) # \times<tab>
@assert cross_v == [0, 0, 0]
'Flux in Julia > Learning Julia (Intro_to_Julia)' 카테고리의 다른 글
12. Factorizations and other fun (한글) (0) | 2018.10.05 |
---|---|
12. Factorizations and other fun (0) | 2018.10.05 |
11. Basic linear algebra (0) | 2018.10.04 |
10. Multiple dispatch (한글) (0) | 2018.10.02 |
10. Multiple dispatch (0) | 2018.10.02 |