9
$\begingroup$

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

$\endgroup$
  • 3
    $\begingroup$ Simplify[expr, Assumptions -> {x > 0}, ExcludedForms -> {_Log}] --- makes you wish Refine had the same option :) $\endgroup$ – kglr Aug 23 '14 at 14:55
  • $\begingroup$ ... maybe something like Refine[expr /. Log -> log, x > 0] /. log -> Log? $\endgroup$ – kglr Aug 23 '14 at 14:59
9
$\begingroup$

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] *)
$\endgroup$
6
$\begingroup$

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

EDIT

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.

$\endgroup$
  • 2
    $\begingroup$ You could also do expr = Inactivate[Sqrt[x^4] Log[x^2] + Log[x^4], Log], instead of pattern matching yourself. $\endgroup$ – Chip Hurst Aug 23 '14 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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