MathGroup Archive 2001

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

Search the Archive

RE: Re: slot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28073] RE: [mg28050] Re: slot
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Fri, 30 Mar 2001 04:12:25 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Bob,

just let me shortly add 

With a definition (which results in a rewrite rule) you can introduce
patterns and conditions for the arguments, which often is very 
convenient, esp. when when you combine multiple definitions to one 
"function". Also you may define default values for arguments.

In[78]:=
g[(a_)?NumericQ, b_] := a*b; 
g[a_List, b_List] := a . b; 

In[80]:= g[3, {1, 2}]
Out[80]= {3, 6}
In[81]:= g[{3, 3}, {1, 2}]
Out[81]= 9
In[82]:= g[{1, 2}, 3]
Out[82]= g[{1, 2}, 3]
In[83]:= g[a, {1, 2}]
Out[83]= g[a, {1, 2}]
In[84]:= a*{1, 2}
Out[84]= {a, 2 a}

At the other side, with Functions you may have an indefinite 
number of arguments (##) and you can call them with more 
arguments than defined:

In[77]:= Function[{x, y}, x * y][2, 3, 4]
Out[77]= 6

etc., there is more to be said. It is certainly rewarding 
to read the Book for details!

The reasons to do so or so, have to be given from your 
application. If you just "want to go forth and multiply", 
then just write

In[85]:= 1 * 2 * 3
Out[85]= 6

In your example, you effectively have restricted the number
of arguments of Times to three. Your have to decide if this
is of value.

-- Hartmut

> -----Original Message-----
> From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> Sent: Thursday, March 29, 2001 10:24 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg28073] [mg28050] Re: slot
> 
> 
> Bob,
> One reason is convenience: in the middle of some progam we can use
> Times[#1,#2,#3]& (or Times[##]& or Function[{x,y,z}, x y z])
>  without needing to make a definition like those you give for 
> g2 and g3.
> 
> There is also some speed advantage in avoiding having to look up a
> definition.
> 
> g2[x_,y_,z_]:=Times[x,y,z]
> g3:=Times[#1,#2,#3]&
> 
> Do[g2[2,3,4],{10000}]//Timing
> 
>         {0.88 Second,Null}
> 
> Do[g3[2,3,4],{10000}]//Timing
> 
>         {0.66 Second,Null}
> 
> Do[Function[{x,y,z},x *y *z][2,3,4],{10000}]//Timing
> 
>         {0.77 Second,Null}
> 
> Do[Times[#1,#2,#3]&[2,3,4],{10000}]//Timing
> 
>         {0.44 Second,Null}
> 
> Do[Times[##]&[2,3,4],{10000}]//Timing
> 
>         {0.55 Second,Null}
> --
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
> 
> "Robert-Lewis HYDE" <bobhyde at wanadoo.fr> wrote in message
> news:99s682$pbr at smc.vnet.net...
> > hello,
> > For those who never dared ask.
> >
> > g2[x_,y_,z_]:=Times[x,y,z]
> >
> > g3:=Times[#1,#2,#3]&
> >
> > g2[1,2,3]===g3[1,2,3]
> >
> > True
> >
> > Why or when should we go in for the g3 format ?
> >
> > It confuses us ordinary folks (me) who just want to go 
> forth and multiply.
> >
> > Bob
> >
> >
> 
> 
> 



  • Prev by Date: RE: Row spacing in GraphicsArray
  • Next by Date: Plotting a function of x and y
  • Previous by thread: Re: slot
  • Next by thread: Eigenvalues of a Special Matrix