10
$\begingroup$

Bug introduced in 12.3, and persisting through 13.2.1 or later.


Is this a bug? The region and polygon don't match. How can I fix this?
I would prefer to continue using "Implicit", as it is related to one of my previous threads here.

polygon = Polygon@{{0, 1}, {1, 1}, {5, 5}, {5, 0}, {0, 0}};
region = RegionConvert[polygon, "Implicit"];
{Graphics@polygon,  Region@region}

"13.0.1 for Microsoft Windows (64-bit) (January 28, 2022)"

enter image description here

EDIT:

I don't think it's related to plot quality, as this result of the code indicates that the intersection between the polygon and the line is a line, but that is incorrect.
If the region were represented correctly, the result of this RegionIntersection[region, ImplicitRegion[y == 3 - 2 x && x <= 1, {x, y}]] should be a point or RegionDimension of 0.
Hope that I'm wrong and it's not a bug so we can solve this problem.

polygon = Polygon@{{0, 1}, {1, 1}, {5, 5}, {5, 0}, {0, 0}}; 
region = RegionConvert[polygon,      "Implicit"];
RegionDimension@  RegionIntersection[region,    ImplicitRegion[y == 3 - 2 x && x <= 1, {x, y}]]

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ may be it is a bug. But "Mesh" does not have this issue. Screen shot !Mathematica graphics $\endgroup$
    – Nasser
    Commented Mar 2, 2023 at 14:18
  • $\begingroup$ The RegionDimension issue appeared in v12.3. In v12.2 this worked correctly if one substituted RegionConvert with equivalent function or copied the result from a newer version. $\endgroup$
    – kirma
    Commented Mar 4, 2023 at 6:51

5 Answers 5

5
$\begingroup$

The situation is the same even in 13.2.0.

One way to fix this if we want to keep "Implicit" is the following:

polygon = Polygon@{{0, 1}, {1, 1}, {5, 5}, {5, 0}, {0, 0}};
region = RegionConvert[polygon, "Implicit"];
{Graphics@polygon, Region@CanonicalizePolygon[region]}

res

$\endgroup$
5
  • $\begingroup$ +1 I am using a region for this purpose , but now I am concerned that using Mesh together with DiscretizeRegion might reduce the accuracy of my calculations. $\endgroup$
    – hana
    Commented Mar 2, 2023 at 14:47
  • $\begingroup$ It does not seem to work with every polygon. This gives me an error. Region@CanonicalizePolygon@ RegionConvert[ Polygon@{{0, 1}, {1, 1}, {5, 5}, {5, 0}, {-5, 0}, {-5, 5}}, "Implicit"] $\endgroup$
    – hana
    Commented Mar 2, 2023 at 14:57
  • 1
    $\begingroup$ @hana After some tests, I think it has to do with this point {-5, 5}. Not sure why it does not like it. But I agree it's weird. I have not found a work-around for this example while keeping "Implicit" $\endgroup$
    – bmf
    Commented Mar 3, 2023 at 1:04
  • $\begingroup$ @bmf The problem can be simplified down to RegionDimension[ImplicitRegion[x >= 0 && x == y && x + y <= 0, {x, y}]] returning 1 although the region consists of a single point at the origin. I'll update the bug report I've submitted to WRI on the subject. $\endgroup$
    – kirma
    Commented Mar 4, 2023 at 6:22
  • $\begingroup$ ... and even this is wrong: RegionDimension[ImplicitRegion[x >= 0 && x <= 0 && y == 0, {x, y}]] (1). Also, the problem seems to be a regression in v12.3. $\endgroup$
    – kirma
    Commented Mar 4, 2023 at 6:34
4
$\begingroup$

It's both numerical and display issue, and also Analytically a bug.

I think this is a problem in all areas: numerical analytical and a display issue.

Numerical

When evaluating the region numerically coordinates outside but very close ($64\times$ $MachineEpsilon) to the edge are interpreted incorrectly as inside the region.

TableForm@Table[
     {k ϵ, RegionMember[region, {1,1}+{0,1}$MachineEpsilon*k]}
    ,{k, 60, 70,1}
]

enter image description here

Graphical

The Region plot is very bad quality by default, but if you focus on the area of interest one can see that it's interpreted correctly.

Region[
    region
    , PlotRange ->#
    , Axes->True
]& /@ {
        { {-10, 10}, {-10, 10} },
        All, 
        { {0.8, 1.2}, {0.8, 1.2} } 
    }

enter image description here

Analytical

No problems here

RegionMember[region, {1,1}+{0,1}/10^100]
(* False *)

RegionMeasure@  RegionIntersection[region, ImplicitRegion[y == 2 -  x && x<1, {x, y}]]
(* 0 *)

but this is a problem too

RegionMeasure@  RegionIntersection[region, ImplicitRegion[y == 2 -  x && x<=1, {x, y}]]

enter image description here

I would call this a bug, you should contact Wolfram Support.

$\endgroup$
4
  • $\begingroup$ What would you think about the past that I just updated in the question? $\endgroup$
    – hana
    Commented Mar 2, 2023 at 16:44
  • 2
    $\begingroup$ @hana I agree, this is a bug. $\endgroup$
    – rhermans
    Commented Mar 2, 2023 at 16:56
  • $\begingroup$ Do you know any possible workarounds and keep "Implicit"? $\endgroup$
    – hana
    Commented Mar 2, 2023 at 17:31
  • 3
    $\begingroup$ @hana I think my answer provides a workaround. I submitted this as a bug report to WRI. If your license account is a particularly well-paying customer you may want to submit it again just to catch their attention. Some bugs are fixed surprisingly quickly, but some submissions by mere mortals may be piled up for decades... $\endgroup$
    – kirma
    Commented Mar 3, 2023 at 9:27
3
$\begingroup$

The change in polygon shape is a discretisation problem but doesn't affect the RegionDimension part. That appears to be a bug, very similar to one which I have reported to WRI earlier, although not necessarily the same.

In this case you can work around this by Reduce'ing the implicit equation resulting from the intersection:

RegionIntersection[region, 
   ImplicitRegion[y == 3 - 2 x && x <= 1, {x, y}]] /. 
  ImplicitRegion[pred_, vars_] :> 
   ImplicitRegion[Reduce[pred, vars, Reals], vars] // RegionDimension

(* 0 *)
$\endgroup$
3
  • $\begingroup$ Thank you, I will check it out today. I attempted to write an ImplicitRegion myself by dividing a region into many smaller ones and combining them. While the combined region works, the entire code does not seem to be functioning properly. Unfortunately, I believe I may have deleted that code. $\endgroup$
    – hana
    Commented Mar 3, 2023 at 17:14
  • $\begingroup$ Could you please explain why using Reduce makes it work in this case? $\endgroup$
    – hana
    Commented Mar 6, 2023 at 11:33
  • 1
    $\begingroup$ @hana Reduce is able to simplify the region to simply a form corresponding to ImplicitRegion[x == 1 && y == 1, {x, y}], which RegionDimension recognises as a single point. It's more of a question why v12.3 stopped handling the original form correctly... $\endgroup$
    – kirma
    Commented Mar 6, 2023 at 12:46
2
$\begingroup$

RegionPlot generally produces higher quality than Region and has the options PlotPoints and MaxRecursion to help resolve any issues.

Clear["Global`*"]

polygon = Polygon@{{0, 1}, {1, 1}, {5, 5}, {5, 0}, {0, 0}};

region = RegionConvert[polygon, 
   "Implicit"] /. {\[FormalX] -> x, \[FormalY] -> y}

(* ImplicitRegion[(x + 4 y >= 5 && x <= 5 && y <= x) || (x >= 0 && y >= 0 && 
    x + 4 y <= 5 && y <= 1), {x, y}] *)

{Graphics@polygon, Region@region,
 RegionPlot[region[[1]], {x, 0, 5}, {y, 0, 5},
  Frame -> False, BoundaryStyle -> None]}

enter image description here

$\endgroup$
3
  • $\begingroup$ I don't think it's related to plot quality, as this result of the code indicates that the intersection between the polygon and the line is a line, but that is incorrect. polygon = Polygon@{{0, 1}, {1, 1}, {5, 5}, {5, 0}, {0, 0}}; region = RegionConvert[polygon, "Implicit"] /. {\[FormalX] -> x, \[FormalY] -> y}; RegionDimension@ RegionIntersection[region, ImplicitRegion[y == 3 - 2 x && x <= 1, {x, y}]] $\endgroup$
    – hana
    Commented Mar 2, 2023 at 16:04
  • $\begingroup$ It the region were represented correctly, the result of this RegionIntersection[region, ImplicitRegion[y == 3 - 2 x && x <= 1, {x, y}]] should be a point or RegionDimension of 0. $\endgroup$
    – hana
    Commented Mar 2, 2023 at 16:13
  • 2
    $\begingroup$ Then I recommend that you edit your question to reflect the issue that you have. As presented, it appears to be a graphical issue. Note that the RegionDimension result doesn't necessarily mean that the region is wrong, it could be that the RegionIntersection is wrong. Further inspection is required. $\endgroup$
    – Bob Hanlon
    Commented Mar 2, 2023 at 16:22
1
$\begingroup$

I am sure this is not a bug. In many cases the Region command without options produces a plot of low quality. The following works well in 13.2 on Windows 10.

Region[region, PlotRange -> {{-1, 5}, {-1, 5}}]

enter image description here

$\endgroup$
6
  • $\begingroup$ I believe that the issue is not related to the quality of the plot, as it produced an incorrect result when I used region intersection. $\endgroup$
    – hana
    Commented Mar 2, 2023 at 14:49
  • $\begingroup$ @hana: Sorry, don't understand. I don't see any intersection in region. Can you elaborate your comment , giving us a code inastead of empty words? TIA. $\endgroup$
    – user64494
    Commented Mar 2, 2023 at 14:59
  • $\begingroup$ Sorry, what I meant to say is that I checked the RegionDimension>=1 intersection of the region with the line y=ConditionalExpression[3 - 2 x, x <= 1], and the result should be False. However, it is incorrectly showing True because the wrong part of the region has been included. $\endgroup$
    – hana
    Commented Mar 2, 2023 at 15:05
  • $\begingroup$ @hana: You are not right. The command region = RegionConvert[polygon, "Implicit"] performs ImplicitRegion[(-\[FormalX] - 4 \[FormalY] <= -5 && 5 \[FormalX] <= 25 && -4 \[FormalX] + 4 \[FormalY] <= 0) || (-\[FormalX] <= 0 && -5 \[FormalY] <= 0 && \[FormalX] + 4 \[FormalY] <= 5 && \[FormalY] <= 1), {\[FormalX], \[FormalY]}] and we see that region incudes its boundary . In any case the plots of open and closed polygons do not take into account the boundaries of these. $\endgroup$
    – user64494
    Commented Mar 2, 2023 at 15:37
  • $\begingroup$ @user64494 RegionDimension returning 0 means that the dimension consists of zero-dimensional disconnected subregions, in practice points, not that it is empty. In this case the intersection is a single point. If region is empty the region dimension is minus infinity. $\endgroup$
    – kirma
    Commented Mar 3, 2023 at 9:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.