将东西放入 HoldPattern 中

发布于 2024-09-26 09:58:50 字数 314 浏览 4 评论 0原文

我正在生成这样的替换规则列表。

ops = {LessEqual, GreaterEqual};
ineqRules = Table[HoldPattern[Inequality[a_, op1, c_, _, e_]] -> a == c, {op1, ops}]

上面不起作用,因为“op1”被 HoldPattern 从表中隐藏,我该如何修复它?

这是上一个问题的后续问题

I'm generating a list of replacement rules like this

ops = {LessEqual, GreaterEqual};
ineqRules = Table[HoldPattern[Inequality[a_, op1, c_, _, e_]] -> a == c, {op1, ops}]

Above doesn't work because "op1" is hidden from Table by HoldPattern, how do I fix it?

This is a follow-up to previous question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

挽容 2024-10-03 09:58:50

编辑怎么样

ops = {LessEqual, GreaterEqual};    
ineqRules = (HoldPattern[Inequality[a_, #, c_, _, e_]] :> a == c) & /@ ops

:要解决贝利撒留的答案中指出的问题,请尝试:

ineqRules=Flatten[{HoldPattern[Inequality[a_,#,c_,___]]:>a==c,HoldPattern[#[a_,c_]&&___]:>a==c}&/@ops]

这显然取决于您有一个简单的结构开始,即没有其他 && 的。

How about

ops = {LessEqual, GreaterEqual};    
ineqRules = (HoldPattern[Inequality[a_, #, c_, _, e_]] :> a == c) & /@ ops

Edit: To fix the problem noted in belisarius's answer, try:

ineqRules=Flatten[{HoldPattern[Inequality[a_,#,c_,___]]:>a==c,HoldPattern[#[a_,c_]&&___]:>a==c}&/@ops]

This obviously depends on you having a simple structure to begin with, i.e. no other &&'s.

腹黑女流氓 2024-10-03 09:58:50

这是 With 的工作:

ops = {LessEqual, GreaterEqual};
ineqRules =
  Table[
    With[{op1=op1},
      HoldPattern[Inequality[a_, op1, c_ ,_ ,e_]] -> a == c
    ],
    {op1, ops}
  ]

This is a job for With:

ops = {LessEqual, GreaterEqual};
ineqRules =
  Table[
    With[{op1=op1},
      HoldPattern[Inequality[a_, op1, c_ ,_ ,e_]] -> a == c
    ],
    {op1, ops}
  ]
小情绪 2024-10-03 09:58:50

我确信应该有更好的方法,但这似乎有效:

ops = {LessEqual, GreaterEqual};
ineqRules[op_] := HoldPattern[Inequality[a_, op, c_, _, e_]] -> a == c;
ineq = Table[ineqRules[op], {op, ops}];
Inequality[1, LessEqual, x, Less, 2] /. ineq

Out: 1 == x

HTH

Edit

请小心这一点:

Inequality[e1, GreaterEqual, e2, Equal, e3] /. ineq
Out> e1 == e2

Inequality[1, GreaterEqual, e2, Equal, 2] /. ineq
Out> False

我想如果需要的话,需要一些 Hold[] 野兽才能摆脱困境......让我们知道

I am sure there should be a better way, but this seems to work:

ops = {LessEqual, GreaterEqual};
ineqRules[op_] := HoldPattern[Inequality[a_, op, c_, _, e_]] -> a == c;
ineq = Table[ineqRules[op], {op, ops}];
Inequality[1, LessEqual, x, Less, 2] /. ineq

Out: 1 == x

HTH

Edit

Be carefull with this:

Inequality[e1, GreaterEqual, e2, Equal, e3] /. ineq
Out> e1 == e2

But

Inequality[1, GreaterEqual, e2, Equal, 2] /. ineq
Out> False

I guess some Hold[] beast is needed to get out of that if needed ... let us know

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