On Tue, 1 Nov 2011, Allin Cottrell wrote:
(b) Row and/or column names are attached to the gretl_matrix struct
itself, rather than the wrapper. That is more plausible, but I still
don't like it much. The extra memory required for an empty
gretl_matrix would only be two pointers (8 bytes on a 32-bit system)
but it represents additional complexity that I'd prefer to keep out
of what is in concept a mathematical type.
I agree: matrices should stay as lean-and-mean as possible. Besides, there
is the possibility of achieving Artur's original aim in Hansl, albeit in a
way that most people will find a bit convoluted: here's two ways to do it.
My personal preference goes to func2, but I happen to like bundles quite a
lot.
<hansl>
set echo off
set messages off
function string func1(scalar r, scalar c, matrix *m)
string rl = ""
loop i=1..r --quiet
sprintf rl "%s,row_%d", rl, i
end loop
string cl = ""
loop i=1..c --quiet
sprintf cl "%s,col_%d", cl, i
end loop
m = zeros(r,c)
sprintf ret "%s %s", rl, cl
return ret
end function
function bundle func2(scalar r, scalar c)
bundle ret
string rl = ""
loop i=1..r --quiet
sprintf rl "%s,row_%d", rl, i
end loop
ret["Rlab"] = rl
string cl = ""
loop i=1..c --quiet
sprintf cl "%s,col_%d", cl, i
end loop
ret["Clab"] = cl
m = zeros(r,c)
ret["X"] = m
return ret
end function
# ----- using func1 -----------------------------
a = {}
boo = func1(3,2,&a)
rowlab = strsub(strsplit(boo, 1), ",", " ")
collab = strsub(strsplit(boo, 2), ",", " ")
rownames(a, rowlab)
colnames(a, collab)
print a
# ----- using func2 -----------------------------
bundle zoo = func2(3,2)
rowlab = zoo["Rlab"]
collab = zoo["Clab"]
a = zoo["X"]
rownames(a, rowlab)
colnames(a, collab)
print a
</hansl>
Riccardo (Jack) Lucchetti
Dipartimento di Economia
Università Politecnica delle Marche
r.lucchetti(a)univpm.it
http://www.econ.univpm.it/lucchetti