Understanding RegionNearest

As far as I understand RegionNearest-function the last command

p = {x1, y1};
np = RegionNearest[Disk[]][p]
abs=(np - p) . (np - p) (* scalar *)


gives a result

which is scalar!

But if I substitude point p by {x1->1/2,y1->1/3}

abs /.{x1->1/2,y1->1/3}
(*{1/36, 1/36}*)


I get a list of values?

What's wrong here? Thanks!

• Your abs is not a scalar, it has a list inside of it (and squaring a list gives a list of squares) ... I think the problem is in np - p not "combining" the components in List and Piecewise correctly. Apr 14, 2023 at 9:08
• @Domen Thanks for your comment. pand np both are lists! Scalar product should give a scalar I think. Apr 14, 2023 at 9:10
• Observe what happens in this simpler case: {x1, y1} + Piecewise[{{{x2, y2}, cond}}, {x3, y2}]. Piecewise gets distributed into each component! So if you then do % /. cond->True, you don't get one vector – as one might expect. Apr 14, 2023 at 9:13
• Relevant questions: Prevent Plus from threading, Sum of two Piecewise functions give wrong vector. One of the possible solutions: abs = (np~plus~-p) . (np~plus~-p); abs /. {x1 -> 1/2, y1 -> 1/3} /. plus -> Plus Apr 14, 2023 at 9:26