Skip to content

Commit 9d97757

Browse files
Alexey Stukalovalyst
authored andcommitted
simplify duplication_matrix()
1 parent c64f9d1 commit 9d97757

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

src/additional_functions/helper.jl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,19 @@ function sparse_outer_mul!(C, A, B::Vector, ind) #computes A*S*B -> C, where ind
104104
end
105105
end
106106

107-
function duplication_matrix(nobs)
108-
nobs = Int(nobs)
109-
n1 = Int(nobs * (nobs + 1) * 0.5)
110-
n2 = Int(nobs^2)
111-
Dt = zeros(n1, n2)
112-
113-
for j in 1:nobs
114-
for i in j:nobs
115-
u = zeros(n1)
116-
u[Int((j - 1) * nobs + i - 0.5 * j * (j - 1))] = 1
117-
T = zeros(nobs, nobs)
118-
T[j, i] = 1
119-
T[i, j] = 1
120-
Dt += u * transpose(vec(T))
107+
# n²×(n(n+1)/2) matrix to transform a vector of lower
108+
# triangular entries into a vectorized form of a n×n symmetric matrix,
109+
# opposite of elimination_matrix()
110+
function duplication_matrix(n::Integer)
111+
ntri = div(n * (n + 1), 2)
112+
D = zeros(n^2, ntri)
113+
for j in 1:n
114+
for i in j:n
115+
tri_ix = (j - 1) * n + i - div(j * (j - 1), 2)
116+
D[j+n*(i-1), tri_ix] = 1
117+
D[i+n*(j-1), tri_ix] = 1
121118
end
122119
end
123-
D = transpose(Dt)
124120
return D
125121
end
126122

0 commit comments

Comments
 (0)