Re: Question about OneIdentity
- To: mathgroup at smc.vnet.net
- Subject: [mg88227] Re: [mg88198] Question about OneIdentity
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 29 Apr 2008 06:47:36 -0400 (EDT)
- References: <200804280838.EAA05888@smc.vnet.net>
You can find a very clear explanation of these matters here: http://hilbert.math.hr/arhive/mathgroup/2000/01/0323.html Note also that, although, as you note: Attributes[f] = Attributes[Times]; MatchQ[1, f[1]] False but MatchQ[f[1], f[f[1]]] True For the explanation see the above link. Andrzej Kozlowski On 28 Apr 2008, at 17:38, Szabolcs Horv=E1t wrote: > > (Scroll down for the actual question) > > I never really understood Flat and OneIdentity, and unfortunately > documentation about them is scarce. > > =46rom 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). >
- References:
- Question about OneIdentity
- From: Szabolcs Horvát <szhorvat@gmail.com>
- Question about OneIdentity