! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

  module G25M
    implicit none
    public :: Complex_To_Polar, Polar_To_Complex, Mult_Polar, Print_Polar
    type, public :: Polar
      private
      real :: R, Theta
    end type Polar
  contains
    function Complex_To_Polar( Z ) result( CTP_R )
      complex, intent (in) :: Z
      type(Polar) :: CTP_R
  ! start function Complex_To_Polar
      CTP_R % R = abs( Z )
      CTP_R % Theta = atan2( aimag( Z ), real( Z ) )
      return
    end function Complex_To_Polar
    function Polar_To_Complex( P ) result( PTC_R )
      type (Polar), intent (in) :: P
      complex :: PTC_R
      real :: X, Y
  ! start function Polar_To_Complex
      X = P % R * real( cos( P % Theta ) )
      Y = P % R * real( sin( P % Theta ) )
      PTC_R = cmplx( X, Y )
      return
    end function Polar_To_Complex
    function Mult_Polar( P1, P2 ) result( MP_R )
      type (Polar), intent(in) :: P1, P2
      type (Polar) :: MP_R
  ! start function Mult_Polar
      MP_R % R = P1 % R * P2 % R
      MP_R % Theta = P1 % Theta + P2 % Theta
      return
    end function Mult_Polar
    subroutine Print_Polar( P )
      type (Polar), intent(in) :: P
  ! start subroutine Print_Polar
      write (unit = *, fmt = *) P
      return
    end subroutine Print_Polar
  end module G25M
