重新定义 Mathematica 中的非交换乘法
Mathematicas NonCommutativeMultiply (**) 不会简化类似
a**0=0**a=0
a**1=1**a=a
或
a**a=a^2.
我想重新定义 **
之类的术语来执行此操作。我使用 NCAlgebra 来执行此操作,但我需要 ReplaceRepeated (//.) 和 NCAlgebra,正如他们的文档所述,专门破坏了 mathematica 中的此功能。
有人可以告诉我如何清除 **
的属性并重新定义此乘法,执行与正常情况相同的操作,并处理 1 和 0。我真的不需要乘法来处理 a**a
,但是如果足够简单就好了。主要是我需要 **
来处理 1 和 0。
Mathematicas NonCommutativeMultiply (**) does not simplify terms like
a**0=0**a=0
a**1=1**a=a
or
a**a=a^2.
I would like to redefine **
to do this. I was using NCAlgebra to do this but I need ReplaceRepeated (//.) and NCAlgebra, as their documentation says, specifically breaks this functionality in mathematica.
Can some show me how to Clear the attributes of **
and redefine this multiplication do the same things it would normal do plus dealing with 1 and 0. I really do not need the multiplication to deal with a**a
, but It would be nice if it is simple enough. The main thing I need **
to deal with 1 and 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当您删除 NonCommutativeMultiply 的 Flat 属性时,以下内容才有效
(这是我在测试过程中犯的错误......菜鸟错误!)
最简单的事情是
需要最终的表达式,以便
a**1
简化为a 而不是
NonCommutativeMultiply[a]
。您可能还需要NonCommutativeMultiply[]:=1
以便正确简化1**1
等表达式 (*)。所有这一切的唯一问题是,对于大型表达式,模式会根据所有内容进行检查,这会变得非常慢。
上述 0 和 1 的两个定义可以组合并推广,从而
排除表达式内的任何数字项。
但这在大型表达式中会进一步减慢速度,因为每个项都会被检查以查看其是否为数值。
要将
a**a
简化为a^2
,您需要类似或更一般的
(*) 请注意,这只是因为 Mathematica 放置其 < 的默认顺序在这种情况下,code>DownValues 不一定是最好的。更改顺序,使
NonCommutativeMultiply[a_]
出现在a___ ** n_?NumericQ ** b___
之前,则不会生成NonCommutativeMultiply[]
按照规则,您不需要最后一个模式(除非您以其他方式生成NonCommutativeMultiply[]
)。The below only works if you remove the Flat attribute of NonCommutativeMultiply
(Which is something I did by mistake during testing... a rookie mistake!)
The simplest thing to do is
The final expression is needed so that
a**1
simplifies toa
instead ofNonCommutativeMultiply[a]
. You might also needNonCommutativeMultiply[]:=1
so that expressions like1**1
simplify properly (*).The only problem with all of this, is for large expressions, the pattern is checked against everything and this gets really slow.
The above two definitions for 0 and 1 can be combined and generalized to
which factors out any numerical terms inside the expression.
But this slows down things even more in large expressions, since each term is checked to see if its numerical.
To simplify your
a**a
toa^2
, you need something likeor more generally
(*) Note that this is only because the default order that Mathematica puts its
DownValues
in is not necessarily the best in this case. Change the order so thatNonCommutativeMultiply[a_]
comes beforea___ ** n_?NumericQ ** b___
thenNonCommutativeMultiply[]
won't be generated by the rules, and you won't need that last pattern (unless you produceNonCommutativeMultiply[]
some other way).好吧,编写与 NonCommutativeMultiply 的属性配合良好的规则有时会很麻烦。下面是另一种方法,它引入了一个帮助程序
NCM
,该帮助程序没有与之关联的NonCommutativeMultiply
的规则和属性。以下代码还包含您的最后几个问题。
(1) (2)
请注意,
NCMFactorNumericQ
速度很快,因为它在单遍中工作,但与其关联的规则nc:NonCommutativeMultiply[a__]/;MemberQ[{a},_?NumericQ ]
速度很慢,因为 Flat 属性意味着它使用NumericQ
进行大量愚蠢的检查。如果您确实想要更快的速度并拥有较大的表达式,那么您应该手动应用
Sort
和Factor
例程,以便 Mathematica 执行更少的模式检查。OK, writing rules that play nice with the attributes of
NonCommutativeMultiply
is sometimes a hassle. Here's an alternate method which introduces a helperNCM
that does not have the rules and attributes ofNonCommutativeMultiply
associated with it.The following code also incorporates the last couple of your questions.
(1) (2)
Note that
NCMFactorNumericQ
is fast because it works in a single pass, but the rule associated with itnc:NonCommutativeMultiply[a__]/;MemberQ[{a},_?NumericQ]
is slow, because the Flat attribute means that it does a stupid number of checks usingNumericQ
.If you really want more speed and have large expressions, then you should just manually apply the
Sort
andFactor
routines, so that Mathematica does less pattern checks.搭配的技巧
非常好!我花了 10 个小时尝试解决
NonCommutativeMultiply
问题(如何展平涉及 nc 和普通乘法的表达式,例如a**b**(c*d*(e**f ))
但更复杂)但我没有想到修改NonCommutativeMultiply
本身。谢谢!The trick with
is very good! I spent 10 hours trying to solve a problem with
NonCommutativeMultiply
(how to flatten expressions that involved both n.c. and normal multiplication likea**b**(c*d*(e**f))
but more complicated) but I didn't think of amendingNonCommutativeMultiply
itself. Thanks!