Re: Working with ppatterns

• To: mathgroup at smc.vnet.net
• Subject: [mg113254] Re: Working with ppatterns
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Wed, 20 Oct 2010 04:10:49 -0400 (EDT)

```Themis Matsoukas wrote:
> I would like to create a pattern that recognizes multinomial terms of the form
>
> A (a[2]^x2) (a[5]^x5) (a[6]^x6)...
>
> Ultimately, I want to identify all the indices i (as in a[i]) and exponents xi that appear in a given term so that the above term would produce a list {{2, x2}, {5, x5}, {6, x6}, etc}.
>
> I have hard time writing a rule that is general enough to work with any number of terms in the products and also one that will work whether the prefactor or any of the exponents of the multinomial is 1. I explain all this with the example below:
>
> data=Apply[List,Expand[(a[1]+a[2]+a[3]+a[4])^4]];
> Column[data];
>
> AB=Cases[data,X_ a[i1_]^j1_ a[i2_]^j2_];
> ABindices=Cases[data,X_ a[i1_]^j1_ a[i2_]^j2_->{{i1,j1}, {i2, j2}}];
> Column[Transpose[{AB, ABindices}]]
>
> {6 a[1]^2 a[2]^2,{{1,2},{2,2}}}
> {6 a[1]^2 a[3]^2,{{1,2},{3,2}}}
> {6 a[2]^2 a[3]^2,{{2,2},{3,2}}}
> {6 a[1]^2 a[4]^2,{{1,2},{4,2}}}
> {6 a[2]^2 a[4]^2,{{2,2},{4,2}}}
> {6 a[3]^2 a[4]^2,{{3,2},{4,2}}}
>
> Here, data is a made up list of multinomials, AB uses a rule to extract double products, and ABindices produces a list of indices and exponents, as I wanted. However:
>
> 1. my rule does misses terms like "4 a[1] a[3]^3" because the exponent of a[1] is 1; a separate rule would have to be written.
>
> 2. If the prefactor of a term is 1, the term will not be selected unless X_ is removed from the rule.
>
> In summary, I would have to write separate rules depending on the number of terms in the product, and also depending on whether X or any of the exponents is 1 or not.
>
> Is there a way to write the rule so that ABindices would contain the indices and exponents of *all* terms in the original data?
>
> Themis
>

Could use the internal function DistributedTermsList in GroebnerBasis`
context, to get at the exponent vectors (along with the corresponding
coefficients). It is then a matter of list manipulation to intertwine
the numreals denoting the various variables.

In[31]:= data = Expand[(a[1] + a[2] + a[3] + a[4])^4];
vars = Union[Cases[data, a[_], Infinity]];

In[33]:= dtlist = GroebnerBasis`DistributedTermsList[data, vars]

Out[33]= {{{{4, 0, 0, 0}, 1}, {{3, 1, 0, 0}, 4}, {{3, 0, 1, 0},
4}, {{3, 0, 0, 1}, 4}, {{2, 2, 0, 0}, 6}, {{2, 1, 1, 0},
12}, {{2, 1, 0, 1}, 12}, {{2, 0, 2, 0}, 6}, {{2, 0, 1, 1},
12}, {{2, 0, 0, 2}, 6}, {{1, 3, 0, 0}, 4}, {{1, 2, 1, 0},
12}, {{1, 2, 0, 1}, 12}, {{1, 1, 2, 0}, 12}, {{1, 1, 1, 1},
24}, {{1, 1, 0, 2}, 12}, {{1, 0, 3, 0}, 4}, {{1, 0, 2, 1},
12}, {{1, 0, 1, 2}, 12}, {{1, 0, 0, 3}, 4}, {{0, 4, 0, 0},
1}, {{0, 3, 1, 0}, 4}, {{0, 3, 0, 1}, 4}, {{0, 2, 2, 0},
6}, {{0, 2, 1, 1}, 12}, {{0, 2, 0, 2}, 6}, {{0, 1, 3, 0},
4}, {{0, 1, 2, 1}, 12}, {{0, 1, 1, 2}, 12}, {{0, 1, 0, 3},
4}, {{0, 0, 4, 0}, 1}, {{0, 0, 3, 1}, 4}, {{0, 0, 2, 2},
6}, {{0, 0, 1, 3}, 4}, {{0, 0, 0, 4}, 1}}, {a[1], a[2], a[3], a[4]}}

In[34]:= exponvecs = dtlist[[1, All, 1]]

Out[34]= {{4, 0, 0, 0}, {3, 1, 0, 0}, {3, 0, 1, 0}, {3, 0, 0, 1}, {2,
2, 0, 0}, {2, 1, 1, 0}, {2, 1, 0, 1}, {2, 0, 2, 0}, {2, 0, 1,
1}, {2, 0, 0, 2}, {1, 3, 0, 0}, {1, 2, 1, 0}, {1, 2, 0, 1}, {1, 1,
2, 0}, {1, 1, 1, 1}, {1, 1, 0, 2}, {1, 0, 3, 0}, {1, 0, 2, 1}, {1,
0, 1, 2}, {1, 0, 0, 3}, {0, 4, 0, 0}, {0, 3, 1, 0}, {0, 3, 0,
1}, {0, 2, 2, 0}, {0, 2, 1, 1}, {0, 2, 0, 2}, {0, 1, 3, 0}, {0, 1,
2, 1}, {0, 1, 1, 2}, {0, 1, 0, 3}, {0, 0, 4, 0}, {0, 0, 3, 1}, {0,
0, 2, 2}, {0, 0, 1, 3}, {0, 0, 0, 4}}

In[35]:= Map[Partition[Riffle[dtlist[[2, All, 1]], #], 2] &, exponvecs]

Out[35]= {{{1, 4}, {2, 0}, {3, 0}, {4, 0}}, {{1, 3}, {2, 1}, {3,
0}, {4, 0}}, {{1, 3}, {2, 0}, {3, 1}, {4, 0}}, {{1, 3}, {2, 0}, {3,
0}, {4, 1}}, {{1, 2}, {2, 2}, {3, 0}, {4, 0}}, {{1, 2}, {2,
1}, {3, 1}, {4, 0}}, {{1, 2}, {2, 1}, {3, 0}, {4, 1}}, {{1, 2}, {2,
0}, {3, 2}, {4, 0}}, {{1, 2}, {2, 0}, {3, 1}, {4, 1}}, {{1,
2}, {2, 0}, {3, 0}, {4, 2}}, {{1, 1}, {2, 3}, {3, 0}, {4, 0}}, {{1,
1}, {2, 2}, {3, 1}, {4, 0}}, {{1, 1}, {2, 2}, {3, 0}, {4,
1}}, {{1, 1}, {2, 1}, {3, 2}, {4, 0}}, {{1, 1}, {2, 1}, {3, 1}, {4,
1}}, {{1, 1}, {2, 1}, {3, 0}, {4, 2}}, {{1, 1}, {2, 0}, {3,
3}, {4, 0}}, {{1, 1}, {2, 0}, {3, 2}, {4, 1}}, {{1, 1}, {2, 0}, {3,
1}, {4, 2}}, {{1, 1}, {2, 0}, {3, 0}, {4, 3}}, {{1, 0}, {2,
4}, {3, 0}, {4, 0}}, {{1, 0}, {2, 3}, {3, 1}, {4, 0}}, {{1, 0}, {2,
3}, {3, 0}, {4, 1}}, {{1, 0}, {2, 2}, {3, 2}, {4, 0}}, {{1,
0}, {2, 2}, {3, 1}, {4, 1}}, {{1, 0}, {2, 2}, {3, 0}, {4, 2}}, {{1,
0}, {2, 1}, {3, 3}, {4, 0}}, {{1, 0}, {2, 1}, {3, 2}, {4,
1}}, {{1, 0}, {2, 1}, {3, 1}, {4, 2}}, {{1, 0}, {2, 1}, {3, 0}, {4,
3}}, {{1, 0}, {2, 0}, {3, 4}, {4, 0}}, {{1, 0}, {2, 0}, {3,
3}, {4, 1}}, {{1, 0}, {2, 0}, {3, 2}, {4, 2}}, {{1, 0}, {2, 0}, {3,
1}, {4, 3}}, {{1, 0}, {2, 0}, {3, 0}, {4, 4}}}

The result above appears to capture each monomial in the way you have
specified.

Daniel Lichtblau
Wolfram Research

```

• Prev by Date: Re: Working with ppatterns
• Next by Date: Re: Table of tables
• Previous by thread: Re: Working with ppatterns
• Next by thread: Re: Working with ppatterns