matrixsympy

SymPy: Extract the lower triangular part of a matrix


I am trying to extract the lower triangular part of a SymPy matrix. Since I could not find a tril method in SymPy, I defined:

def tril (M):
    m = M.copy()
    for row_index in range (m.rows):
        for col_index in range (row_index + 1, m.cols):
            m[row_index, col_index] = 0
    return (m)

It seems to work:

enter image description here

Is there a more elegant way to extract the lower triangular part of a SymPy matrix?

Is .copy() the recommended way to ensure the integrity of the original matrix?


Solution

  • In SymPy, M.lower_triangular(k) will give the lower triangular elements below the kth diagonal. The default is k=0.