6
$\begingroup$

I have a complicated equation, with a parameter T which is positive. The parameter ends up multiplying all terms of my equation, so Mathematica factors it out the front. But for some reason Simplify (or FullSimplify) refuses to factor it out, unless the rest of the equation is simple.

Simplify[T (b +  e (-1 + c) (1 + Tanh[a])) == 0, T > 0]

(* T (b + (-1 + c) e (1 + Tanh[a])) == 0 *)

If I make the equation slightly simpler, Mathematica has no problem with removing the T:

Simplify[T (b +  e (c) (1 + Tanh[a])) == 0, T > 0]

(* b + c e + c e Tanh[a] == 0 *)

My actual equation is much longer, but I want to be able to check that T does not exist in it.

$\endgroup$
  • $\begingroup$ How simple an expression is depends on how you quantify simplicity. If you provide Simplify with a different complexity function (e.g. ComplexityFunction -> (StringLength[ToString[#1]] &), you can get the T factored out, at the expense of some other reorganization of your equation. See also (92686). $\endgroup$ – MarcoB Jun 26 '17 at 14:12
  • $\begingroup$ It is not clear, what do you want. Mathematica does factor it out in your first expression. You seem to mean that Mma does not automatically divide by the both equation parts by it, do you? You have written that your equation always has T as a factor. If I understood this right, there is a simple way to get rid of it: equation/.T->1. Have fun! $\endgroup$ – Alexei Boulbitch Jun 26 '17 at 14:15
  • $\begingroup$ It might be helpful to try Reduce to see possible solutions. If T==0 is one of them, it suggests (but does not guarantee) that you can factor it out. Reduce[tt (b + e (c - 1) (1 + Tanh[a])) == 0, tt] = ` (* b == -(-1 + c) e (1 + Tanh[a]) || tt == 0 *)`. $\endgroup$ – MikeY Jun 26 '17 at 14:15
  • $\begingroup$ @AlexeiBoulbitch Mathematica factors $T$ out yes, but I'm telling it that $T>0$ in the assumptions, so it should be able to remove it entirely. $\endgroup$ – KraZug Jun 26 '17 at 14:56
  • 1
    $\begingroup$ @MarcoB, for any complexity function, removing the T entirely would make it simpler. $\endgroup$ – KraZug Jun 26 '17 at 14:57
2
$\begingroup$

This is an interesting case. First, here is a more minimal example. While Simplify does not eliminate T:

Simplify[(1 + 2 a^2 (a + b)) T == 0, T != 0]
(* (1 + 2 a^2 (a + b)) T == 0 *)

FullSimplify does the job in this case:

FullSimplify[(1 + 2 a^2 (a + b)) T == 0, T != 0]
(* 1 + 2 a^2 (a + b) == 0 *)

Simplify does work for much more complicated cases such as

Simplify[(1 + 2 a^2 (a + b) + x^1/3 - Coth[Gamma[42^z] I]) T == 0, T != 0]
(* 3 + 6 a^3 + 6 a^2 b + x + 3 I Cot[Gamma[42^z]] == 0 *)

So the complexity by itself is not the issue.

Now, to a possible solution to your problem. In the MMA documentation for ComplexityFunction the "Properties & Relations" section contains the complexity function Simplify uses by default to assess the complexity of a term. You may modify this function such that any appearance of T is extremely expensive:

SimplifyCountX[p_] := Which[
    p===T, 10^6,
    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, SimplifyCountX[Numerator[p]]+SimplifyCountX[Denominator[p]]+1,
    Head[p]===Complex, SimplifyCountX[Re[p]]+SimplifyCountX[Im[p]]+1,
    NumberQ[p], 2,
    True, SimplifyCountX[Head[p]]+If[Length[p]==0,0,Plus@@(SimplifyCountX/@(List@@p))]
]

The first case within Which[...] is the modification. Everything else is like in the documentation.

Using this complexity function things should at least improve for you:

Simplify[(1 + 2 a^2 (a + b)) T == 0, T != 0, ComplexityFunction -> SimplifyCountX]
(* 1 + 2 a^3 + 2 a^2 b == 0 *)

I also tested this with more complicated functions, and it seems to work.

To make sure if it worked, use

FreeQ[..., T]

on the simplified expression.

$\endgroup$
1
$\begingroup$

"I want to be able to check that T does not exist in it."

I assume you mean you want to check that you can factor T out. So, expanding on @AlexeiBoulbitch's suggestion, remove it, multiply by it, and check for equality.

expression = T (b + e (-1 + c) (1 + Tanh[a]))
Simplify[T (expression /. T -> 1) == expression]
(* True *)
$\endgroup$
  • $\begingroup$ But this doesn't work if the factor is $T^2$ for example. $\endgroup$ – KraZug Jun 27 '17 at 8:26
  • 1
    $\begingroup$ It'm still not entirely clear on what you want, but MatchQ[Cancel[expression/(expression /. T -> 1)], Power[T, _]] will check to see if you can factor any power of T out. $\endgroup$ – John Doty Jun 27 '17 at 14:04
  • $\begingroup$ To be honest, I mostly just want to understand why Mathematica doesn't remove it in this case, when it can. $\endgroup$ – KraZug Jun 27 '17 at 14:06
  • $\begingroup$ Simplify doesn't exhaustively try every available transformation. It has no reliable way to to tell it's "done". It makes decisions based on the informed guesses its developers built into it. There's no way for the user to understand why it didn't choose a particular transformation trajectory, and it would probably not be illuminating if you could. $\endgroup$ – John Doty Jun 27 '17 at 14:38
1
$\begingroup$
Simplify[Expand[T (b + e (-1 + c) (1 + Tanh[a]))] == 0, T > 0]
(* b + c e + c e Tanh[a] == e (1 + Tanh[a]) *)

eliminates T. Perhaps, this works because Expand makes the LeafCount of the equation larger, giving Simplify more options to try things. (Not a very satisfying explanation, I admit. Nonetheless, I have seen Expand before Simplify help in the other situations.) To gather all terms to the left side of the equation again, use

Simplify[# - Last[%]] & /@ %
(* b + (-1 + c) e + (-1 + c) e Tanh[a] == 0 *)

Addendum

Motivated by the resourceful (+1) answer of JEM_Mosig, I offer the simpler alternative

cf[e_] := LeafCount[e] + 100 Count[e, T, Infinity]
Simplify[T (b + e (-1 + c) (1 + Tanh[a])) == 0, T > 0, ComplexityFunction -> cf]
(* b + (-1 + c) e + (-1 + c) e Tanh[a] == 0 *)

Note that this answer is taken almost directly from Mathematica documentation. Once again, it appears that Simplify works better when the achievable savings in ComplexityFunction is large.

$\endgroup$

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.