Bill Allombert on Sun, 11 Dec 2005 11:49:10 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: matsolve() gp function and PARI matrix allocation in C


On Sun, Dec 11, 2005 at 03:04:09AM +0100, Alessio Rocchi wrote:
> 2) the second question is related to matrix dynamic allocation with libpari
> and C.
> This one is not a real problem: only a doubt i have about correct use of
> pari.
> I alloc dynamic mxn matrices using the standard C way:
> 
> GEN** mat;
> long size;
> mat=(GEN**)malloc(size*sizeof(GEN*);
> 
> Is this the correct way to do it in parior is maybe there some better (and
> maybe faster) way?

Depends. This will allocate a C matrix of GEN but not a PARI matrix. 
PARI matrices are very different and PARI functions only handle PARI
matrices.

If you want to allocate a PARI matrix, you must use cgetg():

 /* allocate a nxm matrix */
 GEN M = cgetg(m+1, t_MAT);
 for (i = 1; i <= m; i++)
   gel(M, i) = cgetg(n+1, t_COL);

This will allocate it in the PARI stack, which is much fatser than
malloc().

I would really suggest you try gp2c.
For example feeding gp2c with
f(n:small,m:small)=matrix(n,m)
essentially give you the snippet above.

Cheers,
Bill.