stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Subroutine to set diagonal elements of a matrix

Open milancurcic opened this issue 4 years ago • 3 comments

In support of: https://github.com/j3-fortran/fortran_proposals/issues/14

Prior art:

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

milancurcic avatar Oct 27 '21 20:10 milancurcic

I presume this is supposed to be the subroutine version of diag that modifies the array in place.

ivan-pi avatar Oct 28 '21 12:10 ivan-pi

@ivan-pi correct, though it would need a different name, thus the proposed set_diag.

milancurcic avatar Oct 28 '21 13:10 milancurcic

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
  • filter can be R for a row, C for the column, and D for diagonal
  • index denotes 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

vickysharma0812 avatar Nov 04 '21 04:11 vickysharma0812