unordered pairs in q

Given a set s of, say, numbers I would like to produce the set ofpairs (x y), where both x and y are in s and x<y essentiallygives the set of unordered pairs i wrote iterativeimplementation below but there must be a more elegant solution inq …-peterq s crosss>

As often happens, I saw the light soon after I had posted the message.Here is a more elegant solution:q)upairs:{[s] scs where {(x 0) < x 1} each scs:s cross s}q)upairs 1 2 31 21 32 3q)-PeterOn Mar 23, 4:45?pm, Peter <szer…> wrote:> Given a set s of, say, numbers I would like to produce the set of> pairs (x y), where both x and y are in s and x gives the set of unordered pairs). I wrote the iterative> implementation below, but there must be a more elegant solution in> q …>> -Peter>> q)upairs:{[s] () {$[(y 0)<(y 1);(enlist ((y 0);(y 1))),x;x]}/ s cross> s }> q)upairs (1 2 3)> 2 3> 1 3> 1 2> q)</szer…>

q)f:{p where{x[;0]q)f 2 1 3
2 3
1 2
1 3

2010/3/23 Peter :
> Given a set s of, say, numbers I would like to produce the set of
> pairs (x y), where both x and y are in s and x> gives the set of unordered pairs). I wrote the iterative
> implementation below, but there must be a more elegant solution in
> q …
>
> -Peter
>
> q)upairs:{[s] () {$[(y 0)<(y 1);(enlist ((y 0);(y 1))),x;x]}/ s cross
> s }
> q)upairs (1 2 3)
> 2 3
> 1 3
> 1 2
> q)
>
> –
>

Submitted via Google Groups

my solution needs to use a global but it would be faster for long list.

upairs2:{`res set ();{if[not 2=3Dcount x;.z.s 1_x];res,:((count
1_x)#(1#x)),'(1_x)}asc x ;:res}

q)p:-100?1000
q)\t do[1000;upairs2 p]
944
q)\t do[1000;upairs p]
4091

(asc upairs2 p)~(asc upairs p)

Xi

try

q){x raze(til each a),:'a:til count x}

Regards,
Attila

This seems not working; for example:

q){x raze(til each a),:'a:til count x} 3 2 1
3 2
3 1
2 1
the x
Regards,
Petru

right, i would say that the important property is that you have only
the unique pairs
but if you insist

q){x raze(til each a),:'a:til count x:asc x} 3 2 1
1 2
1 3
2 3

Attila

On Mar 23, 4:45 pm, Peter <szer…> wrote:> Given a set s of, say, numbers I would like to produce the set of> pairs (x y), where both x and y are in s and x gives the set of unordered pairs).On Mar 26, 12:23?am, Attila Vrabecz <attila.vrab…> wrote:> right, i would say that the important property is that you have only> the unique pairs.Yes, I agree with Attila.I collected all the five posted solutions, and removed the sorting(asc x) wherever applicable. Wrote a 6th one (which, of course, buildson the programs posted earlier) using a while loop. Below is the codeand some test runs, including timings. It’s quite interesting that thefirst variant runs well over two magnitudes slower than the last…-Peterup1:{[s] () {$[(y 0)<(y 1);(enlist ((y 0);(y 1))),x;x]}/ s cross s }up2:{p where{x[;0]</attila.vrab…></szer…>

q)up7:{raze x,/:'(1_)scan 1_x}

Attila