MathGroup Archive 2010

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

Search the Archive

Re: Working with ppatterns

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113293] Re: Working with ppatterns
  • From: Valeri Astanoff <astanoff at gmail.com>
  • Date: Fri, 22 Oct 2010 01:36:34 -0400 (EDT)
  • References: <i9jq39$ohd$1@smc.vnet.net> <i9mdo8$mmf$1@smc.vnet.net>

On 20 oct, 11:45, Valeri Astanoff <astan... at gmail.com> wrote:
> On 19 oct, 11:58, Themis Matsoukas <tmatsou... 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
>
> Good day,
>
> Here is my solution :
>
> In[1]:= data = Apply[List, Expand[(a[1] + a[2] + a[3] + a[4])^4]] ;
>
> In[2]:= data /. (_.)*a[k1_]^(p1_.)*a[k2_]^(p2_.)*a[k3_]^(p3_.)*
>  a[k4_]^(p4_.) -> {{k1, p1}, {k2, p2}, {k3, p3}, {k4, p4}} /.
>  (_.)*a[k1_]^(p1_.)*a[k2_]^(p2_.)*a[k3_]^(p3_.) ->
>  {{k1, p1}, {k2, p2}, {k3, p3}} /.
>  (_.)*a[k1_]^(p1_.)*a[k2_]^(p2_.) -> {{k1, p1}, {k2, p2}} /.
>  (_.)*a[k1_]^(p1_.) -> {{k1, p1}}
>
> Out[2]= {{{1, 4}}, {{1, 3}, {2, 1}}, {{1, 2}, {2, 2}},
> {{1, 1}, {2, 3}}, {{2, 4}}, {{1, 3}, {3, 1}},
> {{1, 2}, {2, 1}, {3, 1}}, {{1, 1}, {2, 2}, {3, 1}},
> {{2, 3}, {3, 1}}, {{1, 2}, {3, 2}}, {{1, 1}, {2, 1}, {3, 2}},
> {{2, 2}, {3, 2}}, {{1, 1}, {3, 3}}, {{2, 1}, {3, 3}},
> {{3, 4}}, {{1, 3}, {4, 1}}, {{1, 2}, {2, 1}, {4, 1}},
> {{1, 1}, {2, 2}, {4, 1}}, {{2, 3}, {4, 1}},
> {{1, 2}, {3, 1}, {4, 1}}, {{1, 1}, {2, 1}, {3, 1}, {4, 1}},
> {{2, 2}, {3, 1}, {4, 1}}, {{1, 1}, {3, 2}, {4, 1}},
> {{2, 1}, {3, 2}, {4, 1}}, {{3, 3}, {4, 1}},
> {{1, 2}, {4, 2}}, {{1, 1}, {2, 1}, {4, 2}}, {{2, 2}, {4, 2}},
> {{1, 1}, {3, 1}, {4, 2}}, {{2, 1}, {3, 1}, {4, 2}},
> {{3, 2}, {4, 2}}, {{1, 1}, {4, 3}}, {{2, 1}, {4, 3}},
> {{3, 1}, {4, 3}}, {{4, 4}}}
>
> Hope you'll find a simpler way to do it...
>
> --
> Valeri- Masquer le texte des messages pr=E9c=E9dents -
>
> - Afficher le texte des messages pr=E9c=E9dents -


Good day,

Even simpler :

In[1]:= data = Apply[List, Expand[(a[1] + a[2] + a[3] + a[4])^4]];

In[2]:= Transpose@Table[{k, #} & /@
Exponent[data, a[k]], {k, 1, 4}] /. {_, 0} -> Sequence[]

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

--
Valeri


  • Prev by Date: Error with VoronoiDiagram[] function
  • Next by Date: Researchers break speed barrier in solving important class of linear systems
  • Previous by thread: Re: Working with ppatterns
  • Next by thread: Re: Working with ppatterns