Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

function to check if array is empty

1,152 views
Skip to first unread message

will parr

unread,
May 1, 2008, 3:23:57 AM5/1/08
to
Dear Math Forum,

this probably already exists, but I can't find it anywhere:

is there a function that will check if an array is empty?

i'm looking for a function to return either True (or 1), or False (or 0) when it checks an array. eg:

X = Array[0 &, {3, 3}]

then test with function (called ArrayEmpty, for example)

In: ArrayEmpty[X]

Out: True

best wishes,

Will

Adriano Pascoletti

unread,
May 1, 2008, 6:27:56 AM5/1/08
to
Will,
a null row is a list of one or more 0's {(0)..} and a null matrix is a list
of one or more null rows {{(0)..}..}, so

In[4]:= MatchQ[X, {{(0) ..} ..}]
Out[4]= True

and

ArrayEmpty=MatchQ[#, {{(0) ..} ..}]&

If you want to match both exact and approximate 0's replace (0).. with
(0|0.)..


Adriano Pascoletti

Jean-Marc Gulliet

unread,
May 2, 2008, 3:46:21 AM5/2/08
to
will parr wrote:

> is there a function that will check if an array is empty?
>
> i'm looking for a function to return either True (or 1), or False (or 0) when it checks an array. eg:
>
> X = Array[0 &, {3, 3}]
>
> then test with function (called ArrayEmpty, for example)
>
> In: ArrayEmpty[X]
>
> Out: True

If you deal only with m x n rectangular arrays (matrices) you could use

ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]

A more general function would be

ArrayEmptyQ[arr_List] :=
If[Count[arr, x_ /; x != 0, -1] != 0, False, True]

For instance,

X = Array[0 &, {3, 3}]

ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
ArrayEmptyQ[X]
X[[1, 2]] = 1;
X
ArrayEmptyQ[X]

{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

True

{{0, 1, 0}, {0, 0, 0}, {0, 0, 0}}

False

Regards,
-- Jean-Marc

Murray Eisenberg

unread,
May 2, 2008, 3:49:10 AM5/2/08
to
Your array is hardly "empty": it's full of zeros!

If what you want to do is test to see whether every entry is 0, the
following will do it:

And @@ Thread[0 == Flatten @ X]
]


will parr wrote:
> Dear Math Forum,
>
> this probably already exists, but I can't find it anywhere:
>

> is there a function that will check if an array is empty?
>
> i'm looking for a function to return either True (or 1), or False (or 0) when it checks an array. eg:
>
> X = Array[0 &, {3, 3}]
>
> then test with function (called ArrayEmpty, for example)
>
> In: ArrayEmpty[X]
>
> Out: True
>

> best wishes,
>
> Will
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Joe Bolte

unread,
May 5, 2008, 6:20:16 AM5/5/08
to
On May 2, 2008, at 3:41 AM, Jean-Marc Gulliet wrote:

> will parr wrote:
>
>> is there a function that will check if an array is empty?
>>
>> i'm looking for a function to return either True (or 1), or False
>> (or 0) when it checks an array. eg:
>>
>> X = Array[0 &, {3, 3}]
>>
>> then test with function (called ArrayEmpty, for example)
>>
>> In: ArrayEmpty[X]
>>
>> Out: True
>

> If you deal only with m x n rectangular arrays (matrices) you could
> use
>
> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
>
> A more general function would be
>
> ArrayEmptyQ[arr_List] :=
> If[Count[arr, x_ /; x != 0, -1] != 0, False, True]
>
> For instance,
>

> X = Array[0 &, {3, 3}]

> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
> ArrayEmptyQ[X]
> X[[1, 2]] = 1;
> X
> ArrayEmptyQ[X]
>
> {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
>
> True
>
> {{0, 1, 0}, {0, 0, 0}, {0, 0, 0}}
>
> False

Tally provides a very fast way to test for any non-zero elements. It
is faster than the solutions above except in where there are non-zero
elements very close to the beginning of the array.

Test Data

array1=ConstantArray[0,{1000,1000}];
array2=DiagonalMatrix[Table[1,{1000}]];
array3=ConstantArray[0,{1000,1000}];
array3[[1000,1000]]=1;

Tally-based solution

In[14]:= ZeroMatrixQ:=Tally[Flatten@#]==={0,Length@Flatten@#}&
In[24]:= Timing[ZeroMatrixQ[#]]&/@{array1,array2,array3}
Out[24]= {{0.008125,True},{0.006914,False},{0.008563,True}}

MatrixQ-based solution

Function[array, Timing[MatrixQ[array, ((# == 0) &)]]] /@ {array1,
array2,
array3}

In[29]:= Function[array,Timing[MatrixQ[array,((#==0)&)]]]/
@{array1,array2,array3}
Out[29]= {{0.857164,True},{0.000012,False},{0.866723,False}}


Cheers,
Joe

Jean-Marc Gulliet

unread,
May 5, 2008, 6:20:49 AM5/5/08
to
Jean-Marc Gulliet wrote:

> will parr wrote:
>
>> is there a function that will check if an array is empty?
>> i'm looking for a function to return either True (or 1), or False (or
>> 0) when it checks an array. eg:
>> X = Array[0 &, {3, 3}]
>>
>> then test with function (called ArrayEmpty, for example)
>>
>> In: ArrayEmpty[X]
>>
>> Out: True
>

> If you deal only with m x n rectangular arrays (matrices) you could use
>
> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
>
> A more general function would be
>
> ArrayEmptyQ[arr_List] :=
> If[Count[arr, x_ /; x != 0, -1] != 0, False, True]
>
> For instance,
>

> X = Array[0 &, {3, 3}]

> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
> ArrayEmptyQ[X]
> X[[1, 2]] = 1;
> X
> ArrayEmptyQ[X]
>
> {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
>
> True
>
> {{0, 1, 0}, {0, 0, 0}, {0, 0, 0}}
>
> False

If speed is important, using *Total* and *Unitize* (to prevent
cancellation of terms with same magnitude but opposite signs) will be
faster than the above solution.

In[1]:= X = Array[0 &, {3, 3}]
ArrayEmptyQ[arr_List] := Total[Unitize[arr], Infinity] == 0
ArrayEmptyQ[X]
X[[1, 2]] = 1; X[[1, 3]] = -1;
X
ArrayEmptyQ[X]
X = Array[0 &, {1000, 1000}];
Timing@ArrayEmptyQ[X]

Out[1]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

Out[3]= True

Out[5]= {{0, 1, -1}, {0, 0, 0}, {0, 0, 0}}

Out[6]= False

Out[8]= {0.015834, True}

In[9]:= Total[X, -1] == 0

Out[9]= True

In[10]:= X = Array[0 &, {3, 3}]


ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
ArrayEmptyQ[X]

X[[1, 2]] = 1; X[[1, 3]] = -1;
X
ArrayEmptyQ[X]
X = Array[0 &, {1000, 1000}];
Timing@ArrayEmptyQ[X]

Out[10]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

Out[12]= True

Out[14]= {{0, 1, -1}, {0, 0, 0}, {0, 0, 0}}

Out[15]= False

Out[17]= {0.714124, True}

Regards,
-- Jean-Marc

Jean-Marc Gulliet

unread,
May 5, 2008, 6:21:00 AM5/5/08
to
On Sun, May 4, 2008 at 7:22 PM, Joe Bolte <jo...@wolfram.com> wrote:

>
> On May 2, 2008, at 3:41 AM, Jean-Marc Gulliet wrote:
>
>
> > will parr wrote:
> >
> >
> > > is there a function that will check if an array is empty?
> > >
> > > i'm looking for a function to return either True (or 1), or False (or 0)
> when it checks an array. eg:
> > >
> > > X = Array[0 &, {3, 3}]
> > >
> > > then test with function (called ArrayEmpty, for example)
> > >
> > > In: ArrayEmpty[X]
> > >
> > > Out: True
> > >
> >
> > If you deal only with m x n rectangular arrays (matrices) you could use
> >
> > ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
> >
> > A more general function would be
> >
> > ArrayEmptyQ[arr_List] :=
> > If[Count[arr, x_ /; x != 0, -1] != 0, False, True]
> >
> > For instance,
> >
> > X = Array[0 &, {3, 3}]
> > ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
> > ArrayEmptyQ[X]
> > X[[1, 2]] = 1;
> > X
> > ArrayEmptyQ[X]
> >
> > {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
> >
> > True
> >
> > {{0, 1, 0}, {0, 0, 0}, {0, 0, 0}}
> >
> > False
> >
>
> Tally provides a very fast way to test for any non-zero elements. It is
> faster than the solutions above except in where there are non-zero elements
> very close to the beginning of the array.

Hi Joe,

Nice approach. The code you posted, however, contains a small typo. I
have provided a fix; see below.

> Test Data
>
> array1=ConstantArray[0,{1000,1000}];
> array2=DiagonalMatrix[Table[1,{1000}]];
> array3=ConstantArray[0,{1000,1000}];
> array3[[1000,1000]]=1;
>
> Tally-based solution
>
> In[14]:= ZeroMatrixQ:=Tally[Flatten@#]==={0,Length@Flatten@#}&

------------------------------------------------------------------^^^---------------------------^^^
An additional pair of parentheses is required here to mach the
structure of the result returned by Tally.

> In[24]:= Timing[ZeroMatrixQ[#]]&/@{array1,array2,array3}
> Out[24]= {{0.008125,True},{0.006914,False},{0.008563,True}}

-----------------------------------------------------------------------------------^^^^^
Error: should be false. See below for the explanation and a fix.

> MatrixQ-based solution
>
> Function[array, Timing[MatrixQ[array, ((# == 0) &)]]] /@ {array1, array2,
> array3}
>
> In[29]:=

> Function[array,Timing[MatrixQ[array,((#==0)&)]]]/@{array1,array2,array3}


> Out[29]= {{0.857164,True},{0.000012,False},{0.866723,False}}

In[1]:= array1 = ConstantArray[0, {1000, 1000}];
array2 = DiagonalMatrix[Table[1, {1000}]];
array3 = ConstantArray[0, {1000, 1000}];
array3[[1000, 1000]] = 1;

Tally[Flatten@#] & /@ {array1, array2, array3}
(*Note that Tally returns *a list of list* even if there is only one
kind element that is counted. So, for a list that contains only zeros,
say ten, Tally will return {{0,10}} rather than {0,10}*)
ZeroMatrixQ := Tally[Flatten@#] === {{0, Length@Flatten@#}} &
Timing[ZeroMatrixQ[#]] & /@ {array1, array2, array3}

(*It even faster than the solution with Total Unitize*)
Function[array, Timing[Total[Unitize[array], -1] == 0]] /@ {array1,
array2, array3}

Out[5]= {{{0, 1000000}}, {{1, 1000}, {0, 999000}}, {{0, 999999}, {1,
1}}}

Out[7]= {{0.004968, True}, {0.004939, False}, {0.005611, False}}

Out[8]= {{0.016013, True}, {0.016292, False}, {0.016105, False}}

Best regards,
--
Jean-Marc

Jean-Marc Gulliet

unread,
May 5, 2008, 6:21:10 AM5/5/08
to
Jean-Marc Gulliet wrote:
> Jean-Marc Gulliet wrote:
>
>> will parr wrote:
>>
>>> is there a function that will check if an array is empty?
>>> i'm looking for a function to return either True (or 1), or False (or
>>> 0) when it checks an array. eg:
>>> X = Array[0 &, {3, 3}]
>>>
>>> then test with function (called ArrayEmpty, for example)
>>>
>>> In: ArrayEmpty[X]
>>>
>>> Out: True
>>
>> If you deal only with m x n rectangular arrays (matrices) you could use
>>
>> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
>>
>> A more general function would be
>>
>> ArrayEmptyQ[arr_List] :=
>> If[Count[arr, x_ /; x != 0, -1] != 0, False, True]
>>
>> For instance,
>>
>> X = Array[0 &, {3, 3}]
>> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
>> ArrayEmptyQ[X]
>> X[[1, 2]] = 1;
>> X
>> ArrayEmptyQ[X]
>>
>> {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
>>
>> True
>>
>> {{0, 1, 0}, {0, 0, 0}, {0, 0, 0}}
>>
>> False
>
> If speed is important, using *Total* and *Unitize* (to prevent
> cancellation of terms with same magnitude but opposite signs) will be
> faster than the above solution.
>
> In[1]:= X = Array[0 &, {3, 3}]
> ArrayEmptyQ[arr_List] := Total[Unitize[arr], Infinity] == 0
> ArrayEmptyQ[X]

> X[[1, 2]] = 1; X[[1, 3]] = -1;
> X
> ArrayEmptyQ[X]
> X = Array[0 &, {1000, 1000}];
> Timing@ArrayEmptyQ[X]
>
> Out[1]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
>
> Out[3]= True
>
> Out[5]= {{0, 1, -1}, {0, 0, 0}, {0, 0, 0}}
>
> Out[6]= False
>
> Out[8]= {0.015834, True}
>
> In[9]:= Total[X, -1] == 0
>
> Out[9]= True
>
> In[10]:= X = Array[0 &, {3, 3}]

> ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
> ArrayEmptyQ[X]
> X[[1, 2]] = 1; X[[1, 3]] = -1;
> X
> ArrayEmptyQ[X]
> X = Array[0 &, {1000, 1000}];
> Timing@ArrayEmptyQ[X]
>
> Out[10]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
>
> Out[12]= True
>
> Out[14]= {{0, 1, -1}, {0, 0, 0}, {0, 0, 0}}
>
> Out[15]= False
>
> Out[17]= {0.714124, True}

Of course, if we know before hand that the list is made of non-negative
elements only (or non-positive only), we can discard the *Unitize*
function, so the resulting function is faster.

In[1]:= X = Array[0 &, {3, 3}]
ArrayEmptyQ[arr_List] := Total[arr, -1] == 0
ArrayEmptyQ[X]


X[[1, 2]] = 1; X[[1, 3]] = -1;
X
ArrayEmptyQ[X]
X = Array[0 &, {1000, 1000}];
Timing@ArrayEmptyQ[X]

Out[1]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

Out[3]= True

Out[5]= {{0, 1, -1}, {0, 0, 0}, {0, 0, 0}}

Out[6]= True

Out[8]= {0.009338, True}

--
Jean-Marc


0 new messages