E.g., shape and size of the array can be derived using values from the input parameters
FUNCTION MyFuncName(Param1, Param2, ...) RESULT( resultName ) IMPLICIT NONE TYPE, DIMENSION(...) :: resultName TYPE [, Intention] :: Param1 TYPE [, Intention] :: Param2 .... END ! Ends subroutine definition |
|
FUNCTION MatVecMult(A, v) RESULT( w ) IMPLICIT NONE REAL, dimension(:,:), intent(in) :: A REAL, dimension(:), intent(in) :: v REAL, DIMENSION( ??? ) :: w .... END |
Problem:
|
Specifically:
|
Solution:
FUNCTION MatVecMult(A, v) RESULT( w ) IMPLICIT NONE REAL, dimension(:,:), intent(in) :: A REAL, dimension(:), intent(in) :: v REAL, DIMENSION( SIZE(A,1) ) :: w .... END |
The rest is straight forward....
function MatVecMult(A, v) result (w) implicit none real, dimension(:,:), intent(in) :: A real, dimension(:), intent(in) :: v real, dimension( SIZE(A,1) ) :: w integer :: i, j integer :: N N = size(v) w = 0.0 !! clear whole vector DO i = 1, N w = w + v(i) * A( :, i ) END DO end function |
We must learn how to declare a function that returns an array
interface !! Declare MatVecMult function function MatVecMult(A, v) result (w) real, dimension(:,:), intent(in) :: A real, dimension(:), intent(in) :: v real, dimension( SIZE(A,1) ) :: w end function end interface |
program Main implicit none interface !! Declare function function MatVecMult(A, v) result (w) real, dimension(:,:), intent(in) :: A real, dimension(:), intent(in) :: v real, dimension( SIZE(A,1) ) :: w end function end interface real, dimension( 3, 3 ) :: A real, dimension( 3 ) :: v1, v2 v2 = MatVecMult(A, v1) !! Use it... |