[Gretl-users] [Gretl-devel] saving the row/col names of a matrix

Riccardo (Jack) Lucchetti r.lucchetti at univpm.it
Wed Nov 2 03:17:02 EDT 2011


On Wed, 2 Nov 2011, Riccardo (Jack) Lucchetti wrote:

> 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.

Sorry for the noise: (1) I forgot to send my previous message to the 
users' list; (2) there was a small glitch in func2 (corrected below); (3) 
I'd like to express my point more explicitly: IMO, base types such as 
matrices should stay as unencumbered by extra info (or metadata, call it 
what you want) in the interest of efficiency. If you really need to wrap 
up a complex set of information into one entity, that's exactly what 
bundles are for.


<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 at univpm.it
http://www.econ.univpm.it/lucchetti


More information about the Gretl-users mailing list