3

I have a list that is of the following form

{-x,y,-z,a}

I want this set to become this:

{x,y,z,a}

I tried the following:

{-x,y,-z,a}/.{Minus->Plus}

But with no success. Functions like Abs also don't work, since x,y,z,a are not defined.

Jasper
  • 131
  • 4
  • 2
    Try the rule -1*x_ :> x – Marius Ladegård Meyer Mar 28 '16 at 15:17
  • Oh that works fine! Thank you! – Jasper Mar 28 '16 at 15:19
  • {-x, y, -z, a} /. -t_ :> t works too. – andre314 Mar 28 '16 at 15:21
  • 1
    If you want to see what are the real "words" behind the expression {-x,y,-z,a} try : FullForm[{-x,y,-z,a}], you will see there no Minus, so your replacement rule will have no effect. – andre314 Mar 28 '16 at 15:27
  • 1
    I didn't know about the FullForm expression yet. Thank you for that, that may create lots less struggle in the future with similar problems :) – Jasper Mar 28 '16 at 15:31
  • If you get the Head of first element: list={-x,y,-z,a}; Head[list[[1]]] you see that it is Times whereas the second element's head is Symbol so you can extract negative and positive elemtns separately and do whatever modification you need on them. For example: Join[-1*Cases[list,Except[_Symbol]],Cases[list,_Symbol]] – MathX Mar 28 '16 at 17:34

0 Answers0