Bill Allombert on Sun, 10 Mar 2024 16:10:42 +0100


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

Re: Vecsize?


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