I read the small code by @certik
The code is simply read in a integer matrix A, and output its diagonal elements in array d.
function diag(A) result(d)
integer, intent(in) :: A(:,:)
integer :: d(size(A,1))
integer :: i
do i = 1, size(A,1)
d(i) = A(i,i)
end do
end function
Now, if A is complex number, or real number matrix, do we need to write diag function for each of the different types of A?
I know there is elemental function which can smartly use a scalar function for vector function,
but here, is there a way that Fortran can just use one diag function, and it can smartly apply this function to any data type of the input matrix A?
Things like below for example
function diag(A) result(d)
**type(any)**, intent(in) :: A(:,:)
**type(A)** :: d(size(A,1))
integer :: i
do i = 1, size(A,1)
d(i) = A(i,i)
end do
end function
I think you desire a syntax similar to what I (and many others) wished for in this post. Such syntax is not going to be seen in Fortran ever, or anytime soon, based on the discussions in the above thread.
@CRquantum yes, the new generics, being proposed for F202Y and that have a prototype in LFortran will be able to handle this case, you just declare it as generic, write it once, and it will work for all types.