Pascal Molin on Mon, 14 Nov 2022 14:01:55 +0100


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

[proposal] forvec over cyc


Dear Pari devs,

I consider the forvec loop one of the top features of GP programming: really useful, and painful to write.
Still:
- I find myself writing quite a lot forvec(x=vector(#c,i,[0,c[i]-1]), ... )
- I also need to iterate over the lattice Z^n

I propose to improve on these two aspects by extending forvec with the following additional syntax: if if cyc is a vector of nonnegative integers, forvec(x=cyc, ) would iterate on representatives of Z^n/cyc Z^n.

In particular, if c has positive entries, forvec(x=c, ...) is a shortcut for the former forvec(x=vector(#c,i,[0,c[i]-1]), ... )
And if c=[0,0..0], it would loop on Z^n. A choice has to be made on the ordering used, I consider the consider lexicographic ordering graded by L^1-norm.

Specifically, for n = 3, the loop forvec(x=[0,0,0],print(x)) gives
[0, 0, 0]
[-1, 0, 0]
[0, -1, 0]
[0, 0, -1]
[0, 0, 1]
[0, 1, 0]
[1, 0, 0]
[-2, 0, 0]
[-1, -1, 0]
[-1, 0, -1]
...
(the loop is infinite)

Finally, if c is a mixed vector of zero and positive values, forvec(x=c, ) does what I think the most useful : loop on graded-lex elements, with torsion components having L1 weight 0 (so one always iterates over torsion elements before moving to the next lattice element).

So that forvec(x=[2,0],print(x)) gives
[0, 0]
[1, 0]
[0, -1]
[1, -1]
[0, 1]
[1, 1]
[0, -2]
[1, -2]
[0, 2]
[1, 2]
...

I made a branch pascal-forvec-cyc which adds this syntax.
I used it myself for some time, if this sounds useful for others and reasonable for gp syntax I would be happy to have it merged.

Remark: I did not implement flags, one I could think of is to iterate on centerlifts of the quotient.

Pascal