I'm trying to write a rule that acts on any number, lists, expression and so on to turn any negative numbers positive.
The simple r = n_ -> Abs[n] works perfectly well for numbers, lists and nested lists, but does not work expressions, such as Exp[-5], which is unaffected by my rule. I can't use the replace function, because I also require my rule to work for other Mathematica inputs, like numbers, lists and nested lists.
Can someone suggest why this does not work? Is it due to the rule not penetrating the expression? An edit to my rule would also be much appreciated.
Exp[-5].Traceshows you the sequence of evaluation steps:Trace[Exp[-5] /. r]. PerhapsExp[-5] /. n_?NumberQ :> Abs[n]is what you're after? – Michael E2 Jan 29 '16 at 13:27(-E)^(-5)to returnE^5, but we can't get(-2)^(-5)to return2^5? Changing the-2to-2.0doesn't change it either. – Jason B. Jan 29 '16 at 13:44Traceand the observation thatNumberQ[E]isFalseleads to an explanation. E.g.,(-2)^(-5)evaluates to-1/32before any rules are applied. See if you can figure outHoldForm[(-2)^(-5)] /. n_?NumberQ :> Abs[n] // Trace-- it stumped me at first.Inactivate[(-2)^(-5)] /. n_?NumberQ :> Abs[n]is the only way I figured out to do what you want. – Michael E2 Jan 29 '16 at 14:02Log[1/E], do you want to get 1 or -1? (I.e. do you wantAbs[ Log[ Abs[ Abs[1]/Abs[E]]]]or onlyLog[ Abs[1/E]], respectively?) – Eric Towers Jan 29 '16 at 17:44