<commit_before>! mandelbrot.f95
! Calculate Mandelbrot sets
!
! vim: set ft=fortran sw=2 ts=2 :

module mandelbrot
  implicit none
  save

  real, parameter :: r_max = 2.0

contains

  ! calculate loop count for a (complex) pixel
  pure integer function pixel_value (z, n_max)
    implicit none

    complex, intent( in ) :: z
    integer, optional, intent( in ) :: n_max

    complex :: z1
    integer :: n, n_end

    if ( present(n_max) ) then
      n_end = n_max
    else
      n_end = 256
    end if
    z1 = 0

    do n = 0, n_end
    if (abs(z) > r_max) then
      pixel_value = n
      return
    end if
    z1 = z1**2 + z
    end do

    pixel_value = 0

  end function pixel_value

  ! convert pixel value to RGB
  pure function to_rgb (n)
    use color_map
    implicit none

    integer, dimension(3) :: to_rgb
    integer, intent( in ) :: n

    to_rgb = cm(:,n)

  end function to_rgb

  ! generate mandelbrot set
  function image(width, height, x_center, y_center, pixel_size)
    use ISO_FORTRAN_ENV, only: ERROR_UNIT
    implicit none

    integer, dimension(:,:,:), allocatable :: image
    integer, intent( in ) :: width, height
    real, intent( in ) :: x_center, y_center
    real, intent( in ) :: pixel_size

    integer :: stat, x, y
    complex :: offset, coord

    allocate(image(3, width, height), stat=stat)
    if (stat /= 0) then
      write (ERROR_UNIT,*) "Error allocating data: ", stat
      stop -1
    end if

    offset = cmplx(x_center - 0.5*pixel_size*width, &
      y_center + 0.5*pixel_size*height)

    do x = 1, width
    do y = 1, height
    coord = offset + cmplx(x*pixel_size, y *pixel_size)
    image(:,x,y) = to_rgb(pixel_value(coord, 256))
    end do
    end do

  end function image

  ! write imaeg data as ppm-file<commit_msg>! change y-coordinate direction<commit_after>! mandelbrot.f95
! Calculate Mandelbrot sets
!
! vim: set ft=fortran sw=2 ts=2 :

module mandelbrot
  implicit none
  save

  real, parameter :: r_max = 2.0

contains

  ! calculate loop count for a (complex) pixel
  pure integer function pixel_value (z, n_max)
    implicit none

    complex, intent( in ) :: z
    integer, optional, intent( in ) :: n_max

    complex :: z1
    integer :: n, n_end

    if ( present(n_max) ) then
      n_end = n_max
    else
      n_end = 256
    end if
    z1 = 0

    do n = 0, n_end
    if (abs(z) > r_max) then
      pixel_value = n
      return
    end if
    z1 = z1**2 + z
    end do

    pixel_value = 0

  end function pixel_value

  ! convert pixel value to RGB
  pure function to_rgb (n)
    use color_map
    implicit none

    integer, dimension(3) :: to_rgb
    integer, intent( in ) :: n

    to_rgb = cm(:,n+1)

  end function to_rgb

  ! generate mandelbrot set
  function image(width, height, x_center, y_center, pixel_size)
    use ISO_FORTRAN_ENV, only: ERROR_UNIT
    implicit none

    integer, dimension(:,:,:), allocatable :: image
    integer, intent( in ) :: width, height
    real, intent( in ) :: x_center, y_center
    real, intent( in ) :: pixel_size

    integer :: stat, x, y
    complex :: offset, coord

    allocate(image(3, width, height), stat=stat)
    if (stat /= 0) then
      write (ERROR_UNIT,*) "Error allocating data: ", stat
      stop -1
    end if

    offset = cmplx(x_center - 0.5*pixel_size*width, &
      y_center + 0.5*pixel_size*height)

    do x = 1, width
    do y = 1, height
    coord = offset + cmplx(x*pixel_size, -y*pixel_size)
    image(:,x,y) = to_rgb(pixel_value(coord, 256))
    end do
    end do

  end function image

  ! write imaeg data as ppm-file
{'fraction_non_alphanumeric': 0.10184667039731393, 'fraction_numerical': 0.01706771124790151, 'mean_word_length': 2.557213930348259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '905166', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1246, 'n_words': 401}