Re: Full expansion with a mixture of Times and NonCommutativeMultiply
- To: mathgroup at smc.vnet.net
- Subject: [mg103527] Re: Full expansion with a mixture of Times and NonCommutativeMultiply
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Sun, 27 Sep 2009 07:29:37 -0400 (EDT)
- References: <h9kpqa$n0g$1@smc.vnet.net>
On 2009.09.26. 13:18, 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. > There will most likely be better answers, but you can use this as a starting point: e = ((2 a2 + a3 ** a4 ) ** a6) ** a7 e //. expr : ((_ ** Plus[_, __]) | (Plus[_, __] ** _)) :> Distribute[expr] One warning though: Removing Flat from NonCommutativeMultiply might not be such a good idea because a ** b ** c is still parsed as NonCommutativeMultiply[a,b,c]. Since parens are required around every operand pair, having an operator notation (**) isn't a big advantage anyway.