_?NumericQ equivalent for lists - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-16T07:32:18Z https://mathematica.stackexchange.com/feeds/question/16694 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/16694 27 _?NumericQ equivalent for lists Cuboid https://mathematica.stackexchange.com/users/2034 2012-12-20T13:52:06Z 2016-01-12T08:38:46Z <p>A few months ago I got a simple answer (see: <a href="https://mathematica.stackexchange.com/questions/10918/findminimum-nminimize-etc-with-external-process">1</a> ) to the question of how to force numeric evaluation during use of functions such as <code>NMinimize[]</code> - namely the use of <code>_?NumericQ</code> in the argument list. </p> <p>I now have the same problem, only this time the argument to the user-defined function I'm trying to minimize is a list. Is there an equivalent to <code>_?NumericQ</code> for lists?</p> https://mathematica.stackexchange.com/questions/16694/-/19600#19600 32 Answer by Ajasja for _?NumericQ equivalent for lists Ajasja https://mathematica.stackexchange.com/users/745 2013-02-14T14:29:21Z 2013-02-18T23:54:34Z <p>You have several options:</p> <pre><code>foo[arg_?(VectorQ[#,NumericQ]&amp;)] foo[arg: {_?NumericQ ..}] foo[arg: {__?NumericQ}] </code></pre> <p>For matrices or higher dimensional arrays, the equivalent of <code>VectorQ</code> is <code>MatrixQ</code> and <code>ArrayQ</code>.</p> <p>It's worth noting that <code>VectorQ[..., NumericQ]</code> (and its relatives <code>MatrixQ</code> and <code>ArrayQ</code>) are highly optimized and will avoid unpacking packed arrays:</p> <pre><code>match = RandomReal[1, 10^6]; unpacked = Append[match, 1]; nonmatch = Append[match, "x"]; Table[ Timing[MatchQ[set, _?(VectorQ[#, NumericQ] &amp;)]], {set, {match, unpacked, nonmatch}}] (* {{0.000016, True}, {0.003106, True}, {0.003184, False}} *) Table[ Timing[MatchQ[set, _?(VectorQ[#, NumericQ[#] &amp;] &amp;)]], {set, {match, unpacked, nonmatch}}] (* {{0.402001, True}, {0.364469, True}, {0.362019, False}} *) Table[ Timing[MatchQ[set, {_?NumericQ ..}]], {set, {match, unpacked, nonmatch}}] (* {{0.266730, True}, {0.231373, True}, {0.229849, False}} *) Table[ Timing[MatchQ[set, {__?NumericQ}]], {set, {match, unpacked, nonmatch}}] (* {{0.226045, True}, {0.158696, True}, {0.160493, False}} *) </code></pre>