Section Arrays: Linear Transform#

Adapted from: gjbex/Fortran-MOOC

This program demonstrates an elemental function with multiple arguments in Fortran.#

program linear_transform
  implicit none
  real, dimension(5) :: values
  real, dimension(2) :: a = [ 1.1, 2.2 ], &
                        x = [ 0.5, 2.0 ], &
                        b = [ -1.2, 2.5 ]
  integer :: i

  call random_number(values)
  print *, "The contents of the valus array is:"
  print *, values
  print *, "lin_transform(values, 2.0, 1.0) = "
  print *, lin_transform(values, 2.0, 1.0)
  print *, "lin_transform(x, a, b) = "
  print *, lin_transform(x, a, b)

contains

  elemental real function lin_transform(x, a, b)
      implicit none
      real, intent(in) :: x, a, b

      lin_transform = a*x + b
  end function lin_transform

end program linear_transform

The above program is compiled and run using Fortran Package Manager (fpm):

Build the Program using FPM (Fortran Package Manager)#

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_Arrays_Linear_Transform"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")

Run the Program using FPM (Fortran Package Manager)#

exec_status = os.system("fpm run 2>/dev/null")
 The contents of the valus array is:
  0.562747896      0.888178587      0.920305490      0.607871950      0.339282036    
 lin_transform(values, 2.0, 1.0) = 
   2.12549591       2.77635717       2.84061098       2.21574402       1.67856407    
 lin_transform(x, a, b) = 
 -0.650000036       6.90000010    
exec_status = os.system("fpm run 2>/dev/null")
 The contents of the valus array is:
  0.539215922      0.786644161       9.72322226E-02  0.840005934      0.665701985    
 lin_transform(values, 2.0, 1.0) = 
   2.07843184       2.57328844       1.19446445       2.68001175       2.33140397    
 lin_transform(x, a, b) = 
 -0.650000036       6.90000010