MathGroup Archive 1999

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

Search the Archive

Re: Problems with Flat (Again)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15555] Re: [mg15524] Problems with Flat (Again)
  • From: Carl Woll <carlw at fermi.phys.washington.edu>
  • Date: Tue, 26 Jan 1999 13:44:38 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Torben,

This subject has come up in the past, so you might want to search the
archives under Flat.

My solution to this problem goes as follows.

For the singleton case, use the rule

a_f /; Length[Unevaluated[a]]==1 := Extract[Hold[a],{1,1}]

and for multiple arguments, do the usual

f[x_,y_] := ...

so that the whole function definition would be

SetAttributes[f, {Flat, Orderless, OneIdentity}]

a_f /; Length[Unevaluated[a]]==1 := Extract[Hold[a],{1,1}] f[x_,y_] :=
...


I think a function definition of the form

a_f :=

fires before the Flat Attribute does it's thing, and so it doesn't cause
the pattern matcher to go into an infinite loop. Try evaluating f[x]
and f[x,y] using the above rules.

Carl Woll
Dept of Physics
U of Washington

On Mon, 18 Jan 1999, Torben Winther Graversen wrote:

> I apologize for asking about this twice, but the newsserver here didn't
> quite behave the first time I posted during fall '98. Anyway, here's my
> question:
> 
> I want to have an operator, say p[], that has attribute Flat,
> OneIdentity, and Orderless:
> 
> In[1] := SetAttributes[p, {OneIdentity, Flat, Orderless}]
> 
> I also want it to reduce expressions with only one argument to just the
> argument:
> 
> In[2] := p[a_]=a;
> 
> However, if I now enter an expression like p[x,y], this happens:
> 
> In[3] := p[x, y]
> 
> $RecursionLimit::reclim: Recursion depth of 256 exceeded.
> 
> Out[3]= Hold[p[x, y]]
> 
> Looking at the Trace, it seems as though the pattern-matcher matches
> p[a_] with p[p[x,y]] infinitely many times, since p is Flat.
> 
> The only way I've found around this problem is to set things up in the
> opposite order:
> 
> In[1]:= p[a_]=a;
> 
> In[2]:= SetAttributes[p, {OneIdentity, Flat, Orderless}]
> 
> In[3]:= p[x, y]
> 
> Out[3]= p[x, y]
> 
> *** Question 1:
> So my definition will only give the desired result if I enter the rule
> before turning on the attribute?
> 
> I've noticed that Mathematica will let me have the same rule twice, with
> different attributes:
> 
> In[4]:= p[a_]=a;
> 
> In[5]:= p[x, y]
> 
> $RecursionLimit::reclim: Recursion depth of 256 exceeded.
> 
> Out[5]= Hold[p[x, y]]
> 
> In[6]:= ?p
> Global`p
> 
> Attributes[p] = {Flat, OneIdentity, Orderless}
>  
> p[a_] = a
>  
> p[a_] = a
> 
> If I now remove the attributes, the latest defined rule still applies:
> 
> In[6]:= Attributes[p]={};
> 
> In[7]:= p[x,y]
> 
> $RecursionLimit::reclim: Recursion depth of 256 exceeded.
> 
> Out[7]= Hold[p[x, y]]
> 
> *** Question 2:
> So Mathematica doesn't select rules based on which attributes are
> present, but only based on which attributes where present when the rule
> was defined?
> 
> Where can I find more information on this subject?
> 
> Thank you for your help.
> 
> --
> Best regards,
> 
>   Torben Winther Graversen
>   http://www.student.dtu.dk/~immtwg
> 



  • Prev by Date: Re: ? scatterplots with differently sized labels and dots ?
  • Next by Date: Re: Symbolic Derivative of Piecewise Contin Fcn
  • Previous by thread: Problems with Flat (Again)
  • Next by thread: Cross product with Mathematica