# Change all values of a series to positive values

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}


gives:

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.

-
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

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


Now:

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


Gives:

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


UPDATE

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

-
@ 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;
FromCoefficientRules[
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 *)

FromCoefficientRules[
MapAt[Abs,
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.

FullForm[expr1]
(* 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.

-