Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to change all values of a series to positive values.

I refer to my previous question, answered in the most part, very elegantly by RunnyKine. However, there remains a slight glitch that I can't seem to overcome:

expr1 = x + (2 x^2)/y - (3 x^3)/y^2;
expr1 /. {Times[-1, c_] :> Times[1, c], Rational[-1, d_] :> 
Rational[1, d], n_?Negative :> -1 n}


x + 2 x^2 y + 3 x^3 y^2

I can't seem to fathom where the missing '/' has gone from between the x & y values.

share|improve this question
Although there are already answers for your specific problem here, the question "How to change all values of a series to positive values" cannot be answered in general. Even if you assume that all your variables are positive, one can easily construct examples where all approaches fail. A simple pattern match will only help you in very simple situations. Consider the following example: exp = x + (2 x^2)/y - (3 x^3)/y^2 + (- Sin[z]^2 - Cos[z]^2)/(-z) and try FullSimplify /@ exp – halirutan Nov 7 '13 at 3:18
up vote 1 down vote accepted
expr1 = x + (2 x^2)/y - (3 x^3)/y^2;


expr1 /. {Times[n_?Negative, c_] :> Times[-1 n, c], Rational[-1, d_] :> Rational[1, d]}


x + (3 x^3)/y^2 + (2 x^2)/y


If there are standalone numbers in the series as in e.g.

expr2 = -2 + x + (2 x^2)/y - (3 x^3)/y^2;

Then the following more general solution should be used:

Replace[expr2 /. {Times[n_?Negative, c_] :> Times[-1 n, c], 
                         Rational[-1, d_] :> Rational[1, d]}, n_?Negative :> -1 n, {1}]

Which gives:

2 + x + (3 x^3)/y^2 + (2 x^2)/y
share|improve this answer
@ RunnyKine, Apologies & many thanks :-) – martin Nov 6 '13 at 19:22
@martin. No worries, hope this helped. – RunnyKine Nov 6 '13 at 19:22

Here's another way, if the ys all appear in the denominator. We convert the expression to a polynomial with y -> 1/u and use some of the polynomial functions.

expr1 = x + (2 x^2)/y - (3 x^3)/y^2;
 MapAt[Abs, CoefficientRules[expr1 /. y -> 1/u, {x, u}], {All, {-1}}],
 {x, 1/y}]

(* x + (3 x^3)/y^2 + (2 x^2)/y *)

Another example:

Normal@Series[-2 Exp[-x^2/y], {x, 0, 7}]
(* -2 + x^6/(3 y^3) - x^4/y^2 + (2 x^2)/y *)

       CoefficientRules[Normal@Series[-2 Exp[-x^2/y], {x, 0, 7}] /. y -> 1/u, {x, u}],
       {All, -1}],
 {x, 1/y}]

(* 2 + x^6/(3 y^3) + x^4/y^2 + (2 x^2)/y *)

To answer the implicit question about how to figure out what went wrong with the pattern replacement (the case of the disappearing /), it is a good idea to start by examining the FullForm of the expression.

(* Plus[x, Times[-3, Power[x, 3], Power[y, -2]], Times[2, Power[x, 2], Power[y, -1]]] *)

And if you have trouble seeing the problem, you can use some of the other pattern-matching functions to test your patterns. Here Cases can be helpful.

Cases[expr1, n_?Negative, Infinity]
(* {-3, -2, -1} *)

Now look where those negative numbers come from. One is a coefficient; the others are exponents from powers of y. All are multiplied by -1 by your replacement, which converts the negative powers of y to positive powers, in effect removing the / from each.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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