読者です 読者をやめる 読者になる 読者になる

fortran66のブログ

fortran について書きます。

Fortran2003 OOP memo

参考 Modern Fortran Explained §14.6

    module m_base
      implicit none
      type, abstract :: t_matrix
      contains
        procedure(op2), deferred :: prdct
        generic, public :: operator(*) => prdct
      end type t_matrix
      
      abstract interface
        function op2(mat, vec) result(res) 
          import :: t_matrix 
          class(t_matrix), intent(in) :: mat
          real, intent(in) :: vec(:) 
          real :: res(size(vec))
        end function op2
      end interface     
    end module m_base
     
    module m_test
      use m_base
      implicit none
      type, extends(t_matrix) :: t_dense_matrix
        real, allocatable :: x(:, :)
      contains
        procedure  :: prdct => product_dense  
      end type t_dense_matrix
      
    contains
    
      function product_dense(mat, vec) result(res)
        class(t_dense_matrix), intent(in) :: mat
        real, intent(in) :: vec(:)
        real :: res(size(vec))
        res = matmul(mat%x, vec)
        return
      end function product_dense
      
    end module m_test
  
    program new2014
      use m_test
      implicit none
      type (t_dense_matrix) :: a
      real, allocatable :: x(:), b(:)
      integer :: i, j, n = 10 
      allocate( a%x(n, n), b(n), x(n) ) 
      a%x = 1.0
      b = 1.0
      x = a * b
      print *, x
      stop
    end program new2014