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?

  • $\begingroup$ Have a look at PolynomialReduce. For complicated expressions that go beyond polynomials, maybe check this link $\endgroup$ – Daniel Lichtblau Aug 2 '14 at 21:02
  • $\begingroup$ 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. $\endgroup$ – 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}

  • $\begingroup$ 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. $\endgroup$ – Susan Aug 2 '14 at 20:29
  • $\begingroup$ Simplify[a b^5, b^3 == 1] gives a b^2 $\endgroup$ – Bob Hanlon Aug 2 '14 at 20:47

Use HoldForm for this:

HoldForm[a a b] /. a b -> 1
(* a 1 *)
  • $\begingroup$ 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? $\endgroup$ – Susan Aug 2 '14 at 19:49
  • $\begingroup$ @Susan Maybe you can use HoldForm somewhere earlier? I currently don't think of any way to convert a^2 to a a. $\endgroup$ – m0nhawk Aug 2 '14 at 20:13
  • $\begingroup$ 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? $\endgroup$ – Susan Aug 2 '14 at 20:17

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