Mathematica 帮助中的 ColorFunctionScaling 示例失败 - 为什么?

发布于 2024-10-25 10:03:30 字数 873 浏览 0 评论 0原文

Mathematica 7 帮助中有一个关于 Plot > 的示例选项> ColorFunctionScaling

Table[Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
  ColorFunction -> Function[{x, y}, Hue[x]], 
  ColorFunctionScaling -> cf], {cf, {False, True}}]

在此处输入图像描述

当我自己在 Mathematica 7 上对其进行评估时,两个输出图看起来都像一个在左边

但是,如果我对此进行评估,我会得到右侧的图,如上所示:

Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
 ColorFunction -> Function[{x, y}, Hue[x]], 
 ColorFunctionScaling -> True]

为什么给出的示例可能会失败?


Alexey 和 Simon 证明这不是 HoldAll 的结果,正如我之前推测的那样。

该示例的存在让我怀疑它曾经有效,并且它在版本 8 上有效的信息告诉我行为已经改变。究竟发生了什么变化?

There is an example in the Mathematica 7 help for Plot > Options > ColorFunctionScaling.

Table[Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
  ColorFunction -> Function[{x, y}, Hue[x]], 
  ColorFunctionScaling -> cf], {cf, {False, True}}]

enter image description here

When I evaluate it myself on Mathematica 7, both output plots look like the one on the left.

However, if I evaluate this, I get the plot on the right, as shown above:

Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
 ColorFunction -> Function[{x, y}, Hue[x]], 
 ColorFunctionScaling -> True]

Why might the example as given fail?


Alexey and Simon demonstrated that this is not the result of HoldAll, as I presumed before.

The existence of the example leads me to suspect it once worked, and the information that it works on version 8 tells me that the behavior has changed. What precisely has changed?

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

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

发布评论

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

评论(3

吾家有女初长成 2024-11-01 10:03:30

你的问题真的很有趣。所提到的向内置函数提供选项值的方法在文档中广泛使用。事实上,它仅对 ColorFunctionScaling 失败,这看起来像是一个错误。并且在 v.8 中不存在此问题的信息证实了这是 v.7 中的错误。

无论如何,请考虑以下事项:

In[1]:= SetAttributes[f, HoldAll]
f[__, OptionsPattern[ColorFunctionScaling -> False]] := 
 OptionValue[ColorFunctionScaling]
Table[f[Sin[4 Pi x], {x, 0, 1/2}, 
  ColorFunctionScaling -> cf], {cf, {False, True}}]


Out[3]= {False, True}

您可以看到 HoldAll 属性确实不会阻止替换 cf

这样一来,v.7 中 PlotTable 所描述的错误行为的原因是什么,这真的很有趣?

Your question is really interesting. The mentioned method of supplying option values to built-in functions is widely used in the Documentation. The fact that it fails only for ColorFunctionScaling looks like a bug. And information that in v.8 this problem does not exist confirms that this is a bug in v.7.

In any way consider the following:

In[1]:= SetAttributes[f, HoldAll]
f[__, OptionsPattern[ColorFunctionScaling -> False]] := 
 OptionValue[ColorFunctionScaling]
Table[f[Sin[4 Pi x], {x, 0, 1/2}, 
  ColorFunctionScaling -> cf], {cf, {False, True}}]


Out[3]= {False, True}

You can see that HoldAll attribute in really does not prevent substituting of cf.

In this way, it is really interesting what was the cause of described buggy behavior of Plot with Table in v.7?

森林迷了鹿 2024-11-01 10:03:30

评估顺序似乎有点不对劲。如果您在查看 Plot 命令之前强制替换 cf,它就会起作用。为此,我们使用 With[{x=x},...] 构造:

Table[With[{cf = cf}, 
  Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
   ColorFunction -> Function[{x, y}, Hue[x]], 
   ColorFunctionScaling -> cf]], {cf, {False, True}}]

plot

奇怪的是,在 Mathematica 版本 8 中不需要这样的拼凑。

更奇怪的是,Mathematica 7 文档有一个示例,其中预评估的图形与该版本生成的图形不匹配。 (不错的发现,顺便说一句)

The evaluation order seems slightly out. It works if you force cf to be substituted in before the Plot command is looked at. To do this we use the With[{x=x},...] construct:

Table[With[{cf = cf}, 
  Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
   ColorFunction -> Function[{x, y}, Hue[x]], 
   ColorFunctionScaling -> cf]], {cf, {False, True}}]

plot

It's strange that you don't need such a kludge in Mathematica version 8.

It's even stranger that the Mathematica 7 documentation has an example where the pre-evaluated graphics does not match what is produced by that version. (Nice find, btw)

征﹌骨岁月お 2024-11-01 10:03:30

这个错误实际上与 HoldAll 属性有关,但我被 此自动加载问题 认为事实并非如此。这可以通过执行以下命令看出:

Plot[Sin[x], {x, 0, Pi}];

Unprotect[Plot]
ClearAttributes[Plot, HoldAll]

Table[Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
  ColorFunction -> Function[{x, y}, Hue[x]], 
  ColorFunctionScaling -> cf], {cf, {False, True}}]

需要第一个 Plot 来激活包加载。

因此,可以通过包装 ColorFunctionScaling -> 来获得正确的行为。 ... 在评估中:

Table[Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
  ColorFunction -> Function[{x, y}, Hue[x]], 
  Evaluate[ColorFunctionScaling -> cf]], {cf, {False, True}}]

在此处输入图像描述

This bug in fact is related to the HoldAll attribute, but I was fooled by this auto-load issue into thinking it was not. This can be seen by executing this:

Plot[Sin[x], {x, 0, Pi}];

Unprotect[Plot]
ClearAttributes[Plot, HoldAll]

Table[Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
  ColorFunction -> Function[{x, y}, Hue[x]], 
  ColorFunctionScaling -> cf], {cf, {False, True}}]

The first Plot is needed to activate the package load.

One can therefore get the correct behavior by wrapping ColorFunctionScaling -> ... in Evaluate:

Table[Plot[Sin[4 Pi x], {x, 0, 1/2}, PlotStyle -> Thick, 
  ColorFunction -> Function[{x, y}, Hue[x]], 
  Evaluate[ColorFunctionScaling -> cf]], {cf, {False, True}}]

enter image description here

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