9
$\begingroup$

I've been using Wolfram Engine 14.3 for a personal project, and a common problem I've encountered is that its outputs — even after using Simplify[], FullSimplify[], user-defined simplification functions like SuperFullSimplify[], or the suggestions from "Advice for Mathematica as Mathematician's Aid" — are extremely long, but can be manually simplified down to equivalent expressions that are a tiny fraction of their initial size.

As an example, one of the outputs I got recently was the 592-character, 328-leaf expression

-1/2*((1 + 1/Sqrt[2])*(99 - 70*Sqrt[2])*r*(6 + 4*Sqrt[2] + U^2)^2*
(7 + 4*Sqrt[2] + U^2)^2*(8*(3 - 2*Sqrt[2])*U^4 + 32*(3 + 2*Sqrt[2] + U^2) + 
4*c*(-4*(10 + 7*Sqrt[2]) - 8*(2 + Sqrt[2])*U^2 + 5*(-2 + Sqrt[2])*U^4 + 
(-10 + 7*Sqrt[2])*U^6) + c^2*(68 + 48*Sqrt[2] + 12*(3 + 2*Sqrt[2])*U^2 + 
13*U^4 + 6*(3 - 2*Sqrt[2])*U^6 + (17 - 12*Sqrt[2])*U^8)))/
((-2*(7 + 4*Sqrt[2]) + (-7 + 2*Sqrt[2])*U^2 + (-3 + 2*Sqrt[2])*U^4)^2*
(8*(2 + Sqrt[2]) - 4*(3 + 2*Sqrt[2])*c - 8*(-2 + Sqrt[2] + c)*U^2 + 
(20 - 14*Sqrt[2] + 5*(-3 + 2*Sqrt[2])*c)*U^4 + (-17 + 12*Sqrt[2])*c*U^6)*
Sqrt[1 + (3/2 - Sqrt[2])*(1 + U^2)])

which (with a lot of effort) can be manually simplified down to the 78-character, 45-leaf expression

r*(c*((2 - Sqrt[2])*(U^2 - 1) + 4) - 4)/Sqrt[8*(3 - 2*Sqrt[2])*(1 + U^2) + 16]

for an over 86.8% reduction in length and an almost 86.3% reduction in leaf count. Wolfram Engine will almost-instantly confirm that those two expressions are equal, but I haven't been able to find a way to make the program produce the second expression (or anything even close to it in size) from the first �� in fact, several of the results from my customized ComplexityFunction setups were significantly longer than the first expression, while most simply returned the input unchanged.

Are there any tricks that will make Wolfram Engine reliably find such drastically-shorter equivalent forms when dealing with expressions like this?

$\endgroup$

3 Answers 3

9
$\begingroup$

Since Factor with extensions works only with polynomials we have to separate terms that are polynomials from those that are not. Then Factor[..., Extension -> Sqrt[2]] is used on polynomials in hope for cancellations of some factors common to both numerator and denominator. Extension Sqrt[2] was used because that surd appears in the expression.

(Take definition of expr from OP)

GatherBy[FactorList@expr, PolynomialQ[#[[1]], Variables[expr]] &];

Factor[Times @@ Power @@@ %[[1]], 
  Extension -> Sqrt[2]]*(Times @@ Power @@@ %[[2]])

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Nice & elegant, +1 $\endgroup$ Commented Jan 28 at 21:14
7
$\begingroup$
factorLeading = ReplaceAll[
   p_?PolynomialQ /; Exponent[p, U] > 0 :>
    With[{k = CoefficientList[p, {c, r, U}][[-1, -1, -1]]},
     k Factor[FullSimplify[p/k], Extension -> Sqrt[2]]]
   ];

FullSimplify[expr,
 TransformationFunctions -> {Automatic, factorLeading}]
(*
(r (-2 (2 + Sqrt[2]) + c (3 + 2 Sqrt[2] + U^2))) /
 (2 Sqrt[7 + 4 Sqrt[2] + U^2])
*)

% // LeafCount
(*  45  *)
$\endgroup$
6
$\begingroup$

Instead of forcing Mathematica to arrive at the desired form, we can analyse the expression. Let us first get rid of the square root, which clearly cannot be simplified:

sqrt = Sqrt[1 + (3/2 - Sqrt[2]) (1 + U^2)]

Then we look at the remaining part:

expr2 = expr*sqrt /. {U -> Sqrt[x]};
FunctionPoles[expr2, x]
 (* {} *)

Thus, expr2 is a polynomial, which can be simplified with methods for polynomial functions:

expr3 = PolynomialQuotient[Numerator[expr2], Denominator[expr2], x] //
   FullSimplify
(* 1/4 r (-4 + c (2 + Sqrt[2] - (-2 + Sqrt[2]) x)) *)

And the final simplified expression recovered as

exprSimple = (expr3 /. {x -> U^2})/sqrt

$$\frac{r \left(c \left(-\left(\left(\sqrt{2}-2\right) U^2\right)+\sqrt{2}+2\right)-4\right)}{4 \sqrt{\left(\frac{3}{2}-\sqrt{2}\right) \left(U^2+1\right)+1}}$$

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.