Skip to content

Upper or lower triangular part of an array #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ivan-pi opened this issue May 3, 2020 · 3 comments
Open

Upper or lower triangular part of an array #176

ivan-pi opened this issue May 3, 2020 · 3 comments
Labels
topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ...

Comments

@ivan-pi
Copy link
Member

ivan-pi commented May 3, 2020

triu and tril

tril - lower triangular part of an array
triu - upper triangular part of an array

Return a copy of the lower/upper triangular part of a rank-2 array. The elements below/above the k-th diagonal are replaced with zeroes (default k=0)

Useful to recover the lower or upper part of a matrix factorization.

Interface

interface tril
    module function tril_rsp(A) result(L)
        real(sp), intent(in) :: A(:,:)
        real(sp) :: L(size(A,1),size(A,2))
    end function
    module function tril_k_rsp(A,k) result(L)
        real(sp), intent(in) :: A(:,:)
        integer, intent(in) :: k
        real(sp) :: L(size(A,1),size(A,2))
    end function
    ! .. repeat for all real and integer kinds ..
end interface

Analogous interface for triu. The interfaces would go to the file stdlib_experimental_linalg.f90. Implementations would go to the submodule stdlib_experimental_linalg_trilu.f90.

Point for discussion: two separate functions (without or with diagonal) as shown above or only a single interface using the present intrinsic?

Other languages

Julia

MATLAB

Python (NumPy)

C++

Other

  • Would we like a subroutine version which works in-place? In Julia they use the tril!(M) and triu!(M) syntax for this purpose.
@ivan-pi
Copy link
Member Author

ivan-pi commented May 3, 2020

I was working on some Kalman filter stuff today, and I realized this would be useful to check I am calling the right sequence of BLAS and LAPACK routines.

@jvdp1
Copy link
Member

jvdp1 commented May 3, 2020

Nice proposition. It is something I often use in Octave.

Point for discussion: two separate functions (without or with diagonal) as shown above or only a single interface using the present intrinsic?

Why would you like 2 separate functions? I would suggest to implement only one function and use the optval function for the optional integer.

  • Would we like a subroutine version which works in-place? In Julia they use the tril!(M) and triu!(M) syntax for this purpose.

Yes, I would. However, I don't really like the sign "!" used in Julia.

@certik
Copy link
Member

certik commented May 3, 2020

Great idea, thanks.

Regarding in-place, see #177 for a discussion about the syntax. We can't use ! in Fortran (plus I don't really like it anyway).

@awvwgk awvwgk added the topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ... label Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ...
Projects
None yet
Development

No branches or pull requests

4 participants