8

How can I flip the sign of the real part but not affect the imaginary part of a complex number:

a+bi => -a + bi

Example list:

list = {{-0.282095 + 0.282095 I, -0.27254 + 0.291336 I, 
-0.262018 + 0.300835 I, -0.250437 + 0.310542 I}}

expected:

{{0.282095 + 0.282095 I, 0.27254 + 0.291336 I, 
0.262018 + 0.300835 I, 0.250437 + 0.310542 I}}

So it's "similar" to conjugate but works on the real not imaginary.

leon365
  • 649
  • 4
  • 8

3 Answers3

30
-Conjugate[list]

(* {{0.282095 + 0.282095 I, 0.27254 + 0.291336 I, 
     0.262018 + 0.300835 I, 0.250437 + 0.310542 I}} *)
WReach
  • 68,832
  • 4
  • 164
  • 269
10
list /. Complex[x_, y_] :> Complex[-x, y]

{{0.282095 + 0.282095 I, 0.27254 + 0.291336 I, 0.262018 + 0.300835 I, 0.250437 + 0.310542 I, 2, 3 I}}

Suba Thomas
  • 8,716
  • 1
  • 17
  • 32
  • 1
    This assumes that all entries are truly Complex[_,_]. But for a list {{-0.282095 + 0.282095 I, -0.3445}}, which also contains only complex numbers (although one of them happens to be purely real, and even have Real head), this won't work correctly. – Ruslan Oct 03 '19 at 05:20
  • Agreed. This solution is for complex numbers of the form a + b i that was asked in the question. To include real numbers it needs to be modified to {Complex[x_,y_]:>Complex[-x,y], x_:>-x}. – Suba Thomas Oct 03 '19 at 14:21
  • Actually this would also break for "complex" number like e.g. 1.234+0I, which actually collapses to a Real – Ruslan Oct 03 '19 at 14:23
2
f[z_] = -Re[z] + I Im[z]
f[list]

(* {{0.282095 + 0.282095 I, 0.27254 + 0.291336 I, 0.262018 + 0.300835 I, 0.250437 + 0.310542 I}} *)
Michael Seifert
  • 15,208
  • 31
  • 68