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

enter image description here

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!

  • $\begingroup$ 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. $\endgroup$
    – Domen
    Apr 14, 2023 at 9:08
  • $\begingroup$ @Domen Thanks for your comment. pand np both are lists! Scalar product should give a scalar I think. $\endgroup$ Apr 14, 2023 at 9:10
  • $\begingroup$ 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. $\endgroup$
    – Domen
    Apr 14, 2023 at 9:13
  • $\begingroup$ 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 $\endgroup$
    – Domen
    Apr 14, 2023 at 9:26


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.