John Cremona on Sun, 10 Mar 2024 16:34:36 +0100


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

Re: Vecsize?


Thanks Bill -- I like your trick of using gp2c to answer small questions of the form "I know how to do this in gp, but how do I do it in C?"

About your double loop construction of the matrix -- I knew something like that would work but was glad that doing zero_ZM_copy() was available instead!  I can see myself writing a function which wraps your loop to construct a matrix of size mxn given m,n and a function f(i,j) to provide the entries.  Again, I think that for users (as opposed to PARI developers, which I am not!) some protection from having to use cgetg and adding 1 to the dimension would make libpari more accessible (and hence used more).  At least by oldies like myself...

John

On Sun, 10 Mar 2024 at 15:10, Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
On Sun, Mar 10, 2024 at 12:03:33PM +0000, John Cremona wrote:
> Libpari has a function matsize which gives the pair [nrows,ncols] of a
> matrix but I could not find a similar vecsize for vectors. This is for use
> in a C program, I know that you can use # in GP. My solution is to use
> matsize anyway and ignore one of the numbers it gives (which is 1).

You can use length (and lg(x)-1) in C:

This is the kind of question you can solve with gp2c:

echo 'f(x)=#x'|./gp2c
...
long
f(GEN x)
{
  return glength(x);
}

echo 'f(x:vec)=#x'|./gp2c

long
f(GEN x)
{
  if (!is_matvec_t(typ(x)))
    pari_err_TYPE("f",x);
  return lg(x)-1;
}

> I also could not find a function which would create an nxn matrix of
> integers, ready to be filled with C ints in a double loop,

Yes, I do not think this exists. we usualy do

M = cgetg(n+1,t_MAT);
for(i=1;i<=n; i++)
{
  GEN v = cgetg(n+1, t_COL) (or t_VECSMALL);
  for(j=1;j<=n; j++)
    gel(v,j) = (or   v[j] = ...);
  gel(M,i) = v;

}

> but creating a
> zero matrix of the right size works ok.

Yes you can use zero_Flm_copy

> To end on a positive note, your matsnf works a whole lot better and faster
> than the equivalent function in flint!

Cheers,
Bill