5

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?

Myridium
  • 1,089
  • 5
  • 15
  • 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 '14 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 '14 at 11:50

2 Answers2

7

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].

Jens
  • 97,245
  • 7
  • 213
  • 499
  • Brilliant ! - à propos Simplifying: "Der Weg des Geistes ist der Umweg." - Hegel – eldo May 26 '14 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 '14 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 '14 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 '14 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 '14 at 16:08
  • @Myridium I can't say just by looking at the default code why it goes wrong - the answer to that will by highly dependent on the particular input, and therefore one may not learn much from it, in terms of avoiding similar problems with different input. – Jens May 27 '14 at 17:00
  • I'm just as interested in the journey as I am in the destination. @Jens Okay, I won't worry myself with it any more. – Myridium May 27 '14 at 17:36
1

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

eldo
  • 67,911
  • 5
  • 60
  • 168