MathGroup Archive 2007

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

Search the Archive

Re: ListDimension function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72603] Re: ListDimension function
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 11 Jan 2007 05:28:56 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eo25ue$as5$1@smc.vnet.net> <eo4nh9$lon$1@smc.vnet.net>

carlos at colorado.edu wrote:
> Many thanks to all of you who sent me solutions by direct email.
> For testing purposes I organized them into 7 functions:
> 
> ListDim1[v_]:=If[Head[v]===List,1+Max[Map[ListDim1,v],0],0]; (* Don
> Taylor *)
> ListDim2[lst_]:=Max[Length/@Position[lst,List]]; (* János *)
> ListDim3[expr_]:=Max[Length/@Position[{expr},{___}]]; (* David Park *)
> ListDim4[l_]:=Depth[DeleteCases[l,_?(!ListQ[#1]&)]]-1; (* Jean-Marc
> Gulliet *)
> ListDim5[e_]:=Max[Length/@Position[e,List]]; (* Carl Woll *)
> ListDim6[list_List]:=Depth[list/.x_List/;(!MemberQ[x,{___}])->{}]-1;ListDim6[_]=0;
> (* dh *)
> ListDim7[expr_]:=Depth[0*expr]-1; (* Andrzej Kozlowski *)
> 
> (I excluded a solution by Adriano Pascoletti, who uses 2 functions)
> Here is a quick test:
> 
> ListDimTest[e_]:={ListDim1[e],ListDim2[e],ListDim3[e],ListDim4[e],ListDim5[e],
>                   ListDim6[e],ListDim7[e]};
> 
> t={1,Null,{},{1,2,3,4},{"s"+1},{{},{},{}}, {Infinity,Indefinite,{}},
> {1,{{{{{{x,y*z}}}}}},{6}}, {{{{Sqrt[1+x*y]}}}},
> {1,{2,Null+5,3},{{{4,Infinity,-4}}}},
>    {0,{1,{2,{3^{1,2,3}}}}}, {c*{{1,2},{3+{x,y},4}}} } ;
> Print[Table[ListDimTest[t[[i]]],{i,1,Length[t]}]//TableForm];
> 
> and the results are
> 
> Infinity::indet: Indeterminate expression 0 Infinity encountered.
> 
> Infinity::indet: Indeterminate expression 0 Infinity encountered.
> 
>  0  -Infinity  0  0  -Infinity  0  0
>  0  -Infinity  0  0  -Infinity  0  0
>  1          1  1  1          1  1  1
>  1          1  1  1          1  1  1
>  1          1  1  1          1  1  1
>  2          2  2  2          2  2  2
>  2          2  2  2          2  2  2
>  7          7  7  8          7  7  7
>  4          4  4  7          4  4  4
>  4          4  4  5          4  4  4
>  5          5  5  5          5  5  5
>  4          4  4  6          4  4  4
> 
> Summary: #1, #3, #6 and #7 returned the correct value across the
> board. However #7 produced a warning message when an entry
> contained Infinity due to the 0*Infinite operation.  It seems that the
> most compact solution that works, and produces no messages, is #3.
> Congratulations to David.
> 
> BTW I found surprising that this is not provided as built-in function,
> since it is such a basic operation in list preprocessing.
> 
Hi Carlos,

Just for the record, I have fixed my function (I had forgotten to set 
the third argument of DeleteCases to Infinity so the function analyzed 
only the first level of the structure.)

ListDim4[l_] :=
   Depth[DeleteCases[l, _?(! ListQ[#1] &), Infinity]] - 1;

Regards,
Jean-Marc


  • Prev by Date: Re: question on Plot and Show plus Axis Labels
  • Next by Date: Re: Problem with Position applied on 2D list?
  • Previous by thread: ListDimension function
  • Next by thread: Re: ListDimension function