2
$\begingroup$

I'm just starting to work with Mathematica and this is simplified version of the problem I have: seems that Sqrt[x]^2 simplifies to just x without any restrictions on domain:

In[1]:= FunctionDomain[Sqrt[x]^2, x]
Out[1]:= True

I can't restrict x>=0 globally because negative x is necessary in other parts of my calculation.

Is there a (preferably automatic) way to propagate domain during function composition?

So far I tried Sqrt, entering square root using Ctrl+2, and explicitly creating composition like this:

In[2]:= f[x_] := Sqrt[x]
        g[a_] := a^2
        z[p_] := Composition[g, f]
        FunctionDomain[z[x], x]
Out[2]:= True

enter image description here

$\endgroup$
  • 4
    $\begingroup$ Sqrt's domain is at least the entire real line because Mathematica automatically uses complex numbers as relevant. If you would like to ensure that the real roots are used, consider looking at Surd. $\endgroup$ – eyorble Dec 27 '19 at 1:04
  • $\begingroup$ For negative values: FullSimplify[ComplexExpand[Sqrt[x]], x < 0] gives I*(x^2)^(1/4), and FullSimplify[%^2, x < 0] is x, that corresponds to your plot. $\endgroup$ – Alx Dec 27 '19 at 2:29
  • 1
    $\begingroup$ @eyorble when I run FunctionDomain[Sqrt[x]] it produces {x>=0} $\endgroup$ – fukanchik Dec 27 '19 at 3:17
  • $\begingroup$ @Alx Sqrt is not the best example. Consider (x+2)*(x+1)/(x+2) $\endgroup$ – fukanchik Dec 27 '19 at 3:19
6
$\begingroup$

The problem is that Sqrt[x]^2 evaluates to x:

Sqrt[x]^2

x

Hence, your call to FunctionDomain checks the domain of x. You can use Unevaluated to prevent this:

FunctionDomain[Unevaluated[Sqrt[x]^2], x]

x >= 0

$\endgroup$
  • $\begingroup$ Thanks Carl that is what I was looking for. $\endgroup$ – fukanchik Dec 27 '19 at 3:23

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.