Replacing all constant factors in expressions by +1

It seems that your question boils down to replacing all numerical multiplicative factors with 1, which is directly implemented as

exp = a - b + (f - I g)/h + c (d - e/2); 
exp //. {Times[pre___, n_?NumericQ, post___] :> Times[pre, post]}

(* a + b + c (d + e) + (f + g)/h *)

I've tested this on a number of different cases and it seems pretty robust.


Update: A variant of @Daniel W's idea:

times = Times @@ DeleteCases[{##}, _?NumericQ] &;
numbersToOne = # /. Times -> times &;

numbersToOne[a - b + c (d - 1/2 e) + (f - g I)/Sqrt[h]]

a + b + c (d + e) + (f + g)/h

numbersToOne[a - b + c (d Pi - 1/2 e) + (f E - g I)/Sqrt[h]]

a + b + c (d + e) + (f + g)/Sqrt[h]

Original answer:

exp = a - b + (f  - I g)/h + c (d - e/2);
replaceNumbers = # /.  Power[a_, b_?NumericQ] :> Power[a, ToString[b, StandardForm]] /. 
     x_?NumericQ :> 1 /. s_String :> ToExpression[s] &

replaceNumbers @ exp

a + b + c (d + e) + (f + g)/h

replaceNumbers @ (a - b + c (d Pi - 1/2 e) + (f E - g I)/Sqrt[h])

a + b + c (d + e) + (f + g)/Sqrt[h]


Another idea is to use Collect:

Collect[(a - b + c (d - 1/2 e) + (f - g I)/h), _Symbol, 1&]

a + b + c (d + e) + f/h + g/h

Tags:

Replacement