Re: Controlling function arguments
- To: mathgroup at smc.vnet.net
- Subject: [mg48828] Re: Controlling function arguments
- From: "Dr. Wolfgang Hintze" <weh at snafu.de>
- Date: Fri, 18 Jun 2004 02:12:50 -0400 (EDT)
- References: <carkcp$r9g$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Virgil, the function NumberVectorQ[x] gives True if both x is a vector and if all components of x are numbers, and False otherwise. It is a good example of two basic list operations in Mathematica, Map and Apply. In[1]:= NumberVectorQ[x_] := VectorQ[x] && And @@ NumberQ /@ x; Example In[29]:= x = {1, 3, 2}; In[28]:= VectorQ[x] Out[28]= True The next expression gives True if all components of x are numbers And @@ NumberQ /@ x Out[20]= True Let's analyse it step by step going from right to left: step 1) f/@x (which is a shortcut for Map[f,x]) applies function f to every component of x. Hence In[21]:= y = Map[NumberQ, x] Out[21]= {True, True, True} Step 2) f@@y (which is a shortcut for Apply[f,y]) sets the list y as a parameter list of function f. Hence In[22]:= z = Apply[And, y] Out[22]= True and, finally In[23]:= VectorQ[x] && z Out[23]= True functionx[k_Integer?Positive, v_?NumberVectorQ,...] defines a function f with a parameter k that must be a positive integer, and a parameter v that must be a vector having only numbers as components. If the parameters do not all fit to these conditions the function f is returned unevaluated. Hope this helps, Wolfgang Virgil Stokes wrote: > I found the following Mathematica code: > > NumberVectorQ[x_] := VectorQ[x] && And @@ NumberQ /@ x; > functionx[k_Integer?Positive, v_?NumberVectorQ, w_?NumberVectorQ, > q_?NumberVectorQ] := ..... > > where, I have left out the body of this function (functionx). What does > the first line actually accomplish? > > --V. Stokes > > > > >