Subroutine to set diagonal elements of a matrix
In support of: https://github.com/j3-fortran/fortran_proposals/issues/14
Prior art:
- Python numpy.fill_diagonal
- Julia LinearAlgebra.diagind (returns indices of diagonal elements which can then be used to assign to a diagonal)
- MATLAB diag (enabled by MATLAB's special assignment syntax)
Target module: stdlib_linalg
Proposed signature:
! Scalar diagonal variant
pure subroutine set_diag(d, A, k)
{integer(*), real(*)}, complex(*)}, intent(in) :: d !! Value to set the diagonal to
{integer(*), real(*)}, complex(*)}, intent(inout) :: A(:,:) !! Matrix to set the diagonal in
integer, intent(in), optional :: k !! Number of the diagonal
! Array diagonal variant
pure subroutine set_diag(d, A, k)
{integer(*), real(*)}, complex(*)}, intent(in) :: d(:) !! Values to set the diagonal to
{integer(*), real(*)}, complex(*)}, intent(inout) :: A(:,:) !! Matrix to set the diagonal in
integer, intent(in), optional :: k !! Number of the diagonal
I presume this is supposed to be the subroutine version of diag that modifies the array in place.
@ivan-pi correct, though it would need a different name, thus the proposed set_diag.
Nice idea @milancurcic Thanks, @ivan-pi, for the link.
I have the following implementation in my library.
I have defined the Generic function called set(). From the viewpoint of the current issue, the interface of set is as follows.
PURE SUBROUTINE set(mat, value, index, filter)
REAL, INTENT(INOUT) :: mat(:,:)
REAL, INTENT(IN) :: value(:)
REAL, INTENT(IN) :: index
CHARACTER(LEN=1), INTENT(IN) :: filter
END SUBROUTINE set
-
filtercan beRfor a row,Cfor the column, andDfor diagonal -
indexdenotes row number, column number, diagonal number, depending upon the filter - Val is the row/ col/ diag value
- if size(Val) == 1, then all the row/col/diag values are set to the singleton value.
I would be happy to know your opinion on this.
Regards Vikas