1
$\begingroup$

I have a large expression, for example

Cos[x]Sin[y]Sqrt[1+z]/(1+x^2)-1/(1+y)

I wish to extract the argument inside Sqrt function, namely 1+z. I thought of using /.Sqrt[x_]->(h=x), but executing

Cos[x]Sin[y]Sqrt[1+z]/(1+x^2)-1/(1+y)/.Sqrt[x_]->(h=x);h

returns x but not 1+z, why? Is there a way to achieve my goal?

In my expressions, there will only be one Sqrt, but many other different heads as well. There is a similar question, but it is only applicable to very simple expression like Sqrt[1+z].

$\endgroup$
  • 1
    $\begingroup$ You did not get what you expected because you used Rule(->) rather than RuleDelayed(:>) $\endgroup$ – Bob Hanlon Mar 7 at 20:23
  • $\begingroup$ @BobHanlon I see, thank you very much! $\endgroup$ – pisco Mar 10 at 10:39
2
$\begingroup$
Clear["Global`*"]

expr = Cos[x] Sin[y] Sqrt[1 + z]/(1 + x^2) - 1/(1 + y) Sqrt[1 - z];

Cases[expr, Sqrt[t_] :> t, Infinity]

(* {1 - z, 1 + z} *)

To ensure that all of the terms are real

And @@ Thread[% >= 0] // Simplify

(* -1 <= z <= 1 *)

Or more directly,

And @@ Cases[expr, Sqrt[t_] :> (t >= 0), Infinity] // Simplify

(* -1 <= z <= 1 *)

Better yet,

FunctionDomain[expr, z]

(* -1 <= z <= 1 *)
| improve this answer | |
$\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.