Re: Defining a flat, orderless, one-identical function?
- To: mathgroup at smc.vnet.net
- Subject: [mg28135] Re: Defining a flat, orderless, one-identical function?
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Sat, 31 Mar 2001 02:59:05 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Ralph Benzinger was trying to define a flat, orderless, one-identical function `max', i.e. he wanted the following equivalences to hold: (1) max[a, max[b, c]] == max[a, b, c] (2) max[a, b] == max[b, a] (3) max[a] == a The built-in Plus[] function has exactly these properties. ------------------------ I wanted to do that a few years ago, and Carl Woll game up with the following ingenious solution. In[1]:= ClearAll[max]; SetAttributes[max, {Flat,OneIdentity,Orderless}]; a_max /; Length[Unevaluated[a]]==1 := Identity@@Unevaluated[a] In[4]:= max[a,max[b,c]] Out[4]= max[a,b,c] In[5]:= max[b,a] Out[5]= max[a,b] In[6]:= max[a] Out[6]= a To read a complete discussion of this trick goto my web-site (URL below) then goto the section on Flat. This discussion is at the end of this section and stars with "A warning about the Flat attribute" in bold. -------------------- Regards, Ted Ersek Mathematica tips, tricks have moved to http://www.verbeia.com/mathematica/tips/Tricks.html