Skip to content

Commit fd212c0

Browse files
Alexey Stukalovalyst
authored andcommitted
simplify duplication_matrix()
1 parent 47a1757 commit fd212c0

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/additional_functions/helper.jl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,19 @@ function cov_and_mean(rows; corrected = false)
111111
return obs_cov, vec(obs_mean)
112112
end
113113

114-
function duplication_matrix(nobs)
115-
nobs = Int(nobs)
116-
n1 = Int(nobs * (nobs + 1) * 0.5)
117-
n2 = Int(nobs^2)
118-
Dt = zeros(n1, n2)
119-
120-
for j in 1:nobs
121-
for i in j:nobs
122-
u = zeros(n1)
123-
u[Int((j - 1) * nobs + i - 0.5 * j * (j - 1))] = 1
124-
T = zeros(nobs, nobs)
125-
T[j, i] = 1
126-
T[i, j] = 1
127-
Dt += u * transpose(vec(T))
114+
# n²×(n(n+1)/2) matrix to transform a vector of lower
115+
# triangular entries into a vectorized form of a n×n symmetric matrix,
116+
# opposite of elimination_matrix()
117+
function duplication_matrix(n::Integer)
118+
ntri = div(n * (n + 1), 2)
119+
D = zeros(n^2, ntri)
120+
for j in 1:n
121+
for i in j:n
122+
tri_ix = (j - 1) * n + i - div(j * (j - 1), 2)
123+
D[j+n*(i-1), tri_ix] = 1
124+
D[i+n*(j-1), tri_ix] = 1
128125
end
129126
end
130-
D = transpose(Dt)
131127
return D
132128
end
133129

0 commit comments

Comments
 (0)