# How do I format expressions so that replacement rules correctly apply to exponents? [duplicate]

This question already has an answer here:

I have a series of rules that I would like to apply to a series of expressions with variables, but cannot get Mathematica to correctly recognize that these rules should apply when my variables have exponents in them. For example, let's suppose I know that a b=1.

a b /. a b -> 1
(* 1 *)

a c b /. a b -> 1
(* c *)


So those work fine. Then I try:

a a b /. a b -> 1
(* a^2 b *)


In general, my rules are a good deal more complicated, but the problem is always the same. How do I get mathematica to recognize a^2 as a a so that it correctly applies the reduction rules?

## marked as duplicate by Mr.Wizard♦Aug 2 '14 at 23:55

• Have a look at PolynomialReduce. For complicated expressions that go beyond polynomials, maybe check this link – Daniel Lichtblau Aug 2 '14 at 21:02
• I have marked this question as a duplicate since I believe the general operation you are interested in is equivalent. Unfortunately there is not a good universal solution at this time, but see my own answer there for a collection of links to many related questions, including the one that Daniel links above. – Mr.Wizard Aug 2 '14 at 23:57

Keep the LHS of the replacement rule as simple as possible, e.g., use b -> 1/a

{a b, a b c, a a b} /. b -> 1/a


{1, c, a}

• How would you handle a more complex rule, for example, b^3->1 or a b->b? These rules come from (sometimes complex) equations. For example, I know that ab-b=0, but I have too many equations and variables for solving via cases (either b=0 or a=1) to be practical. I also have multiple equations that affect the same variable so it's often more practical to think of b as "b" instead of "a third root of 1," although it it always more practical to think of b^3 as 1. – Susan Aug 2 '14 at 20:29
• Simplify[a b^5, b^3 == 1] gives a b^2 – Bob Hanlon Aug 2 '14 at 20:47

Use HoldForm for this:

HoldForm[a a b] /. a b -> 1
(* a 1 *)

• Thank you. However, by the time that I am ready to apply replacement rules, Mathematica has already auto-formatted my expression. How do I get it back into the form a a b? – Susan Aug 2 '14 at 19:49
• @Susan Maybe you can use HoldForm somewhere earlier? I currently don't think of any way to convert a^2 to a a. – m0nhawk Aug 2 '14 at 20:13
• I am fairly confident that I cannot. I heavily rely on Mathematica to make simplifications first. For example, I usually need to get a common denominator, cancel, and then extract the numerator. I also evaluate some of my variables as I discover their values (although not all variables will have values). It doesn't seem like Mathematica should have such a hard time applying a rule for products to an expression that it knew was a product prior to autoformatting? – Susan Aug 2 '14 at 20:17