From: Oliver Fochler on 8 Feb 2010 07:57 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
From: DrMajorBob on 9 Feb 2010 02:45 For instance, Clear[f] f[list : {__?NumericQ}, y_?NumericQ] := {y, list} f[{1, 2, 3}, 4] {4, {1, 2, 3}} Bobby On Mon, 08 Feb 2010 06:54:52 -0600, Oliver Fochler <janitor048(a)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 > -- DrMajorBob(a)yahoo.com
From: Szabolcs Horvát on 9 Feb 2010 02:46 On 2010.02.08. 13:57, Oliver Fochler 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! You can use for example f[ x_ ? (VectorQ[#, NumericQ]&) ] := ...
From: Patrick Scheibe on 9 Feb 2010 02:47 Hi, what about f[vec : {__?NumericQ}] := vec; or g[vec_?(VectorQ[#, NumericQ] &)] := vec ? Cheers Patrick On Mon, 2010-02-08 at 07:54 -0500, Oliver Fochler 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 >
From: Bill Rowe on 9 Feb 2010 02:47 On 2/8/10 at 7:54 AM, janitor048(a)googlemail.com (Oliver Fochler) wrote: >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! You could use a conditional test instead of a pattern test. For example, In[7]:= f[x_?VectorQ] := Total[x] /; And @@ (NumericQ /@ x) In[8]:= f[RandomReal[1, 10]] Out[8]= 4.46125 In[9]:= f[{a, b, c}] Out[9]= f({a,b,c}) or if you prefer, In[11]:= g[x_?(VectorQ[#, NumericQ] &)] := Total[x] In[12]:= g[RandomReal[1, 10]] Out[12]= 3.52661 In[13]:= g[{a, b, c}] Out[13]= g({a,b,c}) Note, I've not tested these with large data sets. So, there may be significant execution speed differences between these two options.
|
Next
|
Last
Pages: 1 2 Prev: Viviani's Curve Next: Exception message from java: MathLink connection was lost. |