Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #33100: combine sparse and dense _is_hermitian() branches.
Browse files Browse the repository at this point in the history
This is a simple refactoring to combine two almost-identical branches
of an "if" statement.
  • Loading branch information
orlitzky committed Feb 12, 2022
1 parent 3056eea commit 7cac4e4
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions src/sage/matrix/matrix0.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4043,46 +4043,34 @@ cdef class Matrix(sage.structure.element.Matrix):
# the non-zero positions. This will be faster if the matrix
# is truly sparse (if there are not so many of those positions)
# even after taking numerical issues into account.
for (i,j) in self.nonzero_positions():
entry_a = self.get_unsafe(i,j)
entry_b = s*self.get_unsafe(j,i).conjugate()

if tolerance_is_zero:
# When the tolerance is exactly zero, as will
# usually be the case for exact rings, testing for
# literal equality provides a simple answer to the
# question of how we should test against the
# tolerance in rings such as finite fields and
# polynomials where abs/norm support is spotty and
# an ordering may not be intelligently defined.
if entry_a != entry_b:
self.cache(key, False)
return False
else:
d = entry_a - entry_b
# sqrt() can have a different parent, and doesn't
# preserve order in e.g. finite fields, so we
# square both sides of the usual test here.
if (d*d.conjugate()) > tolerance**2:
self.cache(key, False)
return False
entries = self.nonzero_positions()
else:
for i in range(self._nrows):
for j in range(i+1):
entry_a = self.get_unsafe(i,j)
entry_b = s*self.get_unsafe(j,i).conjugate()

if tolerance_is_zero:
# Explained above in the sparse case.
if entry_a != entry_b:
self.cache(key, False)
return False
else:
d = entry_a - entry_b
# Explained above in the sparse case.
if (d*d.conjugate()) > tolerance**2:
self.cache(key, False)
return False
entries = ( (i,j) for i in range(self._nrows)
for j in range(i+1) )

for (i,j) in entries:
entry_a = self.get_unsafe(i,j)
entry_b = s*self.get_unsafe(j,i).conjugate()

if tolerance_is_zero:
# When the tolerance is exactly zero, as will
# usually be the case for exact rings, testing for
# literal equality provides a simple answer to the
# question of how we should test against the
# tolerance in rings such as finite fields and
# polynomials where abs/norm support is spotty and
# an ordering may not be intelligently defined.
if entry_a != entry_b:
self.cache(key, False)
return False
else:
d = entry_a - entry_b
# sqrt() can have a different parent, and doesn't
# preserve order in e.g. finite fields, so we
# square both sides of the usual test here.
if (d*d.conjugate()) > tolerance**2:
self.cache(key, False)
return False

self.cache(key, True)
return True
Expand Down

0 comments on commit 7cac4e4

Please sign in to comment.