1

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].

pisco
  • 229
  • 1
  • 6

1 Answers1

2
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 *)
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198