2
votes

l have a matrix of n by n that l transformed to a lower triangular matrix. Then l want to transform the lower traingular matrix using vec() but it doesn't work. here is my code error

x=rand(4,4)
4x4 Array{Float64,2}:
 0.453539  0.00897806  0.590051  0.401164
 0.219694  0.322613    0.826784  0.528058
 0.216173  0.752796    0.318784  0.721735
 0.648246  0.0184203   0.6562    0.69907 

c=LowerTriangular(x)

4x4 LowerTriangular{Float64,Array{Float64,2}}:
 0.453539  0.0        0.0       0.0    
 0.219694  0.322613   0.0       0.0    
 0.216173  0.752796   0.318784  0.0    
 0.648246  0.0184203  0.6562    0.69907

vec(c)
ERROR: ArgumentError: Triangular matrix must have two dimensions
 in similar at linalg/triangular.jl:27
 in reshape at abstractarray.jl:213
 in vec at abstractarraymath.jl:14

is there any function to transform triangular matrix to a vector ?

Thank you

2
you can also use [c...] - Gnimuc

2 Answers

5
votes

You can use collect

julia> x=rand(4,4)
4x4 Array{Float64,2}:
 0.98258   0.786791  0.776911  0.282855
 0.881244  0.270507  0.557487  0.376426
 0.915609  0.165074  0.388279  0.107354
 0.461926  0.210621  0.676718  0.161106

julia> y=LowerTriangular(x)
4x4 LowerTriangular{Float64,Array{Float64,2}}:
 0.98258   0.0       0.0       0.0     
 0.881244  0.270507  0.0       0.0     
 0.915609  0.165074  0.388279  0.0     
 0.461926  0.210621  0.676718  0.161106

julia> collect(y)
16-element Array{Float64,1}:
 0.98258 
 0.881244
 0.915609
 0.461926
 0.0     
 0.270507
 0.165074
 0.210621
 0.0     
 0.0     
 0.388279
 0.676718
 0.0     
 0.0     
 0.0     
 0.161106
3
votes

do

x = rand(4,4)
c=LowerTriangular(x)
lower_triangular_matrix = convert(Array{Float64,2}, c)
l_t_vel = vec(lower_triangular_matrix)

Whats seems to happening is that you have a type LowerTriangular and the vec() function wont accept that type as an argument, so you just convert it right back to a matrix

Edit/Appendix

You can use

methodswith(LowerTriangular)

to see what methods the type LowerTriangular appears in

and you can use

methods(vec)
# 3 methods for generic function "vec":
vec{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}) at sparse/sparsematrix.jl:243
vec(a::AbstractArray{T,1}) at abstractarraymath.jl:15
vec(a::AbstractArray{T,N}) at abstractarraymath.jl:14

to see what "dispatches" exist (if that's a phrase that makes sense) exist for a method