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.

I'm having problems using FullSimplify to simplify a differential equation. The expression is simplifying just as I would like when I express a variable c as a function, but not when I leave it by itself. It boils down to this:

FullSimplify[
  1 - (4 H n (c[x]^(-1 + 2 n)) )/(K^n + c[x]^n)^2 + (4 H n (c[x]^(-1 + n)))/(K^n + c[x]^n)]

Gives the expected output:

1 + (4 H K^n n c[x]^(-1 + n))/(K^n + c[x]^n)^2

However, when I replace c[x] with just c, the output changes completely and refuses to simplify. The output then is:

(c^(1 + 2 n) + c K^(2 n) + 2 c^n K^n (c + 2 H n))/(c (c^n + K^n)^2)

I don't understand why Mathematica is behaving inconsistently, or what I am doing wrong. I've tried adding assumptions about c, using things like Part, and searching the web to no avail.

Can anyone help?

share|improve this question
    
I don't quite understand your question. You got the "desired" output, so where's the problem? Also, c[x] and c are in no case equivalent. –  eldo May 25 at 11:49
    
I am simplifying the problem. In my case, I want Mathematica to simplify the latter input. All variables here are completely undefined, so why aren't c[x] and c rearranged in the same way? –  Myridium May 25 at 11:50

2 Answers 2

up vote 7 down vote accepted

FullSimplify and Simplify try to minimize a "cost" function called ComplexityFunction, but do so with different degree of persistence. In your expression, the default ComplexityFunction appears to guide the search for a minimum in the wrong direction.

The solution is to replace the default strategy with a different one.

First I'll define the two un-simplified expressions with c[x] and c, respectively:

a0 = 
 1 - (4 H n (c[x]^(-1 + 2 n)))/(K^n + 
      c[x]^n)^2 + (4 H n (c[x]^(-1 + n)))/(K^n + c[x]^n)

(*
==> 1 - (4 H n c[x]^(-1 + 2 n))/(K^n + c[x]^n)^2 + (
 4 H n c[x]^(-1 + n))/(K^n + c[x]^n)
*)

b1 = a0 /. c[x] -> c

(*
==> 1 - (4 c^(-1 + 2 n) H n)/(c^n + K^n)^2 + (
 4 c^(-1 + n) H n)/(c^n + K^n)
*)

Now define the default ComplexityFunction according to the documentation in the link above:

SimplifyCount[p_] :=
 Which[Head[p] === Symbol, 1,
  IntegerQ[p], 
  If[p == 0, 1, Floor[N[Log[2, Abs[p]]/Log[2, 10]]] + If[p > 0, 1, 2]],
  Head[p] === Rational, 
  SimplifyCount[Numerator[p]] + SimplifyCount[Denominator[p]] + 1,
  Head[p] === Complex, 
  SimplifyCount[Re[p]] + SimplifyCount[Im[p]] + 1, NumberQ[p], 2,
  True, SimplifyCount[Head[p]] + 
   If[Length[p] == 0, 0, Plus @@ (SimplifyCount /@ (List @@ p))]]

Next, test explicitly that it is this function that gives the differing results:

FullSimplify[a0, ComplexityFunction -> SimplifyCount]

(* ==> 1 + (4 H K^n n c[x]^(-1 + n))/(K^n + c[x]^n)^2 *)

FullSimplify[b1, ComplexityFunction -> SimplifyCount]

(*
==> (c^(1 + 2 n) + c K^(2 n) + 
 2 c^n K^n (c + 2 H n))/(c (c^n + K^n)^2)
*)

Now try the failed simplification again with a simpler (non-default) ComplexityFunction:

FullSimplify[b1, 
 ComplexityFunction -> LeafCount]

(* ==> 1 + (4 c^(-1 + n) H K^n n)/(c^n + K^n)^2 *)

This is the same form as above for the expression with c[x].

share|improve this answer
    
Brilliant ! - à propos Simplifying: "Der Weg des Geistes ist der Umweg." - Hegel –  eldo May 26 at 22:22
    
@eldo Thanks - I wonder what Hegel would say to this: "Was man nicht im Kopf hat, muss man in den Beinen haben." –  Jens May 26 at 22:39
    
Ich kam nur bis zur seiner "Schädellehre" - gibt es, vielleicht, noch andere Dinge darunter ? Würde mich wundern ... –  eldo May 26 at 22:50
    
Thanks! I'm not proficient enough in Mathematica to digest all the code of the ComplexityFunction, so would you maybe be able to pinpoint the line at which the discrepancy between $c$ and $c[x]$ occurs? –  Myridium May 27 at 12:43
    
@Myridium - I think your question got a +1 solution, because FullSimplify[a0 /. c[x] -> c, ComplexityFunction -> LeafCount] now gives you the desired result. So, why bother where the discrepancy occurED ? –  eldo May 27 at 16:08

This is not an answer, but to better understand the problem:

a0 = 1 - (4 H n (c[x]^(-1 + 2 n)))/(K^n + c[x]^n)^2 + (4 H n (c[x]^(-1 + n)))/(K^n + c[x]^n)

a1 = a0 // FullSimplify

a2 = a1 /. c[x] -> c

b1 = a0 /. c[x] -> c

b2 = b1 // FullSimplify

a2 == b2 (* ==> False *)

a2 == b2 // FullSimplify  (* ==> True *)

Strange indeed

share|improve this answer
    
Great example. This highlights the problem. –  Myridium May 26 at 7:33

Your Answer

 
discard

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.