fortran66のブログ

fortran について書きます。

ソース・コード

module m_mandel
  implicit none
  integer, parameter :: kd = kind(0.0d0)
  integer, parameter :: maxiter =   50
contains
  pure elemental integer function imandel(z)
    complex(kd), intent(in) :: z
    complex(kd) :: c
    c = (0.0_kd, 0.0_kd)
    do imandel = maxiter, 1, -1
      if (abs(c) > 2.0_kd) exit
      c = c**2 + z
    end do
    return
  end function imandel
end module m_mandel

program cha_mandel
  use m_mandel
  implicit none
  real(kd), parameter :: x0 = -2.0_kd, x1 = 1.0_kd, y0 = -1.25_kd, y1 = 1.25_kd
  integer, parameter :: nx = 135, ny = 45
  character(30) :: fmt
  complex(kd) :: pos(nx, ny)
  integer :: ix, iy
  forall (ix = 1:nx, iy = 1:ny) pos(ix, iy) = cmplx( scaler(ix, nx, x0, x1), scaler(iy, ny, y0, y1) )
  write(fmt, '(a, g, a)') '(*(', nx, 'a:/))'
  print fmt, icha( imandel(pos) )
  stop
contains
  pure real(kd) function scaler(ix, nx, x0, x1)
    integer,  intent(in) :: ix, nx
    real(kd), intent(in) :: x0, x1
    scaler = (x1 - x0) / (nx - 1) * (ix - 1) + x0
    return
  end function scaler
 
  pure elemental character function icha(i)
    character(:), parameter :: color = ' .,:;rcuowijlbCUOW&$@#'
    integer, intent(in) :: i
    integer :: k
    k = mod(i, len(color)) + 1
    icha = color(k:k)
    return
  end function icha
end program cha_mandel