2
$\begingroup$

Say I have a list of lists:

{{Subscript[a,1],Subscript[a,1]+Subscript[a,2],Subscript[a,-3]},
 {Subscript[a,1],Subscript[a,-2],Subscript[a,-4]}}

Now, in the sublists of that list, if an element has a negative subscript I want to change this element to a negative of that element with a positive subscript, so if I applied this to the list above I'd get:

{{Subscript[a,1],Subscript[a,1]+Subscript[a,2],-Subscript[a,3]},
 {Subscript[a,1],-Subscript[a,2],-Subscript[a,4]}}

I don't really know how to do it. Any help would be much appreciated.

$\endgroup$
4
$\begingroup$

You can use pattern matching for this:

expr = {{Subscript[a, 1], Subscript[a, 1] + Subscript[a, 2],    Subscript[a, -3]}, {Subscript[a, 1], Subscript[a, -2], Subscript[a, -4]}}

expr /. Subscript[a, n_Integer /; n < 0] :> - Subscript[a, -n]
$\endgroup$
  • $\begingroup$ I was just looking at pattern matching :) Thank you very much @Fred Simons . $\endgroup$ – amator2357 Mar 14 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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