Skip to content

diag, eye, and trace #169

Closed
Closed
@ivan-pi

Description

@ivan-pi

Related to #10

Convenience functions for use in linear algebra. The functions would go into stdlib_experimental_linalg.f90. Simple implementation by looping over the diagonal. Type-generic (integer, real, complex) implementation using the fypp preprocessor.

A draft is available here: https://github.com/ivan-pi/stdlib/blob/ivan-pi/src/stdlib_experimental_linalg.fypp (based on the version from fortran-utils #103 )

eye

Returns the identity matrix

function eye(n) result(res)
integer, intent(in) :: n
real(dp), allocatable :: res(:  ,:)
end function
  • Do we need an optional kind dummy variable for the return type (eye(n [, kind]))? Or just default to double precision?
  • Other names: identity, ... ?
  • Generalize also to higher rank arrays (tensors)?
  • Fixed-size or allocatable output?

trace

Returns the sum of diagonal elements

pure function trace(A) result(res)
real(dp), intent(in) :: A(:,:)
real(dp) :: res
end function
  • How to deal with non-square matrices? Take the sum over the shorter dimension? Run-time shape assertion?
  • Optional axis argument?
  • Diagonal offset?
  • Generalize to higher rank arrays (tensors)?

diag

Create a diagonal matrix from a vector or extract a diagonal from a matrix

! vector to matrix
function diag(v, k) result(res)
real(dp), intent(in) :: v(:)
integer, intent(in), optional :: k ! place elements of v on the k-th diagonal
real(dp), allocatable :: res(:,:)
end function
! matrix to vector
function diag(A, k) result(res)
real(dp), intent(in) :: A(:,:)
integer, intent(in), optional :: k ! extract elements from the k-th diagonal of A
real(dp), allocatable :: res(:)
end function
  • Other names: diagonal, ...
  • Specify which diagonal to use?
  • How to deal with non-square matrices? Extract the diagonal along the shorter dimension? Run-time shape assertion?
  • Generalize to higher rank arrays?
  • Fixed-size or allocatable output?

Other languages

The API's differ slightly between languages, some are more flexible than others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    implementationImplementation in experimental and submission of a PRspecificationDiscussion and iteration over the API

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions