[Gretl-users] reversing / flipping a matrix

Riccardo (Jack) Lucchetti r.lucchetti at univpm.it
Fri Jul 10 07:37:25 EDT 2009

On Fri, 10 Jul 2009, Sven Schreiber wrote:

> Am 10.07.2009 12:49, Riccardo (Jack) Lucchetti schrieb:
>> On Fri, 10 Jul 2009, Sven Schreiber wrote:
>>>> Oh, sorry, I should have said: the rev() function reverses the order of
>>>> the _rows_ of the matrix (from the example above, it could have been the
>>>> columns).
>>> Would it be worthwhile to generalize this to columns as well? Maybe
>>> using an optional second argument or so. I'm not sure about it, and I'm
>>> aware of rev(A')', just asking as long as it's still in "design stage".
>> If you ask me, I'd rather have one function with the user in charge of
>> transposing things twice, rather than two and save the user the
>> inconvenience. IMHO, all other things being equal, the fewer functions
>> the better. But of course I'm open to change my mind on this.
> I agree that two separate functions would not be good. What I meant was
> something like:
> rev(A,1)	-- reverses row-wise
> rev(A,2)	-- reverses col-wise (or maybe rev(A,'c'))
> rev(A)		-- defaults to /alias for rev(A,1)
> But I don't have a strong opinion, given gretl's short transposition syntax.

(I'm sending this to gretl-dev too, since it's a bit technical)

This may make sense, with a slight modification: instead of 1/2, I'd 
rather use 0/1, so it becomes easy to decide on a logical condition (eg. 
rev(A, foo>bar)). Besides, reversing columns would probably be more 
CPU-efficient, since we could use memcpy() for that, as in

----- C code -----------------------------------------------------
gretl_matrix *gretl_matrix_reverse_cols (const gretl_matrix *m)
     int i, r, c;
     gretl_matrix *ret;

     if (m == NULL) {
 	return NULL;

     if (gretl_is_null_matrix(m)) {
 	return gretl_null_matrix_new();

     r = m->rows;
     c = m->cols;
     ret = gretl_matrix_alloc(r, c);

     if (ret == NULL) {
 	return NULL;

     double *x = m->val;
     double *y = ret->val;
     int size = r * sizeof *x;
     y += r*(c-1);

     for (i=0; i<c; i++) {
 	memcpy(y, x, size);
 	x += r;
 	y -= r;

     return ret;
----- end C code -------------------------------------------------

Allin, what do you think?

Riccardo (Jack) Lucchetti
Dipartimento di Economia
Università Politecnica delle Marche

r.lucchetti at univpm.it

More information about the Gretl-users mailing list