Re: Working with ppatterns

*To*: mathgroup at smc.vnet.net*Subject*: [mg113242] Re: Working with ppatterns*From*: Bob Hanlon <hanlonr at cox.net>*Date*: Wed, 20 Oct 2010 04:08:08 -0400 (EDT)

Include defaults http://reference.wolfram.com/mathematica/ref/Default.html extract[expr_] := expr /. { c_. a[n1_]^x1_. a[n2_]^x2_. :> {{n1, x1}, {n2, x2}}, c_. a[n1_]^x1_. :> {{n1, x1}}} data = Apply[List, Expand[(a[1] + a[2] + a[3] + a[4])^4]]; extract[data] However, if you only want double products extract[expr_] := expr /. { c_. a[n1_]^x1_. a[n2_]^x2_. :> {{n1, x1}, {n2, x2}}, c_. a[n1_]^x1_. :> Sequence[]} extract[data] Bob Hanlon ---- Themis Matsoukas <tmatsoukas at me.com> 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