MathGroup Archive 2006

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

Search the Archive

Re: Re: Re: Taking either a sequence or a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63617] Re: Re: Re: Taking either a sequence or a list
  • From: Maxim <m.r at inbox.ru>
  • Date: Sat, 7 Jan 2006 03:13:50 -0500 (EST)
  • References: <dnm29m$89q$1@smc.vnet.net> <dnopf9$2gp$1@smc.vnet.net> <dnrd16$kqf$1@smc.vnet.net> <200512231008.FAA25950@smc.vnet.net> <dp05eq$g3b$1@smc.vnet.net> <200512311140.GAA28014@smc.vnet.net> <dpg0vi$plt$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Wed, 4 Jan 2006 08:28:34 +0000 (UTC), Oyvind Tafjord  
<tafjord at wolfram.com> wrote:

> ----- Original Message -----
> From: "Maxim" <m.r at inbox.ru>
To: mathgroup at smc.vnet.net
>> In[1]:= pset = Module[{f},
>>   Attributes[f] = {Flat, Orderless};
>>   ReplaceList[f @@ #, f[s___, s2___] -> {s}]
>>   ]&;
>>
>> In[2]:= pset[{1, 2, 3}]
>>
>> Out[2]= {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
>>
>> But should it work this way? Why doesn't it generate the list {2, 1} as
>> well?
>
> This is yet another issue.  The behavior is an important optimization to
> save testing a lot of redundant possible matches. It does mean that only  
> the
> sorted version of each sequence will be tried, but this is consistent  
> with
> the general Orderless nature of the function involved.
>

>
> You probably noticed that if you remove the Flat attribute, you'll get  
> many
> more results. This optimization was for unknown reasons not there for  
> this
> case (leading to slowness bugs being reported). For the next version,  
> only
> sorted sequences will be tried in this case as well.
>

>
> For a Flat+OneIdentity head f, a _ pattern will try things like 1,  
> f[1,2],
> f[1,2,3], etc. On the other hand a __ pattern will try things like 1,
> Sequence[1,2], Sequence[1,2,3]. I agree that writing up a more complete
> documentation of these subtleties would be a good idea.
>

That's certainly interesting, thank you for the clarifications. It seems  
to imply that with the optimization which you mentioned the following  
function won't generate all permutations of a given list:

In[1]:= perm = Module[{f},
   Attributes[f] = Orderless;
   ReplaceList[f @@ #, f[s__] -> {s}]
   ]&;

In[2]:= perm[{1, 2, 3}]

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

Mathematica 6 will simply return {{1, 2, 3}}, is that correct?

Also I think there are some other vague points related to ReplaceAll:

In[3]:= Block[{f}, Attributes[f] = Flat;
   f[1, 2, 3] /. f[x__, y_] -> {x, y}]

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

In[4]:= Block[{f}, Attributes[f] = {Flat, Orderless};
   f[1, 2, 3] /. f[x__, y_] -> {x, y}]

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

If Mathematica tries to regroup or reorder the arguments only at the  
places corresponding to Blank in the pattern, not BlankSequence, that  
explains why x is matched to 1, not f[1]. But where does the outer f in  
the second example come from?

Besides, since a replacement such as f[g[a], g[b], 1] /. f[(_g)..] -> 0  
finds a match (when f is Flat), we cannot just say that Flat doesn't try  
substitutions for BlankSequence or Repeated -- ReplaceAll still tries to  
regroup the arguments even if there isn't a Blank pattern present, so I'm  
not sure how to formulate the general rule that would cover both cases.

Maxim Rytin
m.r at inbox.ru


  • Prev by Date: Another question: How to add some assumptions to this Integrand?
  • Next by Date: Re: Puzzle Challenge
  • Previous by thread: Re: Re: Re: Taking either a sequence or a list
  • Next by thread: Re: Re: Re: Re: Taking either a sequence or a list