#==========
Julia の修行をするときに,いろいろなプログラムを書き換えるのは有効な方法だ。
以下のプログラムを Julia に翻訳してみる。
数量化 IV 類
http://aoki2.si.gunma-u.ac.jp/R/qt4.html
ファイル名: qt4.jl 関数名: qt4, printqt4, plotqt4
翻訳するときに書いたメモ
==========#
using LinearAlgebra, NamedArrays, Plots
function qt4(s; name=[])
n, n2 = size(s)
n != n2 && error("s must be a square matrix. $nx$n2")
length(name) == 0 && (name = ["Obj-$i" for i = 1:n])
h = s + transpose(s)
[h[i, i] = 0 for i = 1:n]
rowsum = -sum(h, dims=2)
[h[i, i] = rowsum[i] for i = 1:n]
values, vectors = eigen(h, sortby=x-> -x)
ax = sum(values .> 1e-5)
values = values[1:ax]
vectors = vectors[:, 1:ax]
axisname = ["Axis-$i" for i = 1:ax]
Dict(:ax => ax, :n => n, :values => values, :vectors => vectors,
:axisname => axisname, :name => name)
end
function printqt4(obj)
axisname = obj[:axisname]
val = obj[:values]
val2 = val ./ sum(val)
println("\n", NamedArray(hcat(val, val2, cumsum(val2))', (["Eigenvalue", "Contribution", "Cum. cont."], axisname)))
println("\nvector\n", NamedArray(obj[:vectors], (obj[:name], axisname)))
end
function plotqt4(obj)
obj[:ax] >= 2 || error("解が一次元なので,二次元配置図は描けません。")
xy = obj[:vectors][:, 1:2]
label = [" $s" for s in obj[:name]]
pyplot()
plt = scatter(xy[:, 1], xy[:, 2], grid=false,
tick_direction=:out, color=:black,
xlabel="Axis-1", ylabel="Axis-2", label="")
annotate!.(xy[:, 1], xy[:, 2], text.(label, 8, :left))
display(plt)
end
s = [
0 -3 -5 -1
-1 0 -2 -3
-2 -3 0 -2
-3 -4 -1 0
];
obj = qt4(s)
printqt4(obj)
#=====
3×3 Named Adjoint{Float64, Matrix{Float64}}
A ╲ B │ Axis-1 Axis-2 Axis-3
─────────────┼─────────────────────────────
Eigenvalue │ 23.1496 21.1747 15.6757
Contribution │ 0.385827 0.352912 0.261261
Cum. cont. │ 0.385827 0.738739 1.0
vector
4×3 Named Matrix{Float64}
A ╲ B │ Axis-1 Axis-2 Axis-3
──────┼────────────────────────────────
Obj-1 │ 0.385193 0.622995 -0.462064
Obj-2 │ 0.601466 -0.529103 0.329072
Obj-3 │ -0.5328 -0.451618 -0.512021
Obj-4 │ -0.45386 0.357727 0.645014
=====#
plotqt4(obj); savefig("fig1.png")
※コメント投稿者のブログIDはブログ作成者のみに通知されます