program mat_transf
  implicit none
  integer, parameter   :: n = 10, m = 3
  real, dimension(n,m) :: mat
  integer              :: choix_methode, ios, num_ligne
  real, external       :: carre, identite, logarithme
  real, intrinsic      :: sqrt
  namelist/methode/choix_methode

        ! Ouverture du fichier contenant la matrice.
  open( unit=1,             file="exo6.matrice", &
        form="unformatted", action="read",       &
        status="old",       position="rewind",   &
        iostat=ios )
  if (ios /= 0) &
    stop 'Erreur à l''ouverture du fichier "exo6.matrice"'
               ! Lecture de la matrice
  read(1) mat
  close(1)
          ! Ouverture du fichier contenant 
               ! la namelist "methode".
  open( unit=1,           file="exo10.namelist", &
        form="formatted", action="read",         &
        status="old",     position="rewind",     &
        iostat=ios )
  if (ios /= 0) &
    stop 'Erreur à l''ouverture du fichier "exo10.namelist"'
  read( unit=1, nml=methode )
  close( unit=1 )

        ! Transformation de la matrice à l'aide
                ! de la méthode choisie.

  select case( choix_methode )
    case (1)
      call transform( mat, n, m, identite )
    case (2)
      call transform( mat, n, m, carre )
    case (3)
      call transform( mat, n, m, sqrt )
    case (4)
      call transform( mat, n, m, logarithme )
  end select

        ! Sauvegarde de la matrice transformée dans
                ! le fichier "exo6_matrice_transf".

  open( unit=1,           file="exo6_matrice_transf", &
        form="formatted", action="write",             &
        status="replace", iostat=ios )

  if ( ios /= 0 ) &
    stop "Erreur lors de l''ouverture &
         &du fichier ""exo6_matrice_transf"""

  do num_ligne=1,n
    write( unit=1, fmt='(3f10.6)' ) mat(num_ligne,:)
  end do
  close( unit=1 )
end program mat_transf

              ! Procédure de transformation.
subroutine transform( t, n, m, f )
  implicit none
  integer              :: n, m, i, j
  real, dimension(n,m) :: t
  real                 :: f

  do i=1,n
    do j=1,m
      t(i,j) = f(t(i,j))
    end do
  end do
end subroutine transform

       ! Définitions des fonctions de transformation.
function identite(x)
  implicit none
  real x, identite
  identite = x
end function identite

function carre(x)
  implicit none
  real x, carre
  carre = x*x
end function carre

function logarithme(x)
  implicit none
  real x, logarithme
  logarithme = log(x)
end function logarithme

