On Sat, 2 Jan 2021, Sven Schreiber wrote:
Hi,
I was wondering what's an efficient way to calculate a (pre-)
multiplication with the duplication matrix.
(For a definition of what I mean see here:
https://en.wikipedia.org/wiki/Duplication_and_elimination_matrices
or here:
https://www.rdocumentation.org/packages/matrixcalc/versions/1.0-3/topics/...)
It's of course very easy to construct that matrix explicitly and then do
the multiplication, and I already have a function for that, but I
suspect it's not very efficient. (Similar to the commutation matrix,
where in the 'extra' addon we have a function called 'commute' which
achieves that without actually creating the interim matrix.)
Very much untested:
<hansl>
function matrix Dup(const matrix A)
scalar m = rows(A)
H = unvech(seq(1,m)')
ret = A[vec(H),]
return ret
end function
function matrix Elim(const matrix A)
scalar n2 = rows(A)
scalar n = round(sqrt(rows(A)))
ndx = vec(lower(mshape(seq(1,n2), n, n)))
ndx = selifr(ndx, ndx.>0)
ret = A[ndx,]
return ret
end function
</hansl>
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------