Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9800

Re: how to check for NumericQ arbitrary data structure

From Alexei Boulbitch <Alexei.Boulbitch@iee.lu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: how to check for NumericQ arbitrary data structure
Date 2012-04-26 09:30 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jnb4k7$5h2$1@smc.vnet.net> (permalink)

Show all headers | View raw


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@iee.lu



Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: how to check for NumericQ arbitrary data structure Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2012-04-26 09:30 +0000

csiph-web