Re: Question about OneIdentity
- To: mathgroup at smc.vnet.net
- Subject: [mg88242] Re: Question about OneIdentity
- From: dh <dh at metrohm.ch>
- Date: Tue, 29 Apr 2008 06:50:18 -0400 (EDT)
- References: <fv42gb$5r2$1@smc.vnet.net>
Hi Szabolcs,
OneIdentity is not easily explained and the writer seems to have taken a
shortcut. A good description from Ted Ersek can be found here:
Ted Ersek
hope this helps, Daniel
Szabolcs Horvát wrote:
> (Scroll down for the actual question)
>
> I never really understood Flat and OneIdentity, and unfortunately
> documentation about them is scarce.
>
> From the docs:
>
> """
> OneIdentity is an attribute that can be assigned to a symbol f to
> indicate that f[x], f[f[x]], etc. are all equivalent to x for the
> purpose of pattern matching.
>
> OneIdentity has an effect only if f has attribute Flat.
> """
>
> ** Some comments:
>
> There is also an example, listing the Attributes of Times and showing
> that Times[a] evaluates to a. However, this is misleading because this
> behaviour cannot be caused by Times's attributes:
>
> In[1]:= Attributes[f] = Attributes[Times]
> Out[1]= {Flat, Listable, NumericFunction,
> OneIdentity, Orderless, Protected}
>
> In[2]:= f[a]
> Out[2]= f[a]
>
> If we assign the same attributes to f, f[a] will not evaluate to a. Was
> the technical writer also confused, or is the example supposed to
> illustrate something different than what I understood?
>
>
> ** And now the actual question:
>
> According to the text in the docs (f[x] is considered equivalent to x in
> pattern matching) I would expect
>
> MatchQ[1, f[1]]
>
> to give True after evaluating SetAttributes[f, {Flat, OneIdentity}].
> But it gives False.
>
>
> ** The application:
>
> This came up in the following application:
>
> fun[HoldPattern@Plus[terms__]] := doSomething[{terms}]
>
> This function should handle a single term, too. Of course, there are
> workarounds, but I couldn't come up with anything as simple as the
> pattern above (which unfortunately does not work).
>