wolfram-mathematica - 为什么这个表达式替换不起作用?

标签 wolfram-mathematica wolframalpha

我想用 wolframalpha 求一条线 y = a x + b 通过 point [2,8] 的概率,当 ab 由公平掷骰决定。

这就是我想要的:

Count[Flatten[Table[a 2 + b, {a,6},{b,6}]],8]/
Length[Flatten[Table[a 2 + b, {a,6},{b,6}]]]

,但我不喜欢重复。我不完全确定为什么以下不起作用:

Count[x, 8]/Length[x] /. x -> Flatten[Table[a 2 + b, {a, 6}, {b, 6}]]

我可以解决这个问题吗?发生了什么?

最佳答案

这里的求值顺序不是你想要的:

Count[x, 8]/Length[x] /. x -> Flatten[Table[a 2 + b, {a, 6}, {b, 6}]]

/. 的左侧在替换前求值,因此变为:Indeterminate

您需要延迟评估。通常的方法是使用“纯函数”。参见 Function &Slot # :

Count[#, 8]/Length[#] & @ Flatten[Table[a 2 + b, {a, 6}, {b, 6}]]

可以强制 ReplaceAll(缩写 /.)工作,但它是非标准的:

Unevaluated[ Count[x, 8]/Length[x] ] /.
    x -> Flatten[Table[a 2 + b, {a, 6}, {b, 6}]]

Unevaluted 此处可防止左侧过早求值。

关于wolfram-mathematica - 为什么这个表达式替换不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9145717/

相关文章:

wolfram-mathematica - 如何通过附加到 Mathematica 中重新定义函数?

wolfram-mathematica - 在 ListPlot 中指定颜色序列

wolfram-mathematica - 如何修改OUTPUT字体类型?

python-2.7 - wolframalpha api语法错误

wolfram-mathematica - 如何修复 'Equations may not give solutions for all "解决“变量”错误

functional-programming - Mathematica 的 Thread 函数的语义,最终需要有人把它搁置一旁

wolfram-mathematica - 数学 : Unable to zoom in/out using the mouse on a 3D graphics after Rotate[]

wolfram-mathematica - 如何在 Mathematica 中获得 Wolfram|Alpha 2D 图形的十字准线行为?

matlab - 我们能否从矩阵中得到 eigenVectors 的不同解?

math - 如何将分段函数输入到 Wolfram alpha 中?