Help us understand the problem. What is going on with this article?

『機械学習のエッセンス(http://isbn.sbcr.jp/93965/)』のPythonサンプルをJuliaで書き換えてみる。(第04章05NumPy/SciPyによる線形代数)

More than 1 year has passed since last update.

はじめに

『機械学習のエッセンス(http://isbn.sbcr.jp/93965/)』のPythonサンプルをJuliaで書き換えてみる。(第04章04疎行列)の続きです。

逆行列を求める

逆行列はinv関数で求めることができます。

\begin{pmatrix}
   3 & 1 & 1 \\
   1 & 2 & 1 \\
   0 & -1 & 1
\end{pmatrix}
julia> a = [3 1 1; 1 2 1; 0 -1 1]
3×3 Array{Int64,2}:
 3   1  1
 1   2  1
 0  -1  1

julia> inv(a)
3×3 Array{Float64,2}:
  0.428571  -0.285714  -0.142857
 -0.142857   0.428571  -0.285714
 -0.142857   0.428571   0.714286

"\"演算子を使う。(1次方程式を解く)

\begin{pmatrix}
   3 & 1 & 1 \\
   1 & 2 & 1 \\
   0 & -1 & 1
\end{pmatrix}
\begin{pmatrix}
  x \\
  y \\
  z
\end{pmatrix}
=
\begin{pmatrix}
  1 \\
  2 \\
  3
\end{pmatrix}

Juliaでは\(バックスラッシュ)演算子で1次方程式が解けるようです。便利ですね。

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

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

julia> a \ b
3-element Array{Float64,1}:
 -0.5714285714285714 
 -0.14285714285714302
  2.857142857142857 

LU分解により連立方程式を解く

JuliaにはLinearAlgebraモジュールにluという関数があります。
https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html#LinearAlgebra.lu
日本語訳はこちら。
https://code.i-harness.com/ja/docs/julia~1.0/stdlib/linearalgebra/index#LinearAlgebra.lu

julia> using LinearAlgebra

julia> l, u, p = lu(a)
LU{Float64,Array{Float64,2}}
L factor:
3×3 Array{Float64,2}:
 1.0        0.0  0.0
 0.333333   1.0  0.0
 0.0       -0.6  1.0
U factor:
3×3 Array{Float64,2}:
 3.0  1.0      1.0     
 0.0  1.66667  0.666667
 0.0  0.0      1.4  

julia> u \ (l \ b)
3-element Array{Float64,1}:
 -0.5714285714285714
 -0.14285714285714302
  2.857142857142857

参考

JuliaにはLinearAlgebra.factorizeという関数があって、与えられた行列に対して最適な分解をしてくれるようです。
https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html#LinearAlgebra.factorize
日本語訳はこちら。
https://code.i-harness.com/ja/docs/julia~1.0/stdlib/linearalgebra/index#LinearAlgebra.factorize

上記のaに対してfactorize関数を使ってみるとLU分解してくれました。

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

julia> using LinearAlgebra

julia> factorize(a)
LU{Float64,Array{Float64,2}}
L factor:
3×3 Array{Float64,2}:
 1.0        0.0  0.0
 0.333333   1.0  0.0
 0.0       -0.6  1.0
U factor:
3×3 Array{Float64,2}:
 3.0  1.0      1.0     
 0.0  1.66667  0.666667
 0.0  0.0      1.4  
yshutaro
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした