Question about OneIdentity
- To: mathgroup at smc.vnet.net
- Subject: [mg88198] Question about OneIdentity
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 28 Apr 2008 04:38:08 -0400 (EDT)
- Organization: University of Bergen
(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).
- Follow-Ups:
- Re: Question about OneIdentity
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Question about OneIdentity