Skip to content

Commit 69b16de

Browse files
Alexey Stukalovalyst
authored andcommitted
simplify duplication_matrix()
1 parent ebed5c5 commit 69b16de

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
@@ -106,23 +106,19 @@ function sparse_outer_mul!(C, A, B::Vector, ind) #computes A*S*B -> C, where ind
106106
end
107107
end
108108

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

0 commit comments

Comments
 (0)