TransWikia.com

Remove elements from a tuple with conditions

Mathematica Asked on August 5, 2021

I have a tuple mytup.
Assume two arbitrary elements from the tuples are {a1, a2, a3, a4, a5, a6, a7, a8, a9} and {b1, b2, b3, b4, b5, b6, b7, b8, b9}.

Now I want to delete the element that satisies the condition:

a1 = b1
a2 = -b2
a3 = b3
a4 = b4
a5 = -b5
a6 = b6
a7 = b7 
a8 = -b8
a9 = -b9

and the deleted element should have a8 = -1 and a9 = 1.
I tried using Gather and First to select the first element from each group but this does not work as the element with a8 = -1 and a9 = 1 is not always placed first in the group.
Is there a good way to do this?

UPDATE: from my tuple below, all elements have a8 = 1 and a9 = -1 or a8 = -1 and a9 = 1. However, the bigger data may include pairs with a8, and a9 are not plus or minus 1 and no element from this pair is deleted.
For example, both {0, -1, -1, 1, 0, -1, d, 2, 3}, {0, 1, -1, 1, 0, -1, d, -2, -3} are kept.

  mytup = {{0, -1, -1, 1, 0, -1, d, 0, 1}, {0, -1, -1, 1, 0, -1, d, 1, 
    0}, {0, -1, -1, 1, 0, -1, d, 1, -1}, {0, -1, -1, 1, 0, -1, d, -1, 
    1}, {0, -1, -1, 1, 0, 0, d, 0, 1}, {0, -1, -1, 1, 0, 0, d, 1, 
    0}, {0, -1, -1, 1, 0, 0, d, -1, 1}, {0, -1, 0, 1, 0, -1, d, 0, 
    1}, {0, -1, 0, 1, 0, -1, d, 1, 0}, {0, 0, -1, 1, -1, -1, d, 0, 
    1}, {0, 0, -1, 1, -1, -1, d, -1, 1}, {0, 0, -1, 1, -1, 0, d, 0, 
    1}, {0, 0, -1, 1, -1, 0, d, -1, 1}, {0, 0, -1, 1, 0, -1, d, 0, 
    1}, {0, 0, -1, 1, 0, -1, d, 1, 0}, {0, 0, -1, 1, 0, -1, d, 
    1, -1}, {0, 0, -1, 1, 0, -1, d, -1, 1}, {0, 0, -1, 1, 0, 0, d, 0, 
    1}, {0, 0, -1, 1, 0, 0, d, 1, 0}, {0, 0, -1, 1, 0, 0, d, 
    1, -1}, {0, 0, -1, 1, 0, 0, d, -1, 1}, {0, 0, -1, 1, 1, -1, d, 0, 
    1}, {0, 0, -1, 1, 1, -1, d, 1, 0}, {0, 0, -1, 1, 1, -1, d, 
    1, -1}, {0, 0, -1, 1, 1, 0, d, 0, 1}, {0, 0, -1, 1, 1, 0, d, 1, 
    0}, {0, 0, -1, 1, 1, 0, d, 1, -1}, {0, 1, -1, 1, 0, -1, d, 
    1, -1}, {0, 1, -1, 1, 0, -1, d, -1, 1}, {0, 1, -1, 1, 0, 0, d, 1, 
    0}, {0, 1, -1, 1, 0, 0, d, 1, -1}, {0, 1, 0, 1, 0, -1, d, 0, 
    1}, {0, 1, 0, 1, 0, -1, d, 1, 0}, {1, -1, -1, 1, 0, -1, d, 0, 
    1}, {1, -1, -1, 1, 0, -1, d, 1, 0}, {1, -1, -1, 1, 0, 0, d, 0, 
    1}, {1, -1, -1, 1, 0, 0, d, 1, 0}, {1, -1, -1, 1, 0, 0, d, -1, 
    1}, {1, -1, 0, 1, 0, -1, d, 1, -1}, {1, -1, 0, 1, 0, -1, d, -1, 
    1}, {1, 0, -1, 1, 0, 0, d, 0, 1}, {1, 0, -1, 1, 0, 0, d, 1, 
    0}, {1, 0, -1, 1, 0, 0, d, 1, -1}, {1, 0, -1, 1, 0, 0, d, -1, 
    1}, {1, 0, -1, 1, 1, -1, d, 0, 1}, {1, 0, -1, 1, 1, -1, d, 1, 
    0}, {1, 0, -1, 1, 1, 0, d, 0, 1}, {1, 0, -1, 1, 1, 0, d, 1, 
    0}, {1, 0, -1, 1, 1, 0, d, 1, -1}, {1, 0, -1, 1, 1, 0, d, -1, 
    1}, {1, 0, 0, 1, 1, -1, d, 1, 0}, {1, 0, 0, 1, 1, -1, d, 1, -1}};
gathered = 
  Gather[mytup, ((#1[[1]] == #2[[1]]) && (#1[[2]] == -#2[[2]]) && 
(#1[[3 ;; 4]] == #2[[3 ;; 
           4]]) && (#1[[5]] == -#2[[5]]) && (#1[[6]] == #2[[6]]) && 
(#1[[7]] == #2[[7]]) && (#1[[8]] == -#2[[8]]) && (#1[[9]] == 
-#2[[9]])) &];
result = Map[First]@gathered

One Answer

I think one way to do this is:

Select[mytup,
  !(#[[8]] == -1
    && #[[9]] == 1
    && MemberQ[mytup, # * {1, -1, 1, 1, -1, 1, 1, -1, -1}])
   &]

This keeps exactly the ones that fail the criterion, i.e. deletes the ones that do satisfy having their 8th part be -1, their 9th part be 1, and such that the list contains some member equal to {1, -1, 1, 1, -1, 1, 1, -1, -1} times the considered element.

Let me know if this works for you!

Correct answer by thorimur on August 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP