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


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

Re: Sum pattern

From Alexei Boulbitch <Alexei.Boulbitch@iee.lu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Sum pattern
Date 2012-10-05 06:41 +0000
Message-ID <k4lveh$d32$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


Hi,

I'm trying to put together a rule whose left hand side is a sum of arbitrar=
y
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?
How can I make this rule work?

Thanks in advance,
Dave Snead

Hi, Dave,

It may be done this way:

f[Table[x[[i, 1]], {i, 1, Length[x]}] /. List -> Plus, x[[1, 2]]]

f[a1 + a2 + a3, s]

or, somewhat shorter, like this:

f[First[List @@@ x], x[[1, 2]]]

f[a1 + a2 + a3, s]

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu



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


Thread

Re: Sum pattern Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2012-10-05 06:41 +0000

csiph-web