0

Is there a technique to non-recursively generate all pair combinations of numbers in an integer sequence. For example for the sequence 0 ... 4:

(1,0)
(2,0) (2,1)
(3,0) (3,1) (3,2)
(4,0) (4,1) (4,2) (4,3)

To clarify: I would like generate a unique combination(i,j) from an arbitrary "index" k. Here's my motivation. I'm writing a GPU kernel in CUDA where each thread considers all pairs of elements from a list and does a computation on them. But the only information that I have is a thread index. And from this thread index I'd like to generate the pair (i,j)

Olumide
  • 1,241
  • That's trivial and not really about mathematics. – Henrik supports the community May 17 '17 at 20:43
  • It is trivial: Table[{i, j}, {i, 4}, {j, 0, i - 1}] // MatrixForm in Mathematica – David G. Stork May 17 '17 at 20:44
  • I'm not using mathematica. – Olumide May 17 '17 at 20:45
  • Move this to stack overflow – u8y7541 May 17 '17 at 22:24
  • @u8y7541 The use of this combination on a GPU does not make it a stack overflow question. What I am having difficulty with is the generation of the pairs of combinations not the GPU programming. I only provided the clarification about the motivation for the question because the question was incorrectly deemed to be trivial. – Olumide May 17 '17 at 23:16
  • I have already asked a related question on three programming forums (including SO) but did not get an answer. SO regulars (like me) usually do not know stuff like triangular numbers. My understanding was that this forum is for non-reseach level mathematics questions. – Olumide May 18 '17 at 06:27

1 Answers1

2

I am assuming you want the first element of the pair to be greater than the second and you want them in lexicographic order, so in your example $(4,1)$ would be at index $8$. If you have $n$ elements in your list there will be $\frac 12n(n-1)$ possible pairs. As you did in the example, consider the list to be $0,1,2,\ldots n-1$. The number of pairs with first element up to and including $k$ is $\frac 12k(k+1)$, the $k^{th}$ triangular number. Given an index $m$ we compute the first element as the largest triangular number strictly below $m$ plus $1$ as $p=\lfloor\frac 12(-1+\sqrt{8(m-1)+1})\rfloor $ Then we have used up $\frac 12p(p+1)$ of the elements, so the second element is $(m-1)-\frac 12p(p+1)$ That gives you the pair.

Ross Millikan
  • 374,822
  • The first element needn't be greater than the second. In fact the pairs needn't be in lexicographic order. I only need the pairs to be unique combinations. – Olumide May 17 '17 at 23:30
  • Do you need the pairs the other way as well, or are they really unordered pairs? This works for unordered pairs. It will give them to you with the first element greater, and never have the two elements equal. Please think about what your requirements are and make them explicit. Do you also need to generate them in the other order, so there will be twice as many? – Ross Millikan May 17 '17 at 23:36
  • No I do not need the elements the other way. I'm okay with the the first element being greater than the second. (I only meant to say that need just one pair $(p,q)$ or $(q,p)$ but not both -- sorry if my language is less than precise I am a novice mathematician). – Olumide May 17 '17 at 23:43
  • It just comes from applying the quadratic formula to $m=\frac 12p(p+1)$ and is mentioned in the Wikipedia article. The $m-1$ comes because if $m$ is a triangular number we want the old first element, not the new one. – Ross Millikan May 17 '17 at 23:44
  • The formula works better with $m$ instead of $m - 1$. The former produces the exact pairs of numbers that I am interested in. – Olumide May 20 '17 at 20:37
  • I may have gotten off by $1$ when considering whether to start counting at $0$ or $1$. – Ross Millikan May 20 '17 at 20:39
  • Ironically my original SO question was answered today with a reference to an identical question that also used a more unwieldy formula based on triangular numbers to generate the upper triangular matrix. http://stackoverflow.com/questions/44001289/operating-on-combining-all-pairs-of-objects-in-a-sequence – Olumide May 20 '17 at 20:44
  • Do you think this idea can be generalized to n-tuples using polygonal numbers? – Olumide Jun 01 '17 at 10:20