[Gretl-devel] Small bug

Allin Cottrell cottrell at wfu.edu
Thu May 5 08:56:44 EDT 2011


On Thu, 5 May 2011, Sven Schreiber wrote:

> Am 05.05.2011 07:32, schrieb Marcin Błażejowski:
> > Hi,
> > simple script:
> >
> > <script>
> > set echo off
> > set messages off
> >
> > function void FOO (matrix mat)
> >     printf "%d\n", nelem(mat)
> > end function
> >
> > nulldata 10
> >
> > matrix foo = zeros(2,1)
> > FOO(foo)
> >
> > foo[1,1] = 1
> > FOO(foo)
> > </script>
> >
> > In both situations we get what we excpect: 2
>
> I'm surprised it works at all, according to the docs nelem() is only for
> lists, not for matrices.

Sven is right: in concept, nelem() is only for lists. For
matrices, use rows() or cols(). However, to facilitate
manipulating lists, it's possible to "cast" from a (suitable)
matrix to a list, where a suitable matrix is a vector containing
nothing but non-negative integers.

What's happening here is that Marcin's matrix looks OK as a
representation of a list, but if it's interpreted that way then in
the second case it references the non-existent series 2.

I think it might be better (to avoid confusing error messages) to
modify "genr" so that the cast from a matrix to a list must be
done explicitly. In that case you'd get a "non-conformable types"
error if you try using nelem() on any object that's not already
defined as a list.

Allin





More information about the Gretl-devel mailing list