Re: Full expansion with a mixture of Times and NonCommutativeMultiply
- To: mathgroup at smc.vnet.net
- Subject: [mg103542] Re: Full expansion with a mixture of Times and NonCommutativeMultiply
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sun, 27 Sep 2009 07:32:29 -0400 (EDT)
- References: <h9kpqa$n0g$1@smc.vnet.net>
ChrisL wrote: > Dear all, > I am using a non-associative, non-commutative product ** > (NonCommutativeMultiply[]). I have a procedure which builds long > polynomials that use ** and the usual Times. What I need eventually is > to extract each of the mononials (parts of the final expression that > do not contain any Plus[]) for some further processing. > I defined NonCommutativeMultiply[] very simply like this: > Unprotect[NonCommutativeMultiply]; > ClearAttributes[NonCommutativeMultiply, Flat]; (* forcing non- > associativity *) > 0 ** x_ := 0; > x_ ** 0 := 0; > 1 ** x_ := x; > x_ ** 1 := x; > (m_Integer*x_) ** y_ := m*(x ** y); > x_ ** (m_Integer*y_) := m (x ** y); > Protect[NonCommutativeMultiply]; > > And getting the full expansion seems to work fine: > Distribute[2 (3 a1) ** (5 a2)] > Distribute[a1 ** (2 a2 + a3 ** a4)] > yields > 30 a1 ** a2 > 2 a1 ** a2 + a1 ** (a3 ** a4) > This is great: I can pick up each mononial with Table[expr[[i]], > {i,Length[expr]}] > > Unfortunately, the expansion seems to stop at the second level. Thus: > Distribute[((2 a2 + a3 ** a4 ) ** a6) ** a7] > Distribute[(2 a2 + a3 ** a4 ) ** (3 a6)] > yields > ((2 a2 + a3 ** a4) ** a6) ** a7 > 3 (2 a2 + a3 ** a4) ** a6 > > when I need: > 2 (a2**a6)**a7 + ((a3**a4)**a6)**a7 > and > 6 a2**a6 + 3 (a3**a4)**a6 > > Is there any way achieve this? Or do I need to write the full > expansion algorithm myself? Note that the final expression will be > much longer - about 30'000 mononials. > > thank you very much in advance! > Cheers. > Forget about Distribute, and perform the transformation using a set of replacement rules exhaustively applied: expansionRules = {(a_ + b_) ** c_ -> a ** c + b ** c, c_ ** (a_ + b_) -> c ** a + c ** b, c_ (a_ + b_) -> c a + c b} In[26]:= (2 a2 + a3 ** a4) ** (3 a6) //. expansionRules Out[26]= 6 a2 ** a6 + 3 a3 ** a4 ** a6 In[25]:= ((2 a2 + a3 ** a4) ** a6) ** a7 //. expansionRules Out[25]= 2 a2 ** a6 ** a7 + a3 ** a4 ** a6 ** a7 I have not tried this on the size of problem you suggest, but I would certainly start with this approach and test if it is adequate. Once you have the expression fully expanded - say ans = 2 a2 ** a6 ** a7 + a3 ** a4 ** a6 ** a7 just replace the Plus with List, taking care to deal with the degenerate single item case: If[Head[ans] === Plus, ans = List @@ ans] BTW NonCommutativeMultiply is a relic from the days when Mathematica only used the ASCII character set, \[CircleTimes] will look a lot more readable! David Bailey http://www.dbaileyconsultancy.co.uk