On Fri, 12 Oct 2018, Sven Schreiber wrote:
3) Want to add an 'eliminate' function as a sister of
'commute', which
returns the result of pre-multiplying with the elimination matrix. Haven't
looked at whether post-multiplication would be wanted as well. Here's my
code:
<hansl>
function matrix eliminate(const matrix vecA)
# The input vecA is assumed to come from the
# operation vec(A) on a square matrix, thus
# rows(vecA) must be a square number.
# Returns vech(A), which is the result of pre-
# multiplying vec(A) with the "elimination"
# matrix L_m.
if cols(vecA) != 1
funcerr "input must be a column vector"
endif
sqr = sqrt(rows(vecA)
if sqr != round(sqr)
funcerr "input must have a square number of elements"
endif
return vech(mshape(vecA, sqr, sqr))
end function
</hansl>
In some cases, you may want to apply the elimination operation to matrices
that have more than one column. I'd modify your function as
<hansl>
function matrix eliminate(const matrix vecA)
r = sqrt(rows(vecA))
if r != round(r)
funcerr "input must have a square number of rows"
endif
e = vech(mshape(seq(1,r^2), r, r)')
return vecA[e,]
end function
</hansl>
The same goes for the duplication operation, I guess.
-------------------------------------------------------
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
-------------------------------------------------------