Re: how to check for NumericQ arbitrary data structure
- To: mathgroup at smc.vnet.net
- Subject: [mg126231] Re: how to check for NumericQ arbitrary data structure
- From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
- Date: Thu, 26 Apr 2012 05:28:34 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Hi,
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,
--Ted
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
or
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.
IEE S.A.
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 iee.lu