为什么这不起作用? 选择中的动态
好的,我这样做:
Select[Range[1, 20], # > Dynamic[q] &]
然后我创建滑块:
Slider[Dynamic[q], {1, 20}]
它总是返回一个空集! 为什么!
更新 这样做的目的是当我移动滑块时让设置发生变化。
Ok, I do this:
Select[Range[1, 20], # > Dynamic[q] &]
And then I create the slider:
Slider[Dynamic[q], {1, 20}]
And it'll always return an empty set! Why!
Update
The goal of this is to have the set change as I move the slider.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关键是
请记住,Dynamic 不直接控制有关评估的任何内容。 它的作用是在屏幕上创建一个具有评估属性的点。
例如,如果您要在新的 Mathematica 中评估以下内容
session...
...那么会打印什么? 不要立即评估,而是考虑一下
在你尝试之前。 提示...这是一个技巧问题,但要理解其中的技巧
会让您了解 Dynamic 正在做什么。
答案,我不会在这里透露(因为你真的应该尝试一下)
你自己!)可以用以下事实来解释:Dynamic 从未执行过任何操作
因为它从未出现在屏幕上。 分号抑制了屏幕上的显示
Dynamic的外观,以及不出现在屏幕上的Dynamic的评价
一事无成。
更巧妙的是,如果删除所有分号,Print[] 语句(位于
至少在我的机器上)仍然保持不变,但现在完全
不同的原因。 那是因为动态的屏幕放置保证了
它的内容将被评估,但不知道它们何时被评估。 我的
示例设置了一个竞争条件,至少在我的 v7 机器上,
Shift+Enter 评估获胜。
回到你的例子,
这并不像你想象的那样工作,因为在这种情况下是动态的
不评估屏幕上显示的内容。
您可以通过执行以下操作来简单地演示结果...
但我假设您不仅对在屏幕上显示它感兴趣,而且
设置某种副作用。 也许您正在将 Select 分配给一个变量。
有两种方法可以使这些副作用发生。 一种是将它们放入
Dynamic 的第二个参数。 例如...
第二个是生成一个确实具有屏幕外观的动态,但是一个
这是不明显的。 例如,
在这种情况下,实际上正在显示动态。 它显示在
滑块。 但您看不到它,因为它显示的是一个空字符串。
尽管如此,它还是具有任何 Dynamic 所具有的所有自动更新属性。
有关更多信息,您应该阅读入门和高级动态教程
在 Mathematica 文档中。 您还可以在 此处查看我在 comp.soft-sys.math.mathematica 上的帖子(我对此响应进行了部分重新表述)。
The key is to
remember that Dynamic does not control anything about evaluation directly. What it does is to create a spot on the screen which has evaluation properties.
If, for example, you were to evaluate the following in a fresh Mathematica
session...
...then what will be printed? Instead of evaluating it immediately, think about
it before you try it. Hint...it's a trick question, but understanding the trick
will open your mind to exactly what Dynamic is doing.
The answer, which I will not reveal here (because you should really try it for
yourself!) can be explained by the fact that the Dynamic never did anything
because it never showed up onscreen. The semicolon inhibited the onscreen
appearance of Dynamic, and without appearing onscreen, the evaluation of Dynamic
accomplishes nothing.
More subtly, if you remove all of the semicolons, the Print[] statement (at
least on my machine) still remains unchanged, but now for a completely
different reason. That's because the onscreen placement of a Dynamic guarantees
that its contents will be evaluated, but not when they'll be evaluated. My
example sets up a race condition which, at least on my machine in v7, the
Shift+Enter evaluation wins.
To go back to your example,
This doesn't work the way you think it does because the Dynamic in this case
isn't evaluating to something which is displayed onscreen.
You could trivially demonstrate the result by doing...
but I'll assume you weren't just interested in displaying it on the screen, but
in setting some kind of side effect. Perhaps you were assigning Select to a variable.
There are two ways to make these side effects happen. One is to put them in the
second argument of Dynamic. For example...
The second is to produce a Dynamic which does have an onscreen appearance, but one
which is not noticeable. For example,
In this case, the Dynamic actually is displaying. It displays right next to the
Slider. But you don't see it because what it's displaying is an empty string.
Nonetheless, it has all of the automatic updating properties that any Dynamic has.
For more information, you should read the beginning and advanced Dynamic tutorials
in the Mathematica documentation. You can also see my post on comp.soft-sys.math.mathematica here (which I partly reformulated for this response).
我认为您想将“动态”排除在选择之外。 当我玩它时,这似乎有效:
编辑:您的实际问题是“为什么这不起作用”而不是“我如何让它起作用”。 问题是这样的:
因此,如果您说“Dynamic[1]”,您确实会在屏幕上看到“3”,但它并不是真正的“3”——它是某种实际上显示的笔记本元素 一个“3”。
比较函数的结果是像上面这样的表达式,它的计算结果不为 True,因此 select 不接受任何元素,因此您得到一个空集。
I think you want to leave "Dynamic" out of the select. This seems to work when I play with it:
EDIT: Your actual question was, "why doesn't this work" instead of "how do I get this to work". Here's the problem:
So if you say 'Dynamic[1]' you do get a '3' on the screen, but it's not really a '3' -- it's some kind of notebook element that actually displays a '3'.
The result of the comparison function is an expression like the above, which does not evaluate to True, so select does not accept any elements, so you get an empty set.
您希望整个初始
Select
表达式是动态的,因为只要q
的值发生变化,您就希望更新选定的子集。 您可以通过将Dynamic
移到外部来做到这一点。 尝试这个:You want the entirety of the initial
Select
expression to be dynamic, because you want to update the selected subset whenever the value ofq
changes. You can do this by moving theDynamic
outside. Try this: