如何绘制有界整数方程的 mathematica 解返回的列表

发布于 2024-12-28 18:42:00 字数 448 浏览 4 评论 0原文

所以我有一组有界丢番图方程,用于指定平面上的线。我想让mathematica 绘制这些方程中两个方程的交集,这样我就可以看到它们的样子。

到目前为止我有类似的东西:

求解[0 < x - y < 3&& -1< 2x-y< 2、{x,y},整数]

返回一些结构,例如:

{{x ->; -2,y-> -4},{x-> -1,y-> -3},{x-> -1,y-> -2},{x-> 0, y-> -1}}

但是我现在怎样才能让mathematica绘制这个图以便我可以看到结果的形状。最好我希望绘图将每个“点”视为 1x1 正方形。

另外,我想知道是否有更好的方法来做这些事情。谢谢。

So I have a set of bounded diophantine equations that specify lines on the plane. I want to make mathematica plot the intersection of two of these equations so I can see what they look like.

So far I have something like:

Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers]

which returns some structure like:

{{x -> -2, y -> -4}, {x -> -1, y -> -3}, {x -> -1, y -> -2}, {x -> 0,
y -> -1}}

but how can I now make mathematica plot this so I can see the resulting shape. Preferably I would like the plot to consider every 'point' to be a 1x1 square.

Also, I wonder if there is a better way to do such things. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も星光 2025-01-04 18:42:00

通过转换 Solve[] 返回的列表来定义要绘制的数据。 更一般地

 data = {x, y} /. Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers]

,您可以使用以下技巧使 Solve 以列表格式(而不是一组规则)返回解决方案:

 data = Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers] /. Rule[a_,b_]->b

对于绘图,在许多替代方案中,您可以使用 < code>ListPlot 以

ListPlot[data, PlotMarkers -> {Style["\[FilledSquare]", FontSize -> 16]}]

获得以下输出

output image

您可以使用许多样式和其他选项进一步细化它的列表图。例如,您可以加入点

ListPlot[data, PlotMarkers -> {Style["\[FilledSquare]", FontSize -> 16]}, 
 Joined -> True]

以获得

joinedplot

编辑:要使用标记位置和大小,有多种选择。使用ListPlot,您可以通过以下两种方式获得所需的内容:

 (* Alternative 1: use fontsize to change the marker size *)
 lp1 := ListPlot[{#} & /@ #1, 
 PlotMarkers -> {Style["\[FilledSquare]", FontSize -> Scaled[#2]]},
 AspectRatio -> 1, AxesOrigin -> {0, 0}, 
 PlotRange -> {{-5, 1}, {-5, 1}}, 
 PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], 
 Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, 
  Line@#1}, Frame -> True, FrameTicks -> All] &;
 (* usage example *)
 lp1 @@ {data, .30}

 (* Alternative 2: use the second parameter of PlotMarkers to control scaled size *)
 lp2 := ListPlot[{#} & /@ #1, 
 PlotMarkers -> {Graphics@{Rectangle[]}, #2}, AspectRatio -> 1, 
 AxesOrigin -> {0, 0}, PlotRange -> {{-5, 1}, {-5, 1}}, 
 PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], 
 Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, 
 Line@#1}, Frame -> True, FrameTicks -> All] &
 (* usage example *)
 lp2 @@ {data, 1/5.75}

在这两种情况下,您都需要使用Epilog,否则连接点的线会被标记遮挡。两种替代方案都会产生以下输出:

listplot withmarkers

或者,您可以使用 GraphicsRegionPlotContourPlotBubbleChartdata 进行适当的转换,以获得与 ListPlot 类似的结果代码>输出 多于。

使用图形基元:

 (* data transformation to define the regions *)
 trdataG[data_, size_] :=  data /. {a_, b_} :> 
         {{a - size/2, b - size/2}, {a + size/2, b + size/2}};
 (* plotting function *)
 gr := Graphics[
      {
      {Hue[RandomReal[]], Rectangle[##]} & @@@ trdataG @@ {#1, #2}, 
      GrayLevel[.3], PointSize[.02], Thick, Point@#1, Line@#1}, 
      PlotRange -> {{-5, 1}, {-5, 1}
      }, 
      PlotRangePadding -> 0, Axes -> True, AxesOrigin -> {0, 0}, 
      Frame -> True, FrameTicks -> All] &
 (* usage example *)
 gr @@ {data, .99}

使用气泡图:

 (* Transformation of data to a form that BubbleChart expects *)
 dataBC[data_] := data /. {a_, b_} :> {a, b, 1};
 (* custom markers *)
 myMarker[size_][{{xmin_, xmax_}, {ymin_, ymax_}}, ___] :=
      {EdgeForm[], Rectangle[{(1/2) (xmin + xmax) - size/2, (1/2) (ymin + ymax) - 
       size/2}, {(1/2) (xmin + xmax) + size/2, (1/2) (ymin + ymax) + size/2}]};
 (* charting function *)
 bc := BubbleChart[dataBC[#1], ChartElementFunction -> myMarker[#2], 
       ChartStyle -> Hue /@ RandomReal[1, {Length@#1}], Axes -> True, 
       AxesOrigin -> {0, 0}, PlotRange -> {{-5, 1}, {-5, 1}}, 
       PlotRangePadding -> 0, AspectRatio -> 1, FrameTicks -> All, 
       Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 bc @@ {data, .99}

使用 RegionPlot:

 (* Transformation of data to a form that RegionPlot expects *)
  trdataRP[data_, size_] :=  data /. {a_, b_} :> 
            a - size/2 <= x <= a + size/2 && b - size/2 <= y <= b + size/2
 (* charting function *)
 rp := RegionPlot[Evaluate@trdataRP[#1, #2], {x, -5, 1}, {y, -5, 1}, 
          AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, 
          PlotRange -> {{-5, 1}, {-5, 1}}, 
          PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], FrameTicks -> All, 
          PlotPoints -> 100, BoundaryStyle -> None, 
          Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 rp @@ {data, .99}

使用 ContourPlot:

 (* Transformation of data to a form that ContourPlot expects *)
 trdataRP[data_, size_] :=   data /. {a_, b_} :> 
            a - size/2 <= x <= a + size/2 && b - size/2 <= y <= b + size/2;
 trdataCP[data_, size_] := Which @@ Flatten@
           Thread[{trdataRP[data, size], Range@Length@data}];
 (* charting function *)
 cp := ContourPlot[trdataCP[#1, #2], {x, -5, 1}, {y, -5, 1}, 
             AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, 
             PlotRange -> {{-5, 1}, {-5, 1}}, FrameTicks -> All, 
             ExclusionsStyle -> None, PlotPoints -> 100, 
             ColorFunction -> Hue, 
             Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 cp @@ {data, .99}

Define the data you wish to plot by transforming the list Solve[] returns. This can done as

 data = {x, y} /. Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers]

More generally, you can make Solve return the solution in a list format (rather than as a set of rules) using the following trick:

 data = Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers] /. Rule[a_,b_]->b

For plotting, among many alternatives, you can use ListPlot as

ListPlot[data, PlotMarkers -> {Style["\[FilledSquare]", FontSize -> 16]}]

to get the following output

output image

You can further refine it using many styling and other options of ListPlot. For example, you can join the points

ListPlot[data, PlotMarkers -> {Style["\[FilledSquare]", FontSize -> 16]}, 
 Joined -> True]

to get

joined plot

EDIT: To play with the marker placement and size there are several alternatives. Using ListPlot you can get what you need in either of the two ways:

 (* Alternative 1: use fontsize to change the marker size *)
 lp1 := ListPlot[{#} & /@ #1, 
 PlotMarkers -> {Style["\[FilledSquare]", FontSize -> Scaled[#2]]},
 AspectRatio -> 1, AxesOrigin -> {0, 0}, 
 PlotRange -> {{-5, 1}, {-5, 1}}, 
 PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], 
 Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, 
  Line@#1}, Frame -> True, FrameTicks -> All] &;
 (* usage example *)
 lp1 @@ {data, .30}

 (* Alternative 2: use the second parameter of PlotMarkers to control scaled size *)
 lp2 := ListPlot[{#} & /@ #1, 
 PlotMarkers -> {Graphics@{Rectangle[]}, #2}, AspectRatio -> 1, 
 AxesOrigin -> {0, 0}, PlotRange -> {{-5, 1}, {-5, 1}}, 
 PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], 
 Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, 
 Line@#1}, Frame -> True, FrameTicks -> All] &
 (* usage example *)
 lp2 @@ {data, 1/5.75}

In both cases, you need to use Epilog, otherwise the lines joining points are occluded by the markers. Both alternatives produce the following output:

listplot with markers

Alternatively, you can use Graphics, RegionPlot, ContourPlot, BubbleChart with appropriate transformations of data to get results similar to the one in ListPlot output above.

Using Graphics primitives:

 (* data transformation to define the regions *)
 trdataG[data_, size_] :=  data /. {a_, b_} :> 
         {{a - size/2, b - size/2}, {a + size/2, b + size/2}};
 (* plotting function *)
 gr := Graphics[
      {
      {Hue[RandomReal[]], Rectangle[##]} & @@@ trdataG @@ {#1, #2}, 
      GrayLevel[.3], PointSize[.02], Thick, Point@#1, Line@#1}, 
      PlotRange -> {{-5, 1}, {-5, 1}
      }, 
      PlotRangePadding -> 0, Axes -> True, AxesOrigin -> {0, 0}, 
      Frame -> True, FrameTicks -> All] &
 (* usage example *)
 gr @@ {data, .99}

Using BubbleChart:

 (* Transformation of data to a form that BubbleChart expects *)
 dataBC[data_] := data /. {a_, b_} :> {a, b, 1};
 (* custom markers *)
 myMarker[size_][{{xmin_, xmax_}, {ymin_, ymax_}}, ___] :=
      {EdgeForm[], Rectangle[{(1/2) (xmin + xmax) - size/2, (1/2) (ymin + ymax) - 
       size/2}, {(1/2) (xmin + xmax) + size/2, (1/2) (ymin + ymax) + size/2}]};
 (* charting function *)
 bc := BubbleChart[dataBC[#1], ChartElementFunction -> myMarker[#2], 
       ChartStyle -> Hue /@ RandomReal[1, {Length@#1}], Axes -> True, 
       AxesOrigin -> {0, 0}, PlotRange -> {{-5, 1}, {-5, 1}}, 
       PlotRangePadding -> 0, AspectRatio -> 1, FrameTicks -> All, 
       Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 bc @@ {data, .99}

Using RegionPlot:

 (* Transformation of data to a form that RegionPlot expects *)
  trdataRP[data_, size_] :=  data /. {a_, b_} :> 
            a - size/2 <= x <= a + size/2 && b - size/2 <= y <= b + size/2
 (* charting function *)
 rp := RegionPlot[Evaluate@trdataRP[#1, #2], {x, -5, 1}, {y, -5, 1}, 
          AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, 
          PlotRange -> {{-5, 1}, {-5, 1}}, 
          PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], FrameTicks -> All, 
          PlotPoints -> 100, BoundaryStyle -> None, 
          Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 rp @@ {data, .99}

Using ContourPlot:

 (* Transformation of data to a form that ContourPlot expects *)
 trdataRP[data_, size_] :=   data /. {a_, b_} :> 
            a - size/2 <= x <= a + size/2 && b - size/2 <= y <= b + size/2;
 trdataCP[data_, size_] := Which @@ Flatten@
           Thread[{trdataRP[data, size], Range@Length@data}];
 (* charting function *)
 cp := ContourPlot[trdataCP[#1, #2], {x, -5, 1}, {y, -5, 1}, 
             AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, 
             PlotRange -> {{-5, 1}, {-5, 1}}, FrameTicks -> All, 
             ExclusionsStyle -> None, PlotPoints -> 100, 
             ColorFunction -> Hue, 
             Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 cp @@ {data, .99}
陌伤ぢ 2025-01-04 18:42:00

可能是

sol = Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers];
pts = Cases[sol, {_ -> n_, _ -> m_} :> {n, m}];
ListPlot[pts, Mesh -> All, Joined -> True, AxesOrigin -> {0, 0}, 
 PlotMarkers -> {Automatic, 10}]

在此处输入图像描述

也可以使用提取点来绘制

{#[[1, 2]], #[[2, 2]]} & /@ sol

may be

sol = Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers];
pts = Cases[sol, {_ -> n_, _ -> m_} :> {n, m}];
ListPlot[pts, Mesh -> All, Joined -> True, AxesOrigin -> {0, 0}, 
 PlotMarkers -> {Automatic, 10}]

enter image description here

Can also extract the points to plot using

{#[[1, 2]], #[[2, 2]]} & /@ sol
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文