MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Full expansion with a mixture of Times and

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103541] Re: [mg103517] Full expansion with a mixture of Times and
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 27 Sep 2009 07:32:18 -0400 (EDT)
  • References: <200909261012.GAA23367@smc.vnet.net>

Hi Crhis,

I think you are looking for something like this:

ClearAll[distributeAll];
distributeAll[expr_] :=
  expr //. x : HoldPattern[a_ ** Plus[b__] | Plus[b__] ** a_] :>
    Distribute[x];

In[1] =
distributeAll[((2 a2 + a3 ** a4) ** a6) ** a7]

Out[1] =
2 (a2 ** a6) ** a7 + ((a3 ** a4) ** a6) ** a7

In[2] =
distributeAll[(2 a2 + a3 ** a4) ** (3 a6)]

Out[2] =
3 (2 a2 ** a6 + (a3 ** a4) ** a6)

Perhaps the name of the function (distributeAll) could
have been chosen better.

Regards,
Leonid



On Sat, Sep 26, 2009 at 3:12 AM, ChrisL <chris.ladroue at gmail.com> 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.
>
>


  • Prev by Date: Re: Full expansion with a mixture of Times and NonCommutativeMultiply
  • Next by Date: Re: Re: Showing the Current Value of a Slider in Manipulate
  • Previous by thread: Full expansion with a mixture of Times and NonCommutativeMultiply
  • Next by thread: Re: Re: Full expansion with a mixture of Times