def make_symmetric_lower(mat):
'''
Copies the matrix entries below the main diagonal to the upper triangle
half of the matrix. Leaves the diagonal unchanged. Returns a `NumPy` matrix
object.
**mat** : `numpy.matrix`
A lower diagonal matrix.
returns : `numpy.matrix`
The lower triangle matrix.
'''
# extract lower triangle from matrix (including diagonal)
tmp_mat = np.tril(mat)
# if the matrix given wasn't a lower triangle matrix, raise an error
if (mat != tmp_mat).all():
raise Exception('Matrix to symmetrize is not a lower diagonal matrix.')
# add its transpose to itself, zeroing the diagonal to avoid doubling
tmp_mat += np.triu(tmp_mat.transpose(), 1)
return np.asmatrix(tmp_mat)
评论列表
文章目录