Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11934

Re: Sum pattern

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Sum pattern
Date 2012-10-04 03:38 +0000
Message-ID <k4j0bq$7ll$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


On 10/3/12 at 3:07 AM, dsnead6@charter.net (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?

Because

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?

Try:

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

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

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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Sum pattern Bill Rowe <readnews@sbcglobal.net> - 2012-10-04 03:38 +0000

csiph-web