Re: Test function argument with VectorQ and NumericQ

• To: mathgroup at smc.vnet.net
• Subject: [mg107327] Re: Test function argument with VectorQ and NumericQ
• From: Simon <simonjtyler at gmail.com>
• Date: Tue, 9 Feb 2010 02:46:02 -0500 (EST)
• References: <hkp1na\$7fh\$1@smc.vnet.net>

```Hi Oliver,

I've provided a few examples below in order of my preference (but
apparently not quite in order of speed):

In[1]:= f[{x__?NumericQ}]:="ok"

In[2]:= g[{x_?NumericQ..}]:="ok"

In[3]:= h[x_?(VectorQ[#,NumericQ]&)]:="ok"

In[4]:= i[x_List]/;VectorQ[x,NumericQ]:="ok"

In[5]:= NumericVectorQ[x_]:=VectorQ[x,NumericQ]
In[6]:= j[x_?NumericVectorQ]:="ok"

In[7]:= k[x_List]/;NumericQ[Total[x]]:="ok"

In[8]:= l[x_List]/;And@@(NumericQ/@x):="ok"

In[9]:= Table[StringQ/@{fn[{1,2}],fn[{.5,\[Pi]}],fn[{x,
1}],fn[{1,y}],fn[{x,y}]} == {True,True,False,False,False},{fn,
{f,g,h,i,j,k,l}}]
Out[9]= {True,False,True,True,True,True,True}

In[10]:= Table[{fn,First@Timing[Table[fn[RandomChoice[{1,2,\[Pi],.
5,x,y},2]],{10^6}]]},{fn,{f,g,h,i,j,k,l}}]
Out[10]= {{f,4.12},{g,4.},{h,4.51},{i,3.93},{j,4.81},{k,10.88},{l,
8.22}}

Simon

On Feb 8, 8:57 pm, Oliver Fochler <janitor... at googlemail.com> wrote:
> Hi there,
>
> in order to get my NIntegrate running properly it seems that I need to
> ensure that my functions actually take numeric arguments (without
> NIntegrate trying to do some symbolic stuff before).
>
> For a simple case something like
>
> f[x_?NumericQ] := (stuff)
>
> seems to work fine. However, I do use vectors (lists) as function
> arguments, i.e. f[x_, y_]:= (stuff) needs to be called as f[ {1,2,3},
> 4].
> How do I test this for numerical values? I know that I can use
> VectorQ[ x, NumericQ ] to check whether x is a vector that contains
> only numeric values.
>
> But how do I combine this into a pattern test, that can be used in an
> argument list?
> I would appreciate any help!
>
> Cheers,
> Oliver

```

• Prev by Date: Re: How fast does & perform?
• Next by Date: Re: Exception message from java: MathLink connection was lost.
• Previous by thread: Re: Test function argument with VectorQ and NumericQ
• Next by thread: Exception message from java: MathLink connection was lost.