Mathematica 绘图中多个函数的检测和样式设置
这个问题让我开始思考 Mathematica 如何检测正在绘制的多个函数。我发现我实在是看不懂这个流程。
考虑:
Plot[{1, Sequence[2, 3], 4}, {x, 0, 1}, PlotRange -> {0, 5}]
我可以理解 Plot
最初在列表中找到三个元素,但它如何“知道”将 2
和 3
设置为相同的样式呢?就好像有关于这两个元素来自起始列表的哪一部分的记忆。这是如何运作的?
This question started me thinking about how Mathematica detects multiple functions being plotted. I find that I really do not understand the process.
Consider:
Plot[{1, Sequence[2, 3], 4}, {x, 0, 1}, PlotRange -> {0, 5}]
I can understand that Plot
finds three elements in the list initially, but how does it "know" to style 2
and 3
the same? It is as though there is a memory of what part of the starting list those two elements came from. How does this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,它知道存在三个参数:
如果允许 x 求值,那么
编辑:人们会更好地看到它:
换句话说,序列的扁平化需要求值器。
编辑2:我显然错过了提出的真正问题,现在将尝试回答它。
一旦 Plot 确定了参数的数量,它就会构建 {{ style1, Line ..}, {style2, Line..}, ... }。在 {1,Sequence[2,3],4} 的情况下,我们得到以下结构:
当绘制 {1,{2,3},4} 时,我们得到不同的结构:
因为列表会被展平,只是不使用评估者。因此,正如您所看到的,出现相同颜色的标记是因为 Sequence[2,3] 被视为黑盒函数,它返回两个元素的列表:
我试图构建一个顶级实现来构建这样的结构,但必须与评估者对抗。例如:
现在我们必须评估线程而不评估其参数,这将给出
{{1, 1}, {2, Sequence[2,3]}, {3, 4}},其中列表的第一个元素是标签,后面的一个元素是要采样的函数。
希望这有帮助。
Well, it knows that there three arguments just so:
If x is allowed to evaluate, then
EDIT: One sees it better with:
in other words, flattening of Sequence requires evaluator.
EDIT 2: I clearly missed the real question posed and will try to answer it now.
Once Plot determines the number of argument it builds {{ style1, Line ..}, {style2, Line..}, ... }. In the case of {1,Sequence[2,3],4} we get the following structure:
When plotting {1,{2,3},4} we get a different structure:
because lists would be flattened, just not using the evaluator. So as you see the tagging in the same color occurs because Sequence[2,3] is treated as a black-box function which returns a list of two elements:
I was trying to build a top-level implementation which would build such a structure, but one has to fight the evaluator. For example:
Now we have to evaluate the Thread without evaluating its arguments, which would give
{{1, 1}, {2, Sequence[2,3]}, {3, 4}}, where the first element of the list is a tag, and the subsequent once are functions to be sampled.
Hope this helps.
想象一个产生此输出的过程并不难。我没有额外的证据证明这确实发生了,但可以合理地假设
Plot
循环遍历传递给它的函数列表,并将每个函数与一个样式相关联。然后,在为绘图变量设置值后,继续评估它们中的每一个。通常,每个“函数”(传递给Plot
的列表中的元素)都会返回一个实数。然而,从版本 6 开始,Mathematica 也可以处理返回数字列表的内容,但其缺陷是它对完整列表使用相同的样式。版本 5 会对返回列表的函数引发错误。It's not that difficult to imagine a process which results in this output. I don't have additional proof that this is indeed what happens, but it is reasonable to assume that
Plot
loops through the list of functions that were passed to it, and associates a style with each. Then it proceeds to evaluate each of them after setting a value to the plot variable. Normally each "function" (element in the list passed toPlot
) would return a real number. However, since version 6, Mathematica can handle those that return lists of numbers too, with the flaw that it uses the same styling for the complete list. Version 5 would throw an error for functions that returned lists.