2
$\begingroup$

During some computation, I sometimes stumble upon expressions such as:

expr = 1.4`*^-16 Sqrt[-4.`*^31 x3^2 Cos[0.1` T]^2 Sin[1.` T]^2 + 
9.`*^31 x3 x4 Cos[0.6` T] Sin[0.6` T] Sin[1.6` T]^2]

This is something like $10^{-16}\sqrt{10^{32}A}$ and I hoped it would Simplify to $\sqrt{A}$.

Why isn't the $10^{-16}$ in front of the Sqrt passed into the Sqrt so that the powers of 10 disappear?

Additionally, it seems very dangerous since

Chop[expr]

return 0.!

Is there a simple way to force MMA simplify the expression?

$\endgroup$
  • $\begingroup$ It's a matter of accuracy I think. Simplify does work when using exact numbers: Simplify[(14*^-17) Sqrt[-4*^31 x3^2 Cos[ T/10]^2 Sin[ T]^2 + 9*^31 x3 x4 Cos[6 T/10] Sin[6 T/10] Sin[16 T/10]^2]] . $\endgroup$ – yohbs Mar 10 '17 at 19:43
  • $\begingroup$ "squaring and square rooting" is generally not valid. It assumes that the argument of Sqrt is nonnegative real for all values of its parameters. $\endgroup$ – Bob Hanlon Mar 10 '17 at 20:05
  • $\begingroup$ @BobHanlon Yes! That's why I want to avoid it!! (cf last sentence in OP). I thought square rooting the square would show that the powers of ten are unnecessary, but apparently it's confusing. I'll remove it in the OP. $\endgroup$ – anderstood Mar 10 '17 at 20:08
  • $\begingroup$ Maybe something like expr /. a_Real Sqrt[b_] :> Sign[a] Sqrt[Expand[a^2 b]] $\endgroup$ – Simon Woods Mar 10 '17 at 20:23
2
$\begingroup$

Sqrt[] automatically factors out a Real factor:

Sqrt[1.*^-3 (2. x - 3. y)]
(*  0.0316228 Sqrt[2. x - 3. y]  *)

So the following, when executed inside a Sqrt will do something like what the OP wants:

factorReal = Plus[x_, y__] :>
   With[{coeffs = Replace[{x, y}, Times[k_Real, __] :> k, 1]},
    With[{p = Round[Log2@GeometricMean@Abs@coeffs, 2]},
      2.^p*Distribute[2.^-p*Plus[x, y]]] /; MatchQ[coeffs, {__Real}]
    ];

It picks an even power of 2 to factor out, the power that centers the (geometric) mean of coefficients about 1, provided all coefficients are Real. Remove the condition and replace Replace[..] with Cases[{x, y}, (k_Real | Times[k_Real, __]) :> k, 1] for a less restrictive rule.

Examples

(-4.`*^31 x3^2 Cos[0.1` T]^2 Sin[1.` T]^2 + 
   9.`*^31 x3 x4 Cos[0.6` T] Sin[0.6` T] Sin[1.6` T]^2) /. factorReal
(*
  8.11296*10^31 (-0.493038 x3^2 Cos[0.1 T]^2 Sin[1. T]^2 + 
     1.10934 x3 x4 Cos[0.6 T] Sin[0.6 T] Sin[1.6 T]^2)
*)

expr /. factorReal
(*
  1.26101 Sqrt[-0.493038 x3^2 Cos[0.1 T]^2 Sin[1. T]^2 + 
      1.10934 x3 x4 Cos[0.6 T] Sin[0.6 T] Sin[1.6 T]^2]
*)

It can be used in with Simplify:

Simplify[expr + 2.^6 x + 2^-3 y,
 TransformationFunctions -> {Automatic, # /. factorReal &}
 ]
(*
  64. x + 0.125 y + 1.26101 Sqrt[x3 (-0.493038 x3 Cos[0.1 T]^2 Sin[1. T]^2 + 
       0.554668 x4 Sin[1.2 T] Sin[1.6 T]^2)]
*)

Simplify returns the "simplest" expression, which might not be the result of applying factorReal:

% /. factorReal
(*
  4. (16. x + 0.03125 y + 0.315252 Sqrt[
      x3 (-0.493038 x3 Cos[0.1 T]^2 Sin[1. T]^2 + 0.554668 x4 Sin[1.2 T] Sin[1.6 T]^2)])
*)
$\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.