7
$\begingroup$

I am familiar with the use of Simplify, FullSimplify, Expand, Factor, Collect and Together for simplifying a long expression in Mathematica. But recently I have encountered an expression which Mathematica doesn't simplify even though it is completely clear to my eye that the expression can be written in a much shorter form.

This is the expression:

-2 Sqrt[1/(4 + (1. - 2. s + Sqrt[
  8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2/(2 d^2))^2] - 
  1/4 Sqrt[(4 d^2 + (1. - 2. s + Sqrt[
  8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2 - 
  4 d (1. - 2. s + Sqrt[
   8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2)^2/(
 d^4 (4 + (1. - 2. s + Sqrt[
   8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2/(2 d^2))^2)] + 
 1/4 Sqrt[(4 d^2 + (1. - 2. s + Sqrt[
  8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2 + 
  4 d (1. - 2. s + Sqrt[
   8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2)^2/(
  d^4 (4 + (1. - 2. s + Sqrt[
   8 d^2 + (1. - 3. s) (1. - 1. s) + s^2])^2/(2 d^2))^2)]

Question: How can we shorten this expression more and more with Mathematica functions. (All variables are reals)

enter image description here

$\endgroup$
3
  • $\begingroup$ You make your dissatisfaction clear, but what is your question? $\endgroup$
    – m_goldberg
    Commented Aug 21, 2016 at 5:41
  • 1
    $\begingroup$ Did you take in account that Mathematica considers all the variables in your expression complex quantities unless told otherwise? $\endgroup$
    – m_goldberg
    Commented Aug 21, 2016 at 5:44
  • $\begingroup$ So sorry I had to be offline unfortunately and have edited the question with delay. $\endgroup$ Commented Aug 21, 2016 at 9:10

3 Answers 3

10
$\begingroup$

A few things of note:

  1. Like @m_goldberg suggested, it is wise to explicitly state d ∈ Reals, s ∈ Reals.
  2. Because you introduced .'s after your numbers, you force mathematica to treat your integers as floats, which introduces rounding errors, removing these helps.
  3. Simplification introduces absolutes variable epxressions, taking these to be positive or negative helps.

Positive:

FullSimplify[expr, 
 Assumptions -> {d ∈ Reals, 
   s ∈ 
    Reals, -6 d^2 + 
     16 d^3 + (1 + Sqrt[8 d^2 + (1 - 2 s)^2] - 2 s) (-1 + 2 s) - 
     4 d (1 + Sqrt[8 d^2 + (1 - 2 s)^2] - 2 s) (-1 + 2 s) > 0}]

$$\frac{1+\sqrt{8d^2+\left(1-2s\right)^2}-2s}{2\sqrt{8d^2+\left(1-2s\right)^2}}$$

Negative:

FullSimplify[expr, 
 Assumptions -> {d ∈ Reals, 
   s ∈ 
    Reals, -6 d^2 + 
     16 d^3 + (1 + Sqrt[8 d^2 + (1 - 2 s)^2] - 2 s) (-1 + 2 s) - 
     4 d (1 + Sqrt[8 d^2 + (1 - 2 s)^2] - 2 s) (-1 + 2 s) < 0}]

$$\begin{cases} -1 & \left(1+4d\right)\left(1+\sqrt{8d^{2}+\left(1-2s\right)}-2s\right)\left(-1+2s\right)\ge2d^{2}\left(3+8d\right)\\ \frac{2d\left(d-sd^{2}+2\left(1+\sqrt{8d^{2}+\left(1-2s\right)^{2}}\right)\right)\left(-1+2s\right)}{-8d^{2}+\left(1+\sqrt{8d^{2}+\left(1-2s\right)^{2}}-2s\right)\left(-1+2s\right)} & Else \end{cases}$$

Yielding an in total 3 cases environment.

$\endgroup$
6
$\begingroup$

Following from your expression expr:

exact = Rationalize[expr]

vars = Variables[expr]

Refine[exact, Element[vars, Reals]]

Simplify[%, Element[vars, Reals]]

(* substitution picked by me and copy-pasted from the last output *)
% /. Sqrt[8 d^2 + (1 - 2 s)^2] -> x1

(* another such substitution *)    
% /. (1 - 2 s + x1) -> x2

enter image description here

$\endgroup$
5
$\begingroup$

Just one approach, and not necessarily the kind of result you want:

expr  (* your expression *)

FullSimplify[Rationalize[%], Variables[%] ∈ Reals];
out = Experimental`OptimizeExpression[%];

new = Symbol /@ CharacterRange[63396, 63421];
old = DeleteDuplicates@Cases[out, s_Symbol /; Context[s] === "Compile`", {-1}]

Extract[out, {1, 2}, Defer] /.
  Cases[Flatten[{old, new}, {2}], {o_, n_} :> (o :> n)]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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