Bill Allombert on Sat, 16 Nov 2024 11:28:26 +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))) 1:[0,0,1]:1 2:[1,0,0]:2 3:[0,1,0]:3 4:[1,0,1]:4 5:[2,0,0]:5 6:[0,0,2]:6 7:[1,1,0]:7 8:[2,0,1]:8 9:[3,0,0]:9 10:[0,1,1]:10 11:[1,0,2]:11 12:[2,1,0]:12 13:[3,0,1]:13 14:[4,0,0]:14 15:[0,2,0]:15 16:[1,1,1]:16 17:[2,0,2]:17 18:[3,1,0]:18 19:[4,0,1]:19 20:[5,0,0]:20 Cheers, Bill.