MathGroup Archive 2001

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

Search the Archive

Defining a flat, orderless, one-identical function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28046] Defining a flat, orderless, one-identical function?
  • From: Ralph Benzinger <mma-l at endlos.net>
  • Date: Thu, 29 Mar 2001 03:24:12 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hello:

I've been trying to define a flat, orderless, one-identical function
`max', i.e. I want 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.

My original definition was

In[1]:=
	ClearAll[max]
	SetAttributes[max, {Flat, OneIdentity,
			    Orderless, NumericFunction}]
	max[a_?NumericQ, b_?NumericQ] := Max[a, b]
	max[a_ b_, a_ c_] := a max[b, c]

That works fine except for one-identity:

In[5]:=
	max[x]
Out[5]=
	max[x]

For some reason I cannot quite fathom, the developers of
Mathematica decided to heed the OneIdentity attribute only in
pattern matching but not during evaluation.  Anyway, to fix this,
I naively added the missing rule

In[7]:=
	max[a_] := a

This works for max[x], but suddenly we have other problems:

In[12]:=
	max[x, x]
	$RecursionLimit::"reclim": "Recursion depth of 256 exceeded."
	General::"stop": "Further output of $RecursionLimit::reclim will
	be suppressed during this calculation."
Out[12]=
	Hold[max[x, x]]

Apparently, the OneIdentity pattern matching gets in the way.  While
I'm not sure about the exact matching/evaluation sequence (Trace[]
won't tell you), I assume that max[x,x] matches max[max[x],max[x]]
which eventually reduces to back to max[x,x].

So let's try without OneIdentity:

In[13]:=
	ClearAll[max]
	SetAttributes[max, {Flat, Orderless}]
	max[a_?NumericQ, b_?NumericQ] := Max[a, b]
	max[a_ b_, a_ c_] := a max[b, c]
	max[a_] := a

Alas, no use:

In[18]:=
	max[x]
	$IterationLimit::"itlim": "Iteration limit of 4096 exceeded."
	General::"stop": "Further output of $IterationLimit::itlim will
	be suppressed during this calculation.
Out[18]=
	Hold[max[x]]

Note, however, that we now have a different kind of infinite loop.

Does any of the Mathematica experts out there know how to define a
one-identical function properly?  Plus[] can do it, so there must be a
way.  I'm surprised that something so fundamental should be so hard to
define.

Regards,
Ralph

-- 
Ralph Benzinger          "This is my theory, it is mine, I own it,
Cornell University        and what it is, too." -- Ann Elk (Mrs.)


  • Prev by Date: Re: Recursive Calculation of Gaussian-Like Sequence
  • Next by Date: Re: Exponential fit question.
  • Previous by thread: Re: Unconventional Max[] behavior
  • Next by thread: Re: Defining a flat, orderless, one-identical function?