MathGroup Archive 2012

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

Search the Archive

Re: how to check for NumericQ arbitrary data structure

I have a data structure e.g. {a,{b,c},{d,{e,f},{g}}}, which I want to
check for NumericQ. For vectors and matrices I can use
VectorQ[x,NumericQ] or MatrixQ[x,NumericQ]. Is there a way to check an
arbitrary data structure that all elements are NumericQ or I have to
construct a function for each data structure?
Thanks in advance,

Hi, Ted,

You did not specify, what exactly do you need to have as the result. You may want simply
to have
1) True, if all terms are numeric and False if any of them are non-numeric
2) you may want say, to preserve the structure of the original list, in which True and False
occupy the places of numeric and non-numeric items.

The first case is simple, since you can flatten the initial list
Try this function:

numericCheck[lst_List] := And @@ NumericQ /@ Flatten[lst];

For example, here are three lists with the structure you have specified:

lst1 = {a, {b, c}, {d, {e, f}, {g}}};
lst2 = {0.136, {0.683, 0.123}, {0.1867, {0.203, 0.493}, {0.5369}}};
lst3 = {0.136, {0.683, 0.123}, {0.1867, {a, 0.493}, {0.5369}}};

Among them only the lst2 is numeric, while lst3 has a variable "a" instead of the number in the position {3,2,1}. Let us check

numericCheck /@ {lst1, lst2, lst3}

{False, True, False}

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch at

  • Prev by Date: Re: About linear programming
  • Next by Date: Re: Coloured strings
  • Previous by thread: Re: Unsatisfactory view of Panel, when transformed to pdf
  • Next by thread: NonlinearModelFit and Complex Data