重新定义 Mathematica 中的非交换乘法 [英] Redefine Noncommutative Multiplication in Mathematica

查看:22
本文介绍了重新定义 Mathematica 中的非交换乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mathematicas NonCommutativeMultiply (**) 不会简化像

Mathematicas NonCommutativeMultiply (**) does not simplify terms like

a**0=0**a=0  
a**1=1**a=a  

a**a=a^2.  

我想重新定义 ** 来做到这一点.我正在使用 NCAlgebra 来做到这一点,但我需要 ReplaceRepeated (//.) 和 NCAlgebra,正如他们的文档所说,专门破坏了 mathematica 中的这个功能.

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.

有人可以告诉我如何清除 ** 的属性并重新定义这个乘法做与正常做相同的事情加上处理 1 和 0.我真的不需要乘法来处理使用 a**a,但是如果它足够简单就更好了.我主要需要 ** 来处理 1 和 0.

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.

推荐答案

以下仅在您移除 NonCommutativeMultiply 的 Flat 属性时有效(这是我在测试过程中犯的错误……菜鸟错误!)

The below only works if you remove the Flat attribute of NonCommutativeMultiply (Which is something I did by mistake during testing... a rookie mistake!)

最简单的方法是

Unprotect[NonCommutativeMultiply];
NonCommutativeMultiply[a___, 1, b___] := a ** b
NonCommutativeMultiply[___, 0, ___] := 0
NonCommutativeMultiply[a_] := a
Protect[NonCommutativeMultiply];

需要最后的表达式,以便 a**1 简化为 a 而不是 NonCommutativeMultiply[a].您可能还需要 NonCommutativeMultiply[]:=1 以便像 1**1 这样的表达式正确地简化 (*).所有这一切的唯一问题是对于大型表达式,模式会根据所有内容进行检查,这会变得非常慢.

The final expression is needed so that a**1 simplifies to a instead of NonCommutativeMultiply[a]. You might also need NonCommutativeMultiply[]:=1 so that expressions like 1**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.

上面对0和1的两个定义可以组合并推广到

The above two definitions for 0 and 1 can be combined and generalized to

NonCommutativeMultiply[a___, n_?NumericQ, b___] := n a ** b

分解出表达式中的任何数字项.但这在大型表达式中会更慢,因为检查每个术语以查看其是否为数字.

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.

要将a**a 简化为a^2,您需要类似

To simplify your a**a to a^2, you need something like

NonCommutativeMultiply[a___, b_, b_, c___] := a ** b^2 ** c

或更普遍的

NonCommutativeMultiply[a___, b_^n_., b_^m_., c___] := a ** b^(n + m) ** c

<小时>

(*) 请注意,这只是因为 Mathematica 放置其 DownValues 的默认顺序在这种情况下不一定是最好的.更改顺序,使 NonCommutativeMultiply[a_] 出现在 a___ ** n_?NumericQ ** b___ 之前,则不会生成 NonCommutativeMultiply[]按照规则,您将不需要最后一个模式(除非您以其他方式生成 NonCommutativeMultiply[]).


(*) 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 that NonCommutativeMultiply[a_] comes before a___ ** n_?NumericQ ** b___ then NonCommutativeMultiply[] won't be generated by the rules, and you won't need that last pattern (unless you produce NonCommutativeMultiply[] some other way).

这篇关于重新定义 Mathematica 中的非交换乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆