# Simplifying expressions with variables that can only take specific values

I am having some trouble simplifying expressions where a variable can only take a number of specific values. As an example, suppose I have the expression:

$$\lambda(m) = \frac{1+m}{2} \frac{1}{(a+b)} + \frac{1-m}{2} \frac{1}{(a - b)}$$,

this can be simplified to

$$\lambda(m) = \frac{1}{(a+mb)}$$.

In Mathematica, using the following code:

λ = (1 + m)/2 1/(a + b) + (1 - m)/2 1/(a - b)

FullSimplify[λ, { m == +1 || m == -1}]


the output is:

(a - b m)/(a^2 - b^2)


This expression is correct and does simplify further if a specific value of $$m$$ is given:

(a - b m)/(a^2 - b^2) /. {m -> +1} // FullSimplify
(a - b m)/(a^2 - b^2) /. {m -> -1} // FullSimplify


Output:

1/(a + b)
1/(a - b)


However, it is not in the fully simplified form given above. I would like to apply a similar procedure to more complex expressions, where it is not so obvious what the answer should be in advance, but I think in these cases I am also getting answers that are not fully simplified.

Any ideas why Mathematica can't simplify the expression in the example?

A more complicated example where I also can work out the simplified version:

\[Lambda]1 = (Axx + Ayy)^2/(
Azz + 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]) + (
4 Axy^2 + (Axx - Ayy)^2)/(
Azz + 2 Bz (\[Gamma]c + \[Gamma]e) + 2 \[CapitalDelta]);

\[Lambda]2 = -((4 Axy^2 + (Axx - Ayy)^2)/(
Azz - 2 Bz (\[Gamma]c + \[Gamma]e) + 2 \[CapitalDelta])) - (Axx +
Ayy)^2/(Azz - 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]);

\[Lambda] = (1 + m)/2 \[Lambda]1 + (1 - m)/2 \[Lambda]2;

FullSimplify[\[Lambda], {m == +1 || m == -1}]


output:

1/2 (((Axx + Ayy)^2 (1 + m))/(
Azz + 2 Bz (\[Gamma]c - \[Gamma]e) -
2 \[CapitalDelta]) + ((4 Axy^2 + (Axx - Ayy)^2) (-1 + m))/(
Azz - 2 Bz (\[Gamma]c + \[Gamma]e) +
2 \[CapitalDelta]) + ((Axx + Ayy)^2 (-1 + m))/(
Azz - 2 (Bz (\[Gamma]c - \[Gamma]e) + \[CapitalDelta])) + ((4 \
Axy^2 + (Axx - Ayy)^2) (1 + m))/(
Azz + 2 (Bz (\[Gamma]c + \[Gamma]e) + \[CapitalDelta])))


Simplified version:

\[Lambda]simplified =
m (Axx + Ayy)^2/(
Azz + m 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]) +
m (4 Axy^2 + (Axx - Ayy)^2)/(
Azz + m 2 Bz (\[Gamma]c + \[Gamma]e) + 2 \[CapitalDelta]);

(\[Lambda]simplified == \[Lambda]) /. {m -> 1} // FullSimplify

(\[Lambda]simplified == \[Lambda]) /. {m -> -1} // FullSimplify


output:

True
True


A more complicated example where I don't know the solution:

\[Lambda]1 = (Axx + Ayy)^2/(
Azz + 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]) + (
4 Axy^2 + (Axx - Ayy)^2)/(
Azz + 2 Bz (\[Gamma]c + \[Gamma]e) + 2 \[CapitalDelta]);

\[Lambda]2 = -((4 Axy^2 + (Axx - Ayy)^2)/(
Azz - 2 Bz (\[Gamma]c + \[Gamma]e) + 2 \[CapitalDelta])) - (Axx +
Ayy)^2/(Azz - 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]);

\[Lambda]3 = (Axx + Ayy)^2/(
Azz + 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]) - (
4 Axy^2 + (Axx - Ayy)^2)/(
Azz - 2 Bz (\[Gamma]c + \[Gamma]e) +
2 \[CapitalDelta]) - (Axx + Ayy)^2/(
Azz - 2 Bz (\[Gamma]c - \[Gamma]e) - 2 \[CapitalDelta]) + (
4 Axy^2 + (Axx - Ayy)^2)/(
Azz + 2 Bz (\[Gamma]c + \[Gamma]e) + 2 \[CapitalDelta]);

\[Lambda] =
m (1 + m)/2 \[Lambda]1 +
m (1 - m)/2 \[Lambda]2 + (1 - m) (1 + m) \[Lambda]3;

FullSimplify[\[Lambda], {m == +1 || m == -1 || m == 0}]


output:

1/2 (-(((Axx + Ayy)^2 (-2 + m) (1 + m))/(
Azz + 2 Bz (\[Gamma]c - \[Gamma]e) -
2 \[CapitalDelta])) + ((4 Axy^2 + (Axx - Ayy)^2) (-1 + m) (2 +
3 m))/(Azz - 2 Bz (\[Gamma]c + \[Gamma]e) +
2 \[CapitalDelta]) + ((Axx + Ayy)^2 (-1 + m) (2 + 3 m))/(
Azz - 2 (Bz (\[Gamma]c - \[Gamma]e) + \[CapitalDelta])) - ((4 \
Axy^2 + (Axx - Ayy)^2) (-2 + m) (1 + m))/(
Azz + 2 (Bz (\[Gamma]c + \[Gamma]e) + \[CapitalDelta])))


Using FullSimplify[\[Lambda]] gives the same result, indicating that the specific values for $$m$$ (-1,0,+1) are not used in the simplification.

• I argue that is impossible. In both cases the MA result is correct. Based on which criteria do you want to steer MA to a different form? Maybe provide a more complicated example that you do not know the answer in advance. Nov 20, 2019 at 10:59
• Hi yarchik, I agree that the result given is correct, but if $m$ can only take the values +1 or -1, the solution $\lambda = 1/(a + m b)$ seems like a simplified version to me. Why do you think that it would be impossible for Mathematica to simplify to this form?
– Joe
Nov 20, 2019 at 14:06
• In order to explain my point I need to see your more complicated example. Nov 20, 2019 at 14:30
• OK I've added two more examples - one where I can also work out the answer and one where it is not so obvious
– Joe
Nov 20, 2019 at 16:22
• Basically, my argument is that if we do not know the way to simplify these expressions by hands, there is no way for MA. It is not a magic tool. But I see also a problem in the formulation. Initially $m=\pm1$, now $m$ can also take the value $m=0$. Once we manage to solve this problem, one can find an example that does not work for 4 values, and so on. Nov 21, 2019 at 7:00

Try this:

λ1 = (1 + m)/2 (a + b) + (1 - m)/2 (a - b);
λ2 = (1 + m)/2 1/(a + b) + (1 - m)/2 1/(a - b);


Now let us transform:

λ1 // Expand

(*  a + b m  *)

λ2 // Together // ExpandDenominator

(*  (a - b m)/(a^2 - b^2)  *)


This requires no special trick to cope with m=+/-1.

Have fun!

• Hi Alexei - sorry I realised that the first example I gave, the simplified solution was valid for all m, not just $m \in {+1,-1}$. In the second example this is not the case - the solution returned is valid for all m, but a simpler solution is possible if m can only take the values +1 or -1 (given above). I have edited the original post to clarify,
– Joe
Nov 20, 2019 at 14:03
• I think here you need to help Mma. Say, if you instead of (a - b m)/(a^2 - b^2)  write the identical one (a - b m)/(a^2 - m^2*b^2) your expression immediately simplifies to what you want. However, I am not aware of the way to force Mma to automate it. Nov 20, 2019 at 15:28
• Yes, I think this works as these expressions are only identical if $m = \pm 1$, but the second expression simplifies to the desired form for all $m$. Unfortunately for the more complicated examples it is not so clear how to make a similar step..
– Joe
Nov 20, 2019 at 16:32
• Yes, it is precisely the point in which science transforms into art. Nov 21, 2019 at 10:29

At least for this simple example, using Solve seems to get close:

Solve[z == λ && m^2==1 && m ∈ Integers, z]


{{z -> ConditionalExpression[1/(a - b), m == -1]}, {z -> ConditionalExpression[1/(a + b), m == 1]}}

• Thanks Carl - that is closer, but I actually am starting here (I have multiple expressions for different values of m). I then write it as a single expression with prefactors $(1\pm m)/2$, and my hope is that similar terms can be combined to make a simplified single expression.
– Joe
Nov 20, 2019 at 16:29