Building lower triangular matrix

Hi all,

I am trying to create a lower triangular matrix S from a given matrix A. I have the following loop to assign 0’s to the above-diagonal entries of the matrix A. When I try to run my full code, I receive a “segmentation fault” and my guess is it has something to do with this function. I would be happy to provide more context or my full code if that helps.

// lower-triangular part of the matrix A (Assigns 0 to entries above diag.)
func matrix lowerTriangular(matrix A) {
    int n = A.n;
    matrix L = A;
    for (int i = 0; i < n; ++i)
        for (int j = i+1; j < n; ++j)
            L(i,j) = 0;
    return L;
}

Thanks in advance.

I think the issue may be that you need to pass A by reference using &.
Also, an implicit loop will be faster.

func matrix lowerTriangular(matrix &A) {
    matrix L = A;
    for [i, j, Lij : L]
        if (j > i) Lij = 0.0;
    return L;
}

Thank you, I really appreciate it!