Loïc Grenié on Sat, 16 Nov 2024 12:09:22 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: some questions on mapping from an integer to a vector and inversely |
On Fri, Nov 15, 2024 at 04:03:43PM -0800, American Citizen wrote:
> Hello:
>
> My topic is on how to develop mapping from one integer n into n-integers
> [vector] , or 1-dimensional space to n-dimensional space.
>
> This is a mapping of 1d(n) --> nd[n] and of course, the inverse map which
> takes the n-dim vector back to the integer.
>
> map_xy_to_n(x,y) = (y^2 + (2*x + 1)*y + x*(x + 3))/2;
> map_n_to_xy(n) =
> S=floor((sqrt(8*n+1)-1)/2);T=S*(S+1)/2;x=n-T;y=S-x;return([x,y]);
> bi_Z_to_N(Z)=if(Z<0,return(-2*Z-1),return(2*Z));
> bi_N_to_Z(N)=if(N%2==0,return(N/2),return(-(N+1)/2));
Maybe you have extra requirement, but otherwise you can just apply
map_xy_to_n recursively. For example for 3 variables:
map_xy_to_n(x,y) = (y^2 + (2*x + 1)*y + x*(x + 3))/2;
map_n_to_xy(n) = S=floor((sqrt(8*n+1)-1)/2);T=S*(S+1)/2;x=n-T;y=S-x;return([x,y]);
map_xyz_to_n(x,y,z)=map_xy_to_n(x,map_xy_to_n(y,z))
map_n_to_xyz(n)=my([x,yz]=map_n_to_xy(n),[y,z]=map_n_to_xy(yz));[x,y,z]
for(i=1,20,my([x,y,z]=map_n_to_xyz(i));print(i,":",[x,y,z],":",map_xyz_to_n(x,y,z)))