简单的功率计数
如何在如下表达式中添加 x 的幂数?
x^2f[x]g[x^3]
or
x^2g[x^4]
or
x^2g[x^2f[x^2]]
计数使得所有这些示例都必须返回 6。 我正在考虑将 Count 与某种模式一起使用,但我没有设法为此构建一个模式。
How can one add the number of powers of x in expressions like the following?
x^2f[x]g[x^3]
or
x^2g[x^4]
or
x^2g[x^2f[x^2]]
The counting is such that all these examples must return 6.
I was thinking of using Count with some pattern, but I didn't manage to construct a pattern for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的快速技巧 - 某些行为(请参阅最后一个示例)可能不完全是您想要的:
然后
和
Here's my quick hack - some of the behaviour (see the final example) might not be quite what you want:
Then
and
由于您想将
x
算作 1 的隐式幂,因此可以使用以下形式:或者,如果这样更易于阅读,也可以不使用 Sow 和 Reap 来编写:
任一形式都可以变得更简洁使用消失的模式,但可能会牺牲清晰度:
Since you want to count
x
as an implicit power of 1, you could use this:Alternatively, this could be written without Sow and Reap if that makes it easier to read:
Either form can be made more terse using vanishing patterns, at the possible expense of clarity: