Mathematica's Plot 中多个函数的检测和样式 [英] Detection and styling of multiple functions in Mathematica's Plot
问题描述
这个问题让我开始思考 Mathematica 如何检测正在绘制的多个函数.我发现我真的不明白这个过程.
This question started me thinking about how Mathematica detects multiple functions being plotted. I find that I really do not understand the process.
考虑:
Plot[{1, Sequence[2, 3], 4}, {x, 0, 1}, PlotRange -> {0, 5}]
我可以理解 Plot
最初在列表中找到三个元素,但是它如何知道"设置 2
和 3
的样式?相同的?就好像有一个关于这两个元素来自起始列表的哪一部分的记忆.这是如何工作的?
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?
推荐答案
好吧,它知道只有三个参数:
Well, it knows that there three arguments just so:
In[13]:= Function[x, Length[Unevaluated[x]], HoldAll][{1,
Sequence[2, 3], 4}]
Out[13]= 3
如果允许 x 求值,则
If x is allowed to evaluate, then
In[14]:= Function[x, Length[x], HoldAll][{1, Sequence[2, 3], 4}]
Out[14]= 4
:
In[15]:= Hold[{1, Sequence[2, 3], 4}]
Out[15]= Hold[{1, Sequence[2, 3], 4}]
换句话说,序列的扁平化需要评估器.
in other words, flattening of Sequence requires evaluator.
编辑 2: 我显然错过了提出的真正问题,现在将尝试回答.
EDIT 2: I clearly missed the real question posed and will try to answer it now.
Once Plot 确定它构建的参数数量 {{ style1, Line ..}, {style2, Line..}, ... }.在 {1,Sequence[2,3],4} 的情况下,我们得到以下结构:
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:
In[23]:= Cases[
Plot[{1, Sequence[2, 3], 4}, {x, 0, 1},
PlotRange -> {0, 5}], {_Hue, __Line},
Infinity] /. {x_Line :> Line, _Hue -> Hue}
Out[23]= {{Hue, Line}, {Hue, Line, Line}, {Hue, Line}}
绘制 {1,{2,3},4} 时,我们得到不同的结构:
When plotting {1,{2,3},4} we get a different structure:
In[24]:= Cases[
Plot[{1, List[2, 3], 4}, {x, 0, 1},
PlotRange -> {0, 5}], {_Hue, __Line},
Infinity] /. {x_Line :> Line, _Hue -> Hue}
Out[24]= {{Hue, Line}, {Hue, Line}, {Hue, Line}, {Hue, Line}}
因为列表会变平,只是不使用评估器.因此,如您所见,出现相同颜色的标记是因为 Sequence[2,3] 被视为一个返回两个元素列表的黑盒函数:
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:
In[25]:= g[x_?NumberQ] := {2, 3}
In[26]:= Cases[
Plot[{1, g[x], 4}, {x, 0, 1}, PlotRange -> {0, 5}], {_Hue, __Line},
Infinity] /. {x_Line :> Line, _Hue -> Hue}
Out[26]= {{Hue, Line}, {Hue, Line, Line}, {Hue, Line}}
我试图构建一个顶级实现来构建这样的结构,但必须与评估器作斗争.例如:
I was trying to build a top-level implementation which would build such a structure, but one has to fight the evaluator. For example:
In[28]:= Thread /@ Function[x,
Thread[{Hold @@ {Range[Length[Unevaluated[x]]]}, Hold[x]}, Hold]
, HoldAll][{1, Sequence[2, 3], 4}]
Out[28]= Hold[Thread[{{1, 2, 3}, {1, Sequence[2, 3], 4}}]]
现在我们必须评估线程而不评估其参数,这将给出{{1, 1}, {2, Sequence[2,3]}, {3, 4}},其中列表的第一个元素是一个标签,后面的一次是要采样的函数.
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.
希望这会有所帮助.
这篇关于Mathematica's Plot 中多个函数的检测和样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!