5
$\begingroup$

I have a list with complex numbers. I would like to delete a number $z$ that is in my list if at least one of these conditions is met:

  • $-z$ is also in the list
  • $\bar{z}$ is in the list
  • $-\bar{z}$ is in the list

Where $\bar{z}$ represents the complex conjugate of $z$.

For example in the list: $$L=\{4,2+\mathrm{i},-4,3,-2+\mathrm{i},-2-\mathrm{i},2-\mathrm{i}\}$$ After applying the algorithm only the element $3$ remains: $$L\to\tilde{L}=\{3\}$$

The problem is that I can have up to 6000 elements in my list. Is there a smart way to proceed?

$\endgroup$

2 Answers 2

4
$\begingroup$

Or use Gather

Gather[
{4, 2 + I, -4, 3, -2 + I, -2 - I, 2 - I}, #1 == Conjugate[#2] || #1 + #2 == 
     0 || #1 + Conjugate[#2] == 0 & ] // Cases[{x_} :> x]
{3}
$\endgroup$
0
7
$\begingroup$

You could use GroupBy:

Cases[
    GroupBy[{4, 2+I, -4, 3, -2+I, -2-I, 2-I}, Abs @* ReIm],
    {v_} :> v
]

{3}

$\endgroup$
2
  • 1
    $\begingroup$ I'd suggest Abs@Re@# + I*Abs@Im@# & instead of Norm. (Consider {..., -4 + 3 I, 3 + 4 I,...}.) $\endgroup$
    – Michael E2
    Mar 19, 2021 at 16:01
  • $\begingroup$ @MichaelE2 good point $\endgroup$
    – Carl Woll
    Mar 19, 2021 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.