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
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
> 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
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
> 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
> 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
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
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