# Building lower triangular matrix

**URL:** <https://community.freefem.org/t/building-lower-triangular-matrix/4249>\
**Category:** General Discussion\
**Created:** [April 7, 2026, 9:39pm UTC](https://community.freefem.org/t/building-lower-triangular-matrix/4249 "2026-04-07T21:39:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gart1542](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@gart1542](https://community.freefem.org/u/gart1542)\
**Post date:** [April 7, 2026, 9:39pm UTC](https://community.freefem.org/t/building-lower-triangular-matrix/4249/1 "2026-04-07T21:39:20Z")

</div>

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.

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [April 8, 2026, 2:39am UTC](https://community.freefem.org/t/building-lower-triangular-matrix/4249/2 "2026-04-08T02:39:01Z")

</div>

I think the issue may be that you need to pass `A` by reference using `&`.  
Also, an [implicit loop](https://doc.freefem.org/references/loops.html#implicit-loop) will be faster.

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

```

---

<div class="post-metadata">

**Author:** ![gart1542](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@gart1542](https://community.freefem.org/u/gart1542)\
**Post date:** [April 8, 2026, 7:04pm UTC](https://community.freefem.org/t/building-lower-triangular-matrix/4249/3 "2026-04-08T19:04:04Z")

</div>

Thank you, I really appreciate it!
