Re: ListDimension function
- To: mathgroup at smc.vnet.net
- Subject: [mg72572] Re: ListDimension function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 11 Jan 2007 02:04:15 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <eo25ue$as5$1@smc.vnet.net>
carlos at colorado.edu wrote:
> Hi - I need a short function ListDimension that
> given a list returns its "brace dimensionality" =
> max number of brace levels to reach deepest entry.
> Examples
>
> List ListDimension
> a+b 0
> {1,2,x*y} 1
> {1,2,{3,0,1}} 2
> {{},{},{}} 2
> {{},{{1,2}},a} 3
>
> Built-in functions dont seem to be of help.
> Dimensions is restricted to matrix or tensor type of lists.
> Depth (minus 1) doesnt help as it is entry type dependent:
>
> Depth[{1,2,3}] -> 2
> Depth[{1,2,3+a}] -> 3
> Depth[{1,2,3+a+Sqrt[x+y]}] -> 5
>
> For the above three, ListDimension=1.
>
Hi Carlos,
The following definition for ListDimension yields the expected results
when tested against the various cases you provided.
In[1]:=
ListDimension[l_] :=
Depth[DeleteCases[l, _?( !ListQ[#1] & )]] - 1;
testcases = {a + b, {1, 2, x*y}, {1, 2, {3, 0, 1}},
{{}, {}, {}}, {{}, {{1, 2}}, a}, {1, 2, 3},
{1, 2, 3 + a}, {1, 2, 3 + a + Sqrt[x + y]}};
ListDimension /@ testcases
Out[5]=
{0, 1, 2, 2, 3, 1, 1, 1}
Best regards,
Jean-Marc