Skip to content

Commit

Permalink
Trac #16306: Replace =0 by is_zero() in test for matrix being alterna…
Browse files Browse the repository at this point in the history
…ting

This branch does a couple small improvements to
`src/sage/matrix/matrix0.pyx`, the most important one being a
replacement of "== 0" by "== zero" since there are rings R whose zero
element is not the same as R(0). (Two grammar errors in the doc are also
fixed.)

URL: http://trac.sagemath.org/16306
Reported by: darij
Ticket author(s): Darij Grinberg
Reviewer(s): Vincent Delecroix
  • Loading branch information
Release Manager authored and vbraun committed May 16, 2014
2 parents a20700c + f36f0ad commit e15e22c
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/sage/matrix/matrix0.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ cdef class Matrix(sage.structure.element.Matrix):
"""
def __init__(self, parent):
"""
The initialization routine of the Matrix base class ensures that it sets
the attributes self._parent, self._base_ring, self._nrows, self._ncols.
It sets the latter ones by accessing the relevant information on parent,
which is often slower than what a more specific subclass can do.
The initialization routine of the ``Matrix`` base class ensures
that it sets the attributes ``self._parent``, ``self._base_ring``,
``self._nrows``, ``self._ncols``. It sets the latter ones by
accessing the relevant information on ``parent``, which is often
slower than what a more specific subclass can do.
Subclasses of Matrix can safely skip calling Matrix.__init__ provided they
take care of initializing these attributes themselves.
Subclasses of ``Matrix`` can safely skip calling
``Matrix.__init__`` provided they take care of initializing these
attributes themselves.
The private attributes self._is_immutable and self._cache are implicitly
initialized to valid values upon memory allocation.
The private attributes ``self._is_immutable`` and ``self._cache``
are implicitly initialized to valid values upon memory allocation.
EXAMPLES::
Expand All @@ -123,15 +125,15 @@ cdef class Matrix(sage.structure.element.Matrix):

def list(self):
"""
List of the elements of self ordered by elements in each
List of the elements of ``self`` ordered by elements in each
row. It is safe to change the returned list.
.. warning::
This function returns a list of the entries in the matrix
self. It does not return a list of the rows of self, so it
is different than the output of list(self), which returns
``[self[0],self[1],...]``.
``self``. It does not return a list of the rows of ``self``,
so it is different than the output of ``list(self)``, which
returns ``[self[0],self[1],...]``.
EXAMPLES::
Expand Down Expand Up @@ -161,12 +163,14 @@ cdef class Matrix(sage.structure.element.Matrix):

def _list(self):
"""
Unsafe version of the list method, mainly for internal use. This
may return the list of elements, but as an *unsafe* reference to
the underlying list of the object. It is might be dangerous if you
change entries of the returned list.
Unsafe version of the ``list`` method, mainly for internal use.
This may return the list of elements, but as an *unsafe* reference
to the underlying list of the object. It is dangerous to change
entries of the returned list.
EXAMPLES: Using _list is potentially fast and memory efficient,
EXAMPLES:
Using ``_list`` is potentially fast and memory efficient,
but very dangerous (at least for generic dense matrices).
::
Expand Down Expand Up @@ -3366,7 +3370,7 @@ cdef class Matrix(sage.structure.element.Matrix):
# testing the diagonal entries to be zero
zero = self.parent().base_ring().zero()
for i from 0 <= i < self._nrows:
if not self.get_unsafe(i,i) == zero:
if self.get_unsafe(i,i) != zero:
return False
sign = -1
else:
Expand Down Expand Up @@ -3731,8 +3735,8 @@ cdef class Matrix(sage.structure.element.Matrix):
Here, "alternating matrix" means a square matrix `A`
satisfying `A^T = -A` and such that the diagonal entries
of `0`. Notice that the condition that the diagonal
entries be `0` is not redundant for matrices over
of `A` are `0`. Notice that the condition that the
diagonal entries be `0` is not redundant for matrices over
arbitrary ground rings (but it is redundant when `2` is
invertible in the ground ring). A square matrix `A` only
required to satisfy `A^T = -A` is said to be
Expand Down Expand Up @@ -3762,11 +3766,12 @@ cdef class Matrix(sage.structure.element.Matrix):
# is the type used for indexing.
cdef Py_ssize_t i,j

zero = self._base_ring.zero()
for i from 0 <= i < self._nrows:
for j from 0 <= j < i:
if self.get_unsafe(i,j) != -self.get_unsafe(j,i):
return False
if self.get_unsafe(i,i) != 0:
if not self.get_unsafe(i,i) == zero:
return False
return True

Expand Down Expand Up @@ -4000,7 +4005,7 @@ cdef class Matrix(sage.structure.element.Matrix):
sage: B.left_kernel().dimension()
0
For "rectangular" matrices, invertibility is always
For *rectangular* matrices, invertibility is always
``False``, but asking about singularity will give an error. ::
sage: C = matrix(QQ, 5, range(30))
Expand All @@ -4024,8 +4029,8 @@ cdef class Matrix(sage.structure.element.Matrix):
sage: d.is_unit()
False
"""
if self.ncols() == self.nrows():
return self.rank() != self.nrows()
if self._ncols == self._nrows:
return self.rank() != self._nrows
else:
raise ValueError("self must be a square matrix")

Expand Down

0 comments on commit e15e22c

Please sign in to comment.