Mathematica 中表达式开头的非交换乘法和负系数 [英] Noncommutative Multiplication and Negative coeffcients at the Beginning of an Expression in Mathematica

查看:34
本文介绍了Mathematica 中表达式开头的非交换乘法和负系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些非常亲切的 stackoverflow 贡献者的帮助下在这篇文章中,我有以下是 Mathematica 中 NonCommutativeMultiply (**) 的新定义:

With the help of some very gracious stackoverflow contributors in this post, I have the following new definition for NonCommutativeMultiply (**) in Mathematica:

<代码>取消保护[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply]
NonCommutativeMultiply[] := 1
NonCommutativeMultiply[___, 0, ___] := 0
NonCommutativeMultiply[a___, 1, b___] := a ** b
NonCommutativeMultiply[a___, i_Integer, b___] := i*a ** b
NonCommutativeMultiply[a_] := a
c___ ** 下标[a_, i_] ** 下标[b_, j_] ** d___/;我>j :=
c ** 下标[b, j] ** 下标[a, i] ** d
SetAttributes[NonCommutativeMultiply, {OneIdentity, Flat}]
Protect[NonCommutativeMultiply];

这个乘法很棒,但是,它不处理表达式开头的负值,即,
a**b**c + (-q)**c**a
应该简化为
a**b**c - q**c**a
它不会.

This multiplication is great, however, it does not deal with negative values at the beginning of an expression, i.e.,
a**b**c + (-q)**c**a
should simplify to
a**b**c - q**c**a
and it will not.

在我的乘法中,变量 q(和任何整数定标器)是可交换的;我仍在尝试编写 SetCommutative 函数,但没有成功.我并不迫切需要 SetCommutative,它会很好.

In my multiplication, the variable q (and any integer scaler) is commutative; I am still trying to write a SetCommutative function, without success. I am not in desperate need of SetCommutative, it would just be nice.

如果我能够将所有 q's 拉到每个表达式的开头也会很有帮助,即:
a**b**c + a**b**q**c**a
应该简化为:
a**b**c + q**a**b**c**a
同样,结合这两个问题:
a**b**c + a**c**(-q)**b
应该简化为:
a**b**c - q**a**c**b

It would also be helpful if I were able to pull all of the q's to the beginning of each expression, i.e.,:
a**b**c + a**b**q**c**a
should simplify to:
a**b**c + q**a**b**c**a
and similarly, combining these two issues:
a**b**c + a**c**(-q)**b
should simplify to:
a**b**c - q**a**c**b

目前我想弄清楚如何处理表达式开头的这些负变量,以及如何拉取q的(-q)的 到前面如上.我已经尝试使用 ReplaceRepeated (\\.) 处理这里提到的两个问题,但到目前为止我没有成功.

At the current time, I would like to figure out how to deal with these negative variables at the beginning of an expression and how to pull the q's and (-q)'s to the front as above. I have tried to deal with the two issues mentioned here using ReplaceRepeated (\\.), but so far I have had no success.

欢迎所有想法,谢谢...

All ideas are welcome, thanks...

推荐答案

这样做的关键是要意识到 Mathematica 将 ab 表示为 a+((-1)*b),正如你从

The key to doing this is to realize that Mathematica represents a-b as a+((-1)*b), as you can see from

In[1]= FullForm[a-b]
Out[2]= Plus[a,Times[-1,b]]

对于问题的第一部分,您只需添加以下规则:

For the first part of your question, all you have to do is add this rule:

NonCommutativeMultiply[Times[-1, a_], b__] := - a ** b

或者您甚至可以从任何位置捕捉标志:

or you can even catch the sign from any position:

NonCommutativeMultiply[a___, Times[-1, b_], c___] := - a ** b ** c

更新 -- 第 2 部分. 将标量放在前面的一般问题是,当前规则中的模式 _Integer 只会发现明显是整数的东西.它甚至不会发现 q 是像 Assuming[{Element[q, Integers]}, a**q**b] 这样的结构中的整数.
要实现这一点,您需要检查假设,将这个过程放入全局转换表中的成本可能很高.相反,我会编写一个可以手动应用的转换函数(并且可能从全局表中删除当前规则).像这样的事情可能会奏效:

Update -- part 2. The general problem with getting scalars to front is that the pattern _Integer in your current rule will only spot things that are manifestly integers. It wont even spot that q is an integer in a construction like Assuming[{Element[q, Integers]}, a**q**b].
To achieve this, you need to examine assumptions, a process that is probably to expensive to be put in the global transformation table. Instead I would write a transformation function that I could apply manually (and maybe remove the current rule form the global table). Something like this might work:

NCMScalarReduce[e_] := e //.  {
    NonCommutativeMultiply[a___, i_ /; Simplify@Element[i, Reals],b___] 
    :> i a ** b
}

上面使用的规则使用Simplify来显式查询假设,您可以通过分配给$Assumptions来全局设置或使用Assuming在本地设置:

The rule used above uses Simplify to explicitly query assumptions, which you can set globally by assigning to $Assumptions or locally by using Assuming:

Assuming[{q \[Element] Reals},
  NCMScalarReduce[c ** (-q) ** c]] 

返回-q c**c.

HTH

这篇关于Mathematica 中表达式开头的非交换乘法和负系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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