Skip to content

Commit

Permalink
Merge pull request sympy#2637 from Tempel/matrix_is_zero
Browse files Browse the repository at this point in the history
MatrixBase.is_zero supporting None results.
  • Loading branch information
jrioux committed Dec 14, 2013
2 parents 1f0217d + cbb12ca commit 362a4d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions sympy/matrices/immutable.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def __setitem__(self, *args):
__neg__ = MatrixBase.__neg__
__div__ = MatrixBase.__div__
__truediv__ = MatrixBase.__truediv__
# This is included after the class definition as a workaround for issue 4114.
# See http://code.google.com/p/sympy/issues/detail?id=4114
ImmutableMatrix.is_zero = DenseMatrix.is_zero


class ImmutableSparseMatrix(Basic, SparseMatrix):
Expand Down
12 changes: 10 additions & 2 deletions sympy/matrices/matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1962,16 +1962,19 @@ def is_zero(self):
A matrix is zero if every element is zero. A matrix need not be square
to be considered zero. The empty matrix is zero by the principle of
vacuous truth.
vacuous truth. For a matrix that may or may not be zero (e.g.
contains a symbol), this will be None
Examples
========
>>> from sympy import Matrix, zeros
>>> from sympy.abc import x
>>> a = Matrix([[0, 0], [0, 0]])
>>> b = zeros(3, 4)
>>> c = Matrix([[0, 1], [0, 0]])
>>> d = Matrix([])
>>> e = Matrix([[x, 0], [0, 0]])
>>> a.is_zero
True
>>> b.is_zero
Expand All @@ -1980,8 +1983,13 @@ def is_zero(self):
False
>>> d.is_zero
True
>>> e.is_zero
"""
return not list(self.values())
if any(i.is_zero == False for i in self):
return False
if any(i.is_zero == None for i in self):
return None
return True

def is_nilpotent(self):
"""Checks if a matrix is nilpotent.
Expand Down
7 changes: 7 additions & 0 deletions sympy/matrices/tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,13 @@ def test_is_zero():
assert Matrix([[0, 0], [0, 0]]).is_zero
assert zeros(3, 4).is_zero
assert not eye(3).is_zero
assert Matrix([[x, 0], [0, 0]]).is_zero == None
assert SparseMatrix([[x, 0], [0, 0]]).is_zero == None
assert ImmutableMatrix([[x, 0], [0, 0]]).is_zero == None
assert ImmutableSparseMatrix([[x, 0], [0, 0]]).is_zero == None
assert Matrix([[x, 1], [0, 0]]).is_zero == False
a = Symbol('a', nonzero=True)
assert Matrix([[a, 0], [0, 0]]).is_zero == False


def test_rotation_matrices():
Expand Down

0 comments on commit 362a4d6

Please sign in to comment.