Often times functions might return Undefined in Mathematica, e.g. when the Volume@RegionIntersection is called for 2 objects which have no overlapping volume. A simple If statement checking if the return has been indeed Undefined does not seem to work. For example, a=Undefined; If[a == Undefined, Print["yes"]] ends up printing the command itself.

  • How can we check if a variable (or a function return) such as a has been assigned as Undefined and assign a different value to it in that case?
  • 1
    $\begingroup$ Can you show a simple version of your function that returns Undefined? $\endgroup$ – bill s Nov 28 '19 at 18:30
  • $\begingroup$ You might want to use RegionDisjoint on the two regions before trying to compute the intersection. $\endgroup$ – Carl Woll Nov 28 '19 at 19:03
  • $\begingroup$ @CarlWoll Thanks, indeed I should be checking for that in the first place, but to save computational time (as my current intersection detection routine is already very slow, this is about the post I mentioned to you earlier), I skipped checking for disjointness beforehand. $\endgroup$ – user929304 Nov 28 '19 at 19:27
  • $\begingroup$ @bills MassDefect have provided one such example in their answer below. $\endgroup$ – user929304 Nov 28 '19 at 19:28

You should be able to use SameQ (===).

Volume[RegionIntersection[Circle[{0, 0}, 1], Circle[{2, 2}, 1]]] === Undefined
(* True *)
|improve this answer|||||

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.