Mathematica 的 Minimize 函数
Mathematica 的 Minimize
函数是否不允许像 Mod[x,2]==0 这样的约束?我正在尝试解决 MinuteMath 与 Mathematica 的谜题:
四个不同正偶数的最小可能平均值是多少?
我的“解决方案”如下所示:
vars = Subscript[x, #] & /@ Range[4];
cond = Apply[And, Mod[#, 2] == 0 & /@ vars] &&
(0 < Subscript[x, 1]) &&
Apply[And, Table[Subscript[x, i] < Subscript[x, i + 1], {i, 1, 3}]];
Minimize[{Mean[vars], cond}, vars, Integers]
但是 Minimize
返回未计算的值。附加问题:我可以使用 EvenQ 来定义约束吗?问题是,对于未定义的表达式 x
,EvenQ[x]
返回 False
。
Is it true that Mathematica's Minimize
function does not allow constraints like Mod[x,2]==0? I am trying to solve a MinuteMath puzzle with Mathematica:
What is the smallest possible average of four distinct positive even integers?
My "solution" looks like this:
vars = Subscript[x, #] & /@ Range[4];
cond = Apply[And, Mod[#, 2] == 0 & /@ vars] &&
(0 < Subscript[x, 1]) &&
Apply[And, Table[Subscript[x, i] < Subscript[x, i + 1], {i, 1, 3}]];
Minimize[{Mean[vars], cond}, vars, Integers]
but Minimize
returns unevaluated. Additional question: Can I use EvenQ
for defining the constraints? Problem is, EvenQ[x]
returns False
for undefined expressions x
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,这不需要 Mathematica,但是,在回答您的问题时,Minimize 似乎不喜欢这些模组。不过,您可以将其构建到论坛中,如下所示:
Clearly, this doens't require Mathematica but, in answer to your question, it seems that
Minimize
doesn't like the mods. You could build it into the forumula, though, like so:对于这个问题来说显然是矫枉过正,但对于展示一些技巧很有用。
请注意:
可以用作
So:
的替代品,或者可能更优雅:
A clear overkill for this problem, but useful to show some tricks.
Note that:
can be used as an alternative to
So:
Or perhaps more elegant:
最小化对这个问题不好,它给出了错误的解决方案:
2 4 8 10
好的解决方案是:
2 4 6 12
“Select”accept Mod function 它比最小化
最小化给出了错误的解决方案。
播放该程序并看到:
Minimize is not good for this question, it gives a wrong solution:
2 4 8 10
The good solution is:
2 4 6 12
"Select" accept Mod function it's better than Minimize
Minimize gives the wrong solution.
Play this programe and see: