Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: how to check for NumericQ arbitrary data structure


Hi,

  I would like to add an "old"-style way of programming to tackle the 
problem:

data1 = {2, {Pi, 0.5}, {1/2, {I, 3 Sin[2]}, {6}}};
data2 = {a, {Pi, 0.5}, {1/2, {I, 3 Sin[2]}, {6}}};

testQ[expr_] := Block[{},
   If[
    Head[expr] === List,
    Do[testQ[expr[[i]]], {i, 1, Length[expr]}],
    If[! NumericQ[expr], Throw[False]]];
   Throw[True]
   ]

TestQ[expr_] := Catch[testQ[expr]]


TestQ[data1]
True

TestQ[data2]
False


Note, that TestQ is needed to catch the False thrown by testQ if 
!NumericQ returns True.

Best,

Christoph



On 04/24/2012 11:32 AM, Ted Sariyski wrote:
> 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
>
>




  • Prev by Date: Efficient floodfill algorithm
  • Next by Date: Unsatisfactory view of Panel, when transformed to pdf
  • Previous by thread: Re: is there internal variable like $0 in perl?
  • Next by thread: Re: is there internal variable like $0 in perl?