为什么这不起作用? 选择中的动态

发布于 2024-07-27 18:36:06 字数 240 浏览 4 评论 0原文

好的,我这样做:

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 技术交流群。

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

发布评论

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

评论(3

也只是曾经 2024-08-03 18:36:06

关键是
请记住,Dynamic 不直接控制有关评估的任何内容。 它的作用是在屏幕上创建一个具有评估属性的点。

例如,如果您要在新的 Mathematica 中评估以下内容
session...

b=5;
Dynamic[a=b];
b=6;
Print[a];

...那么会打印什么? 不要立即评估,而是考虑一下
在你尝试之前。 提示...这是一个技巧问题,但要理解其中的技巧
会让您了解 Dynamic 正在做什么。

答案,我不会在这里透露(因为你真的应该尝试一下)
你自己!)可以用以下事实来解释:Dynamic 从未执行过任何操作
因为它从未出现在屏幕上。 分号抑制了屏幕上的显示
Dynamic的外观,以及不出现在屏幕上的Dynamic的评价
一事无成。

更巧妙的是,如果删除所有分号,Print[] 语句(位于
至少在我的机器上)仍然保持不变,但现在完全
不同的原因。 那是因为动态的屏幕放置保证了
它的内容将被评估,但不知道它们何时被评估。 我的
示例设置了一个竞争条件,至少在我的 v7 机器上,
Shift+Enter 评估获胜。

回到你的例子,

Select[Range[1, 20], # > Dynamic[q] &]

这并不像你想象的那样工作,因为在这种情况下是动态的
不评估屏幕上显示的内容。

您可以通过执行以下操作来简单地演示结果...

Dynamic[Select[Range[1, 20], # > q &]]

但我假设您不仅对在屏幕上显示它感兴趣,而且
设置某种副作用。 也许您正在将 Select 分配给一个变量。
有两种方法可以使这些副作用发生。 一种是将它们放入
Dynamic 的第二个参数。 例如...

findset[x_] := (myset = Select[Range[1, 20], # > x &])
Slider[Dynamic[q, (q=#; myset = findset[q])&], {1, 20}]

第二个是生成一个确实具有屏幕外观的动态,但是一个
这是不明显的。 例如,

Row[{
    Slider[Dynamic[q], {1, 20}],
    Dynamic[myset = Select[Range[1, 20], # > q &]; ""]
}]

在这种情况下,实际上正在显示动态。 它显示在
滑块。 但您看不到它,因为它显示的是一个空字符串。
尽管如此,它还是具有任何 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...

b=5;
Dynamic[a=b];
b=6;
Print[a];

...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,

Select[Range[1, 20], # > Dynamic[q] &]

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...

Dynamic[Select[Range[1, 20], # > q &]]

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...

findset[x_] := (myset = Select[Range[1, 20], # > x &])
Slider[Dynamic[q, (q=#; myset = findset[q])&], {1, 20}]

The second is to produce a Dynamic which does have an onscreen appearance, but one
which is not noticeable. For example,

Row[{
    Slider[Dynamic[q], {1, 20}],
    Dynamic[myset = Select[Range[1, 20], # > q &]; ""]
}]

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).

◇流星雨 2024-08-03 18:36:06

我认为您想将“动态”排除在选择之外。 当我玩它时,这似乎有效:

In[20]:= x = 5

Out[20]= 5

In[21]:= Slider[Dynamic[x], {1, 20}]

Out[21]= \!\(\*
SliderBox[Dynamic[$CellContext`x], {1, 20}]\)

In[26]:= (*manually move the slider a bit to the right *)

In[23]:= x

Out[23]= 9.36

In[24]:= Select[Range[1, 20], # > x &]

Out[24]= {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

x = 2 (*slider moves left when I set this*)

Out[25]= 2

编辑:您的实际问题是“为什么这不起作用”而不是“我如何让它起作用”。 问题是这样的:

In[12]:= q = 3

Out[12]= 3


In[13]:= (# > q) &[10]

Out[13]= True


In[14]:= (# > Dynamic[q]) &[10]

(* what you see on the screen looks like an evaluation that is held or something *)
Out[14] = 10 > 3

(* but the full form, which is conveniently what gets copied to the clipboard for
   pasting into this answer, is actually this! *)
Out[14]= 10 > \!\(\*
DynamicBox[ToBoxes[$CellContext`q, StandardForm],
ImageSizeCache->{7., {1., 8.}}]\)

因此,如果您说“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:

In[20]:= x = 5

Out[20]= 5

In[21]:= Slider[Dynamic[x], {1, 20}]

Out[21]= \!\(\*
SliderBox[Dynamic[$CellContext`x], {1, 20}]\)

In[26]:= (*manually move the slider a bit to the right *)

In[23]:= x

Out[23]= 9.36

In[24]:= Select[Range[1, 20], # > x &]

Out[24]= {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

x = 2 (*slider moves left when I set this*)

Out[25]= 2

EDIT: Your actual question was, "why doesn't this work" instead of "how do I get this to work". Here's the problem:

In[12]:= q = 3

Out[12]= 3


In[13]:= (# > q) &[10]

Out[13]= True


In[14]:= (# > Dynamic[q]) &[10]

(* what you see on the screen looks like an evaluation that is held or something *)
Out[14] = 10 > 3

(* but the full form, which is conveniently what gets copied to the clipboard for
   pasting into this answer, is actually this! *)
Out[14]= 10 > \!\(\*
DynamicBox[ToBoxes[$CellContext`q, StandardForm],
ImageSizeCache->{7., {1., 8.}}]\)

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.

∞觅青森が 2024-08-03 18:36:06

您希望整个初始 Select 表达式是动态的,因为只要 q 的值发生变化,您就希望更新选定的子集。 您可以通过将Dynamic移到外部来做到这一点。 尝试这个:

Slider[Dynamic[q], {1, 20}]

Dynamic[Select[Range[1, 20], # > q &]]

You want the entirety of the initial Select expression to be dynamic, because you want to update the selected subset whenever the value of q changes. You can do this by moving the Dynamic outside. Try this:

Slider[Dynamic[q], {1, 20}]

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