Re: Select from Tuplet using logical expression

• To: mathgroup at smc.vnet.net
• Subject: [mg116979] Re: Select from Tuplet using logical expression
• From: Ray Koopman <koopman at sfu.ca>
• Date: Sun, 6 Mar 2011 05:43:49 -0500 (EST)
• References: <ikihne\$7sk\$1@smc.vnet.net> <ikq8h7\$801\$1@smc.vnet.net> <ikt5o9\$3t\$1@smc.vnet.net>

```On Mar 5, 3:11 am, Peter Pein <pet... at dordos.net> wrote:
> Am 04.03.2011 09:40, schrieb Ray Koopman:
>> On Mar 1, 2:27 am, Lengyel Tamas<lt... at hszk.bme.hu>  wrote:
>>> Hello.
>>>
>>> Skip if needed:
>>> ///I am working on a part combinatorical problem with sets of 3
>>> differently indexed values (e.g. F_i, F_j, F_k, F denoting
>>> frequency channels) which are subsets of many values (e.g 16
>>> different frequency channels, denoted F_0, F_1 ... F_15).
>>>
>>> Now, I need to select triplets from these channels, I used Tuplets.
>>> So far so good. From these I need those combinations where indexes
>>> i!=k and/or j!=k, and i=j is allowed (e.g {i,j,k} = {12, 12, 4}
>>> is a valid channel combination, but {3, 12, 3} is not).///
>>>
>>> So basically I need to generate triplets from a range of integer
>>> numbers, where the first and second elements of these triplets do
>>> not match the third. I thought Select would help, but I don't know
>>> if there exists an option to control elements' values in a condition.
>>>
>>> From then on I must use these triplets' elements in a function.
>>>
>>> But first I am asking your help in generating thos triplets of
>>> numbers.
>>>
>>> Thanks.
>>>
>>> Tam s Lengyel
>>
>> 1 - 6 have been posted previously. 7 is new, a modification of 6.
>> 1 - 5 generate all the triples, then delete unwanted ones.
>> 6&  7 generate only the triples that are wanted.
>>
>> The time differences seem to be reliable.
>>
>> r = Range[32];
>> AbsoluteTiming[t1 = Select[Tuples[r,3],
>>                      #[[1]]!=#[[3]]&&  #[[2]]!=#[[3]]&];     "1"]
>> AbsoluteTiming[t2 = Select[Tuples[r,3],
>>                      FreeQ[Most@#,Last@#]&];                "2"]
>> AbsoluteTiming[t3 = Cases[Tuples[r,3],
>>                      _?(FreeQ[Most@#,Last@#]&)];             "3"]
>> AbsoluteTiming[t4 = DeleteCases[Tuples[r,3],
>>                      _?(MemberQ[Most@#,Last@#]&)];           "4"]
>> AbsoluteTiming[t5 = DeleteCases[Tuples[r,3],
>>                      {k_,_,k_}|{_,k_,k_}];                   "5"]
>> AbsoluteTiming[t6 = Flatten[Function[ij,Append[ij,#]&/@
>>                      Complement[r,ij]] /@ Tuples[r,2], 1];   "6"]
>> AbsoluteTiming[t7 = Flatten[Outer[Append,{#},
>>                      Complement[r,#],1]&  /@ Tuples[r,2], 2]; "7"]
>> SameQ[t1,t2,t3,t4,t5,t6,t7]
>>
>> {0.378355 Second, 1}
>> {0.390735 Second, 2}
>> {0.409103 Second, 3}
>> {0.420442 Second, 4}
>> {0.140180 Second, 5}
>> {0.128378 Second, 6}
>> {0.085107 Second, 7}
>> True
>
> Ray,
>
>   you might want to add
>
> AbsoluteTiming[t8=Flatten/@Flatten[(Distribute[{{#1},
>   Complement[r,#1]},List]&)/@Tuples[r,{2}],1];"8"]
>
> which needs ~95% of the time needed to calculate t7.
>
> Peter

Thanks, Peter. Distribute is one of those functions
that I have trouble thinking of when I need them.

Timing may be version-dependent. I run old versions on old
hardware. Here are some times for methods 7 & 8, along with
a new method, 9, that is essentially 7 with Outer replaced
by Distribute.

m[7] := Flatten[ Outer[
Append,{#},Complement[r,#],1]& /@ Tuples[r,2], 2]
m[8] := Flatten /@ Flatten[ Distribute[
{{#},Complement[r,#]},List]& /@ Tuples[r,2], 1]
m[9] := Flatten[ Distribute[
Append[#,Complement[r,#]],List]& /@ Tuples[r,2], 1]

Transpose@Table[r = Range[n = 2^k];
Table[AbsoluteTiming@Do[Null,{1*^7}];
AbsoluteTiming[m[j];{j,n}],{j,7,9}],{k,4,6}]//ColumnForm

v5.2
{{0.019200, {7,16}}, {0.090837, {7,32}}, {0.681684, {7,64}}}
{{0.015259, {8,16}}, {0.113314, {8,32}}, {0.823682, {8,64}}}
{{0.006522, {9,16}}, {0.040577, {9,32}}, {0.260543, {9,64}}}

v6.0
{{0.016032, {7,16}}, {0.112156, {7,32}}, {0.942626, {7,64}}}
{{0.016059, {8,16}}, {0.124596, {8,32}}, {0.985309, {8,64}}}
{{0.007115, {9,16}}, {0.050089, {9,32}}, {0.251023, {9,64}}}

However, if I were coding a program that needed one of these
routines, and if neither time nor space were critical, I
would be tempted to use method 5, because it's not all that
slow and it's by far the clearest, the most self-documenting.

```

• Prev by Date: Re: Select from Tuplet using logical expression
• Next by Date: Re: Vector Runge-Kutta ODE solver with compilation?
• Previous by thread: Re: Select from Tuplet using logical expression
• Next by thread: Re: Select from Tuplet using logical expression