Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to run Simplify and/or Refine on various expressions, but keep it from touching anything that contains a specific Head.

For example, I would like to Refine the following expression under the assumption x>0 but not do anything with any part of the expression whose Head is Log:

expr = Sqrt[x^4] Log[x^2] + Log[x^4];
Refine[expr, x>0]

Instead of

(* 4 Log[x] + 2 x^2 Log[x] *)

I would like to see

(* x^2 Log[x^2] + Log[x^4] *)

Where the Logarithms remain unsimplified

share|improve this question
Simplify[expr, Assumptions -> {x > 0}, ExcludedForms -> {_Log}] --- makes you wish Refine had the same option :) – kglr Aug 23 '14 at 14:55
... maybe something like Refine[expr /. Log -> log, x > 0] /. log -> Log? – kglr Aug 23 '14 at 14:59
up vote 9 down vote accepted

For Simplify there is the option ExcludedForms:

expr = Sqrt[x^4] Log[x^2] + Log[x^4];
Simplify[expr, Assumptions -> {x > 0}, ExcludedForms -> {_Log}]
(* x^2 Log[x^2] + Log[x^4] *)

For Refine, you can wrap the heads to be excluded with Hold:

Refine[expr /. Log -> Hold[Log], x > 0] // ReleaseHold
(*  x^2 Log[x^2] + Log[x^4] *)

or use ReplaceAll twice

Refine[expr /. Log -> "log", x > 0] /. "log" -> Log
(*  x^2 Log[x^2] + Log[x^4] *)
share|improve this answer

With V10 we can write

expr = Sqrt[x^4] Log[x^2] + Log[x^4] /. x_Log :> Inactivate[x];

Refine[expr, x > 0] // Activate

enter image description here


Thanks to Chip Hurst's comment the above should, of course, be written as

expr = Inactivate[Sqrt[x^4] Log[x^2] + Log[x^4], Log]

One of the advantages of Inactivate is that we can selectively Activate:

expr = Inactivate[Sqrt[x^4] Log[x^2] + Log[x^4] + Sin[Pi/2], Log | Sin];

Activate[Refine[expr, x > 0], Log]

enter image description here

Activate[%, Sin]

enter image description here

Another advantage is that - different from kguler's nice answer - the above examples don't have to distinguish Simplify from Reduce.

share|improve this answer
You could also do expr = Inactivate[Sqrt[x^4] Log[x^2] + Log[x^4], Log], instead of pattern matching yourself. – Chip Hurst Aug 23 '14 at 16:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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