Suppose I have a vector.
I assign to every vector index a four-element array, in such a way that index i corresponds to {x1,p1,x2,p2}, where x1,x2 range from a $[-n,n]$ interval ($n$ is a constant defined elsewhere, used to define the dimension of my system) while p1,p2 are either $0$ or $1$. So, if the vector is {a,b,c,d,...,z}, for example, for $n=1$, I have
1 ------> {-1,0,-1,0}------> a 2 ------> {-1,0,-1,1}------> b 3 ------> {-1,0,0,0} ------> c ... 36------> {1,1,1,1} ------> z What I want to do now is rearranging the vector in such a way that the indexes are ordered like {x1,x2,p1,p2}:
1 ------> {-1,0,-1,0}------> a 2 ------> {-1,0,-1,1}------> b 7 ------> {-1,1,-1,0}------> g 8 ------> {-1,1,-1,1}------> h ... 36 -----> {1,1,1,1} ------> z Then, I'll have to generate another vector summing a+b+g+h as the first element, and so on, summing 4 elements at a time. At the end, I'll have a 9-element vector:
1 -----> {-1,-1} -----> a+b+g+h ... 9 -----> {1,1} -----> j+k+y+z Where j,k are the appropriate elements, chosen by the above rule.
What I do is writing a function that gives me all the 4-elements arrays with x1,p1,x2,p2s, then riffling this with my original vector, then ordering by x1 and x2, and finally summing the elements.
Is there a faster way to do this?
Here's my solution:
I start defining a positionFinder function:
positionFinder[index_Integer,n_]:= PadLeft[IntegerDigits[index,MixedRadix[{2n+1,2,2n+1,2}]],4,0] - {n,0,n,0}; Then I define indexes as Length[vector[n]], and
poleposition[n_]:= Table[positionFinder[i-1,n],{i,1,indexes}]; positions[n_]:= Partition[Riffle[Part[poleposition[n],All,1], Part[poleposition[n],All,3]],2]; So I have the positions list, i.e. an array {{-n(* ,0 *),-n(* ,0 *))},{-n (* ,0 *),-n (* ,1 *)},{-n(* ,1 *),-n+1(* ,0 *)}, ... , {-n(* ,1 *),-n(* ,0 *)},{-n(* ,1 *),-n(* ,1 *)},{-n(* ,1 *),-n+1(* ,0 *)} ... ,{n(* ,1 *),n(* ,1 *)}} (I commented the p1,p2 which make my array disordered). Now, I do
probGet[n_]:=Partition[Flatten[Riffle[positions[n],vector[n]]],3]; to join the positions and their correspondent vector element and
probCompute[n_]:=Sort[Sort[probGet[n],#1[[2]]>#2[[2]]&],#1[[1]]<#2[[1]]&]; to order my total array by the positions. Finally,
probExtract[n_]:= Part[probCompute[n],1;;-1;;4] + Part[probCompute[n],2;;-1;;4] + Part[probCompute[n],3;;-1;;4] + Part[probCompute[n],4;;-1;;4];
nchosen? Is it related to the length of some list? Or is it a parameter in your problem? $\endgroup$MixedRadixis new in V10.2, which I don't have, so I have another question. How does "summinga+b+g+h" result in{-1, 1}? That part is unclear. However: your finding of positions and such is unnecessary, because of you justSortyour list of 4-tuples, it will bring it automatically into the form you want. You can thenPartition[ ..., 4]and sum over those rows. I can show this if you can clear up the part about the final summation. $\endgroup${{-1,-1,a+b+g+h}, ... ,{1,1,j+k+y+z}}. So "summing" does not result in{-1,-1}, they simply become "associated". I'm sorry if I'm being unclear. $\endgroup$