using TensorDec
normalize_markov! = function(w,A,B,C)
for i in 1:size(A,2)
l = sum(A[j,i] for j in 1:size(A,1) )
A[:,i] /= l
w[i] *= l
end
for i in 1:size(B,2)
l = sum(B[j,i] for j in 1:size(B,1) )
B[:,i] /=l
w[i] *= l
end
for i in 1:size(C,2)
l = sum(C[j,i] for j in 1:size(B,1) )
C[:,i] /=l
w[i] *= l
end
w, A,B,C
end
#3 (generic function with 1 method)
A = rand(4,4); B = rand(4,4); C = rand(4,4); w = rand(4);
normalize_markov!(w,A,B,C)
fill(1.,4)'*A, fill(1.,4)'*B, fill(1.,4)'*C
([1.0 1.0 1.0 1.0], [1.0 1.0 1.0 1.0], [1.0 1.0 1.0 1.0])
t = tensor(w,A,B,C)
4×4×4 Array{Float64,3}:
[:, :, 1] =
0.259248 0.349942 0.364485 0.085319
0.215528 0.270488 0.262803 0.0684887
0.214908 0.479766 0.506224 0.0540596
0.269875 0.450153 0.478336 0.0683525
[:, :, 2] =
0.313488 0.384201 0.402716 0.115903
0.256093 0.26977 0.265292 0.0868339
0.221668 0.521591 0.562154 0.0653809
0.300196 0.468211 0.499396 0.0853609
[:, :, 3] =
0.357105 0.223798 0.198387 0.143608
0.312446 0.211622 0.16047 0.130103
0.13943 0.288979 0.270791 0.0376782
0.275407 0.277452 0.270645 0.0943938
[:, :, 4] =
0.270442 0.247415 0.242189 0.138953
0.224147 0.138707 0.092527 0.101356
0.104812 0.344 0.350852 0.0728985
0.187994 0.211849 0.209937 0.0853464
wd, Ad, Bd, Cd = decompose(t)
normalize_markov!(wd,Ad,Bd,Cd)
([0.718702, 10.0193, 2.8635, 2.03042], [0.158147 0.216844 0.407548 0.317121; 0.368735 0.174449 0.348307 0.0277879; 0.337246 0.298045 0.0128447 0.477643; 0.135872 0.310662 0.2313 0.177448], [0.196824 0.184114 0.632411 0.0619155; 0.549973 0.376328 0.0654333 0.386497; 0.101599 0.41063 0.0109438 0.452418; 0.151604 0.0289275 0.291212 0.0991702], [0.179996 0.35144 0.146618 0.161332; 0.125831 0.351245 0.214783 0.292323; 0.368812 0.203257 0.360999 0.0280855; 0.32536 0.0940577 0.2776 0.518259])
w
4-element Array{Float64,1}:
2.8635040860546987
10.019347309230545
2.0304186435229035
0.7187016887591559
wd
4-element Array{Float64,1}:
0.718701688759023
10.019347309230689
2.863504086054696
2.0304186435228844
A
4×4 Array{Float64,2}:
0.407548 0.216844 0.317121 0.158147
0.348307 0.174449 0.0277879 0.368735
0.0128447 0.298045 0.477643 0.337246
0.2313 0.310662 0.177448 0.135872
Ad
4×4 Array{Float64,2}:
0.158147 0.216844 0.407548 0.317121
0.368735 0.174449 0.348307 0.0277879
0.337246 0.298045 0.0128447 0.477643
0.135872 0.310662 0.2313 0.177448
using LinearAlgebra
norm(t-tensor(wd,Ad,Bd,Cd))
1.8522595838390253e-14