Great Answers to
Questions About Everything


This seems like a simple thing to do, but I couldn't find anything relevant from Mathematica documentation.

So suppose I have an expression:

a*b/(a + a*Cos[a/b])

And I have defined:


Now I want to simplify the expression above so that the simplify would use my definition of k in place of a/b in as many places as possible so that the final expression would look something like:


This was just a simple example I made up to demonstrate what I'd like to do, but I have encountered a similar situations many times every now and then.

{ asked by Echows }


Daniel Lichtblau and Andrzej Koslowski posted a solution in mathgroup, which I adjusted marginally. (I like to use german identifiers, because they will never clash with Mma builtins). That's the code:

termErsetzung[expr_, rep_, vars_] := 
Module[{num = Numerator[expr], den = Denominator[expr],
        hed = Head[expr], base, expon},
  If[PolynomialQ[num, vars] && PolynomialQ[den, vars] && ! NumberQ[den], 
    termErsetzung[num, rep, vars]/termErsetzung[den, rep, vars], (*else*)
    If[hed === Power && Length[expr] === 2,        
       base  = termErsetzung[expr[[1]], rep, vars];
       expon = termErsetzung[expr[[2]], rep, vars];
       PolynomialReduce[base^expon, rep, vars][[2]],        (*else*)
      If[Head[Evaluate[hed]] === Symbol && 
        MemberQ[Attributes[Evaluate[hed]], NumericFunction], 
        Map[termErsetzung[#, rep, vars] &, expr],    (*else*)
       PolynomialReduce[expr, rep, vars][[2]] ]]]


Usage is like this:

a*b/(a + a*Cos[a/b]) // TermErsetzung[k b == a, b]

a/(k (1 + Cos[k]))

The first parameter is the "replacement equation", the second the variable (or list of variables) to be eliminated:

a*b/(a + a*Cos[a/b]) // TermErsetzung[k b == a, {a, b}] 

{b/(1 + Cos[k]), a/(k (1 + Cos[k]))}

{ answered by Peter Breitfeld }