Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Mr.Wizard Aug 2 at 23:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Have a look at PolynomialReduce. For complicated expressions that go beyond polynomials, maybe check this link –  Daniel Lichtblau Aug 2 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 at 23:57

2 Answers 2

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}

share|improve this answer
    
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 at 20:29
    
Simplify[a b^5, b^3 == 1] gives a b^2 –  Bob Hanlon Aug 2 at 20:47

Use HoldForm for this:

HoldForm[a a b] /. a b -> 1
(* a 1 *)
share|improve this answer
    
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 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 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 at 20:17

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