MathGroup Archive 2012

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

Search the Archive

Re: Sum pattern

  • To: mathgroup at
  • Subject: [mg128276] Re: Sum pattern
  • From: Bill Rowe <readnews at>
  • Date: Wed, 3 Oct 2012 23:38:15 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:

On 10/3/12 at 3:07 AM, dsnead6 at (Dave Snead) wrote:

>I'm trying to put together a rule whose left hand side is a sum of
>arbitrary length whose elements all have the same head f.

>For example:

>In[4]:= x = f[a1, s] + f[a2, s] + f[a3, s]

>Out[4]= f[a1, s] + f[a2, s] + f[a3, s]

>In[6]:= y = f[First /@ x, s]

>Out[6]= f[a1 + a2 + a3, s]

>which is what I want.

>However when I turn this into a rule

>In[7]:= z = x /. (p : Plus[__f]) -> f[First /@ p, s]

>Out[7]= f[f[a1, s], s] + f[f[a2, s], s] + f[f[a3, s], s]

>Why isn't z equal to y?


In[2]:= x = f[a1, s] + f[a2, s] + f[a3, s];
MatchQ[x, (p : Plus[__f])]

Out[3]= False

>How can I make this rule work?


In[4]:= x /. Plus[y___] :> f[First /@ y, s]

Out[4]= f(a1+a2+a3,s)

Note the use of RuleDelayed :> rather than Rule ->

  • Prev by Date: Re: How to lock down a Dynamic object in a report
  • Next by Date: Re: Multi Thread in Mathematica (not multi core)
  • Previous by thread: Sum pattern
  • Next by thread: Re: Sum pattern