From f0f3c5f88a67a6a352d56ec7ef171ece75dae99f Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Tue, 24 Sep 2024 13:07:41 +0200 Subject: [PATCH 1/6] Implemented several numerical integration algorithms --- DIRECTORY.md | 6 ++ .../gaussian_legendre.f90 | 37 +++++++ .../maths/numerical_integration/midpoint.f90 | 36 +++++++ .../numerical_integration/monte_carlo.f90 | 37 +++++++ .../maths/numerical_integration/simpson.f90 | 37 +++++++ .../maths/numerical_integration/trapezoid.f90 | 37 +++++++ .../gaussian_legendre.f90 | 100 ++++++++++++++++++ .../maths/numerical_integration/midpoint.f90 | 65 ++++++++++++ .../numerical_integration/monte_carlo.f90 | 74 +++++++++++++ .../maths/numerical_integration/simpson.f90 | 72 +++++++++++++ .../maths/numerical_integration/trapezoid.f90 | 65 ++++++++++++ 11 files changed, 566 insertions(+) create mode 100644 examples/maths/numerical_integration/gaussian_legendre.f90 create mode 100644 examples/maths/numerical_integration/midpoint.f90 create mode 100644 examples/maths/numerical_integration/monte_carlo.f90 create mode 100644 examples/maths/numerical_integration/simpson.f90 create mode 100644 examples/maths/numerical_integration/trapezoid.f90 create mode 100644 modules/maths/numerical_integration/gaussian_legendre.f90 create mode 100644 modules/maths/numerical_integration/midpoint.f90 create mode 100644 modules/maths/numerical_integration/monte_carlo.f90 create mode 100644 modules/maths/numerical_integration/simpson.f90 create mode 100644 modules/maths/numerical_integration/trapezoid.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 37d0435..9db0263 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1,5 +1,11 @@ # The Algorithms - Directory of Fortran ## Maths +* numerical_integration + * [trapezoidal_rule](/modules/maths/numerical_integration/trapezoid.f90) + * [simpson_rule](/modules/maths/numerical_integration/simpson.f90) + * [midpoint_rule](/modules/maths/numerical_integration/midpoint.f90) + * [monte_carlo](/modules/maths/numerical_integration/monte_carlo.f90) + * [gauss_legendre](/modules/maths/numerical_integration/gaussian_legendre.f90) * [euclid_gcd](/modules/maths/euclid_gcd.f90) * [factorial](/modules/maths/factorial.f90) * [fibonacci](/modules/maths/fibonacci.f90) diff --git a/examples/maths/numerical_integration/gaussian_legendre.f90 b/examples/maths/numerical_integration/gaussian_legendre.f90 new file mode 100644 index 0000000..c85c9c4 --- /dev/null +++ b/examples/maths/numerical_integration/gaussian_legendre.f90 @@ -0,0 +1,37 @@ +!> Example Program for Gaussian-Legendre Quadrature Module +!! This program demonstrates the use of Gaussian-Legendre Quadrature Module for numerical integration. +!! +!! It sets the integration limits and the number of quadrature points (n), and calls the +!! gauss_legendre_quadrature subroutine to compute the approximate value of the definite integral +!! of the specified function. +!! +!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x) + +program example_gaussian_quadrature + use gaussian_legendre_quadrature + implicit none + + real(dp) :: a, b, integral_result + integer :: n + + ! Set the integration limits and number of quadrature points + a = -1.0_dp + b = 1.0_dp + n = 5 !! Number of quadrature points + + ! Call Gaussian quadrature to compute the integral + call gauss_legendre_quadrature(integral_result, a, b, n, func) + + write(*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574 + +contains + + function func(x) result(fx) + implicit none + real(dp), intent(in) :: x + real(dp) :: fx + + fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + end function func + +end program example_gaussian_quadrature \ No newline at end of file diff --git a/examples/maths/numerical_integration/midpoint.f90 b/examples/maths/numerical_integration/midpoint.f90 new file mode 100644 index 0000000..3869da4 --- /dev/null +++ b/examples/maths/numerical_integration/midpoint.f90 @@ -0,0 +1,36 @@ +!> Example Program for Midpoint Rule +!! This program demonstrates the use of Midpoint Rule for numerical integration. +!! +!! It sets the integration limits and number of subintervals (panels), and calls the +!! midpoint subroutine to compute the approximate value of the definite integral +!! of the specified function. +!! +!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x) + +program example_midpoint + use midpoint_rule + implicit none + real(dp) :: a, b, integral_result + integer :: n + + ! Set the integration limits and number of panels + a = -1.0_dp + b = 1.0_dp + n = 400 !! Number of subdivisions + + ! Call the midpoint rule subroutine with the function passed as an argument + call midpoint(integral_result, a, b, n, func) + + write(*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196 + +contains + + function func(x) result(fx) + implicit none + real(dp), intent(in) :: x + real(dp) :: fx + + fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + end function func + +end program example_midpoint diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 new file mode 100644 index 0000000..31cebbc --- /dev/null +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -0,0 +1,37 @@ +!> Example Program for Monte Carlo Integration +!! This program demonstrates the use of Monte Carlo module for numerical integration. +!! +!! It sets the integration limits and number of random samples, and calls the +!! monte_carlo subroutine to compute the approximate value of the definite integral +!! of the specified function. +!! +!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x) + +program example_monte_carlo + use monte_carlo_integration + implicit none + + real(dp) :: a, b, integral_result, error_estimate + integer :: n + + ! Set the integration limits and number of random samples + a = -1.0_dp + b = 1.0_dp + n = 1E6 !! Number of random samples + + ! Call Monte Carlo integration + call monte_carlo(integral_result, error_estimate, a, b, n, func) + + write(*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421 + +contains + + function func(x) result(fx) + implicit none + real(dp), intent(in) :: x + real(dp) :: fx + + fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + end function func + +end program example_monte_carlo diff --git a/examples/maths/numerical_integration/simpson.f90 b/examples/maths/numerical_integration/simpson.f90 new file mode 100644 index 0000000..4b7a437 --- /dev/null +++ b/examples/maths/numerical_integration/simpson.f90 @@ -0,0 +1,37 @@ +!> Example Program for Simpson's Rule +!! This program demonstrates the use of Simpson's rule for numerical integration. +!! +!! It sets the integration limits and number of panels, and calls the +!! simpson subroutine to compute the approximate value of the definite integral +!! of the specified function. +!! +!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x) + +program example_simpson + use simpson_rule + implicit none + + real(dp) :: a, b, integral_result + integer :: n + + ! Set the integration limits and number of panels + a = -1.0_dp + b = 1.0_dp + n = 100 !! Number of subdivisions (must be even) + + ! Call Simpson's rule with the function passed as an argument + call simpson(integral_result, a, b, n, func) + + write(*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555 + +contains + + function func(x) result(fx) + implicit none + real(dp), intent(in) :: x + real(dp) :: fx + + fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + end function func + +end program example_simpson diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 new file mode 100644 index 0000000..5c08c14 --- /dev/null +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -0,0 +1,37 @@ +!> Example Program for Trapezoidal Rule +!! This program demonstrates the use of the Trapezoidal rule for numerical integration. +!! +!! It sets the integration limits and number of panels, and calls the +!! trapezoid subroutine to compute the approximate value of the definite integral +!! of the specified function. +!! +!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x) + +program example_tapezoid + use trapezoidal_rule + implicit none + + real(dp) :: a, b, integral_result + integer :: n + + ! Set the integration limits and number of panels + a = -1.0_dp + b = 1.0_dp + n = 1E6 !! Number of subdivisions + + ! Call the trapezoidal rule with the function passed as an argument + call trapezoid(integral_result, a, b, n, func) + + write(*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195 + +contains + + function func(x) result(fx) + implicit none + real(dp), intent(in) :: x + real(dp) :: fx + + fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + end function func + +end program example_tapezoid diff --git a/modules/maths/numerical_integration/gaussian_legendre.f90 b/modules/maths/numerical_integration/gaussian_legendre.f90 new file mode 100644 index 0000000..bc4c678 --- /dev/null +++ b/modules/maths/numerical_integration/gaussian_legendre.f90 @@ -0,0 +1,100 @@ +!> Gaussian-Legendre Quadrature Module +!! +!! This module provides the implementation of Gaussian-Legendre Quadrature. +!! +!! The method approximates the definite integral of a function over a specified interval [a, b]. +!! +!! The quadrature method works by transforming nodes and weights from the reference interval [-1, 1] to the +!! interval [a, b] and then evaluating the function at these nodes. The integral is then approximated by summing +!! the product of function values and corresponding weights. +!! +!! Contents: +!! - `gauss_legendre_quadrature`: A subroutine to perform Gaussian-Legendre quadrature using provided nodes and weights. +!! - `gauss_legendre_weights`: A helper subroutine to initialize the quadrature nodes and weights for different orders (n). +!! +!! Input: +!! - `a`: Lower bound of integration (real(dp)) +!! - `b`: Upper bound of integration (real(dp)) +!! - `n`: Number of quadrature points (integer) +!! - `func`: The function to integrate (interface) +!! +!! Output: +!! - `integral_result`: Approximate value of the integral (real(dp)) + +module gaussian_legendre_quadrature + implicit none + integer, parameter :: dp = kind(1.0d0) !! Double precision parameter + +contains + + ! General Gaussian Quadrature for definite integral + subroutine gauss_legendre_quadrature(integral_result, a, b, n, func) + implicit none + real(dp), intent(out) :: integral_result + real(dp), intent(in) :: a, b + integer, intent(in) :: n !! Number of quadrature points (order of accuracy) + + real(dp), dimension(n) :: t, w, x + real(dp), dimension(:), allocatable :: fx + integer :: i + + ! Interface for the function + interface + real(kind(0.d0)) function func(x) result(fx) + real(kind(0.d0)), intent(in) :: x + end function func + end interface + + ! Initialize nodes and weights for Gauss-Legendre quadrature based on n + call gauss_legendre_weights(t, w, n) + + ! Allocate the function value array + allocate(fx(n)) + + ! Transform the nodes from the reference interval [-1, 1] to [a, b] + x = (b + a) / 2.0_dp + (b - a) * t / 2.0_dp + + ! Compute function values at the transformed points + do i = 1, n + fx(i) = func(x(i)) + end do + + ! Apply the Gaussian-Legendre quadrature formula + integral_result = sum(w * fx) * (b - a) / 2.0_dp + + ! Deallocate fx array + deallocate(fx) + + end subroutine gauss_legendre_quadrature + + ! Subroutine to initialize Gauss-Legendre nodes and weights + subroutine gauss_legendre_weights(t, w, n) + implicit none + integer, intent(in) :: n + real(dp), intent(out), dimension(n) :: t, w !! Nodes (t) and weights (w) + + ! Predefined nodes and weights for different values of n + select case(n) + case (1) + t = [0.0_dp] !! Single node at the center for n = 1 + w = [2.0_dp] !! Weight of 2 for the single point + case (2) + t = [-0.5773502692_dp, 0.5773502692_dp] !! Symmetric nodes for n = 2 + w = [1.0_dp, 1.0_dp] !! Equal weights for n = 2 + case (3) + t = [-0.7745966692_dp, 0.0_dp, 0.7745966692_dp] !! Symmetric nodes for n = 3 + w = [0.5555555556_dp, 0.8888888889_dp, 0.5555555556_dp] !! Weights for n = 3 + case (4) + t = [-0.8611363116_dp, -0.3399810436_dp, 0.3399810436_dp, 0.8611363116_dp] !! Nodes for n = 4 + w = [0.3478548451_dp, 0.6521451549_dp, 0.6521451549_dp, 0.3478548451_dp] !! Weights for n = 4 + case (5) + t = [-0.9061798459_dp, -0.5384693101_dp, 0.0_dp, 0.5384693101_dp, 0.9061798459_dp] !! Nodes for n = 5 + w = [0.2369268851_dp, 0.4786286705_dp, 0.5688888889_dp, 0.4786286705_dp, 0.2369268851_dp] !! Weights for n = 5 + ! You can add more cases to support higher values of n. + case default + print *, 'Gauss-Legendre quadrature for n > 5 is not implemented.' + end select + + end subroutine gauss_legendre_weights + +end module gaussian_legendre_quadrature diff --git a/modules/maths/numerical_integration/midpoint.f90 b/modules/maths/numerical_integration/midpoint.f90 new file mode 100644 index 0000000..2612851 --- /dev/null +++ b/modules/maths/numerical_integration/midpoint.f90 @@ -0,0 +1,65 @@ +!> Midpoint rule Module +!! +!! This module implements Midpoint rule for numerical integration. +!! +!! The midpoint rule approximates the integral by calculating the function +!! value at the midpoint of each subinterval and summing these values, multiplied +!! by the width of the subintervals. +!! +!! Note: This implementation is valid for one-dimensional functions +!! +!! Input: +!! - `a`: Lower bound of integration (real(dp)) +!! - `b`: Upper bound of integration (real(dp)) +!! - `n`: Number of panels (integer) +!! - `func`: The function to integrate (interface) +!! +!! Output: +!! - `integral_result`: Approximate value of the integral (real(dp)) + +module midpoint_rule + implicit none + integer, parameter :: dp = kind(0.d0) !! Double precision parameter + +contains + + subroutine midpoint(integral_result, a, b, n, func) + implicit none + integer, intent(in) :: n + real(dp), intent(in) :: a, b + real(dp), intent(out) :: integral_result + + real(dp), dimension(:), allocatable :: x, fx + real(dp) :: h + integer :: i + + ! Interface for the function + interface + real(kind(0.d0)) function func(x) + real(kind(0.d0)), intent(in) :: x + end function func + end interface + + ! Step size + h = (b - a) / (1.0_dp*n) + + ! Allocate array for midpoints + allocate(x(1:n), fx(1:n)) + + ! Calculate midpoints + x = [(a + (i - 0.5_dp) * h, i = 1, n)] + + ! Apply function to each midpoint + do i = 1, n + fx(i) = func(x(i)) + end do + + ! Final integral value + integral_result = h * sum(fx) + + ! Deallocate arrays + deallocate(x, fx) + + end subroutine midpoint + +end module midpoint_rule diff --git a/modules/maths/numerical_integration/monte_carlo.f90 b/modules/maths/numerical_integration/monte_carlo.f90 new file mode 100644 index 0000000..8563e8c --- /dev/null +++ b/modules/maths/numerical_integration/monte_carlo.f90 @@ -0,0 +1,74 @@ +!> Monte Carlo Integration Module +!! +!! This module estimates the integral of a function over a specified range +!! using the Monte Carlo method (with OpenMP parallelization) and provides an error estimate. +!! +!! The method works by randomly sampling points within the integration range [a, b] +!! and evaluating the function at those points to estimate the integral. +!! +!! Note: This implementation is valid for one-dimensional functions +!! +!! Input: +!! - `a`: Lower bound of integration (real(dp)) +!! - `b`: Upper bound of integration (real(dp)) +!! - `n`: Number of random samples (integer) +!! - `func`: The function to integrate (interface) +!! +!! Output: +!! - `integral_result`: Approximate value of the integral (real(dp)) +!! - `error_estimate`: Estimated error of the integral approximation (real(dp)) + +module monte_carlo_integration + use omp_lib !! OpenMP library for parallelization + implicit none + integer, parameter :: dp = kind(0.d0) !! Double precision parameter + +contains + + subroutine monte_carlo(integral_result, error_estimate, a, b, n, func) + implicit none + integer, intent(in) :: n + real(dp), intent(in) :: a, b + real(dp), intent(out) :: integral_result, error_estimate + + real(dp), dimension(:), allocatable :: uniform_sample, fx + real(dp) :: sum_fx, sum_fx_squared + integer :: i + + ! Interface for the function + interface + real(kind(0.d0)) function func(x) + real(kind(0.d0)), intent(in) :: x + end function func + end interface + + ! Allocate arrays for random samples and function values + allocate(uniform_sample(1:n), fx(1:n)) + + ! Generate uniform random points in [a, b] + call random_number(uniform_sample) + uniform_sample = a + (b - a) * uniform_sample !! Scale to the interval [a, b] + + ! Evaluate the function at all random points in parallel + !$omp parallel do !! OpenMP parallelization to distribute the loop across multiple threads + do i = 1, n + fx(i) = func(uniform_sample(i)) + end do + !$omp end parallel do + + ! Sum of function values and sum of function values squared (for error estimation) + sum_fx = sum(fx) + sum_fx_squared = sum(fx**2) + + ! Compute the Monte Carlo estimate of the integral + integral_result = (b - a) * (sum_fx / real(n, dp)) + + ! Estimate the error using the variance of the function values + error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2) / (n-1)) * (b-a) + + ! Deallocate arrays + deallocate(uniform_sample, fx) + + end subroutine monte_carlo + +end module monte_carlo_integration diff --git a/modules/maths/numerical_integration/simpson.f90 b/modules/maths/numerical_integration/simpson.f90 new file mode 100644 index 0000000..1e2052a --- /dev/null +++ b/modules/maths/numerical_integration/simpson.f90 @@ -0,0 +1,72 @@ +!> Simpson's Rule Module +!! +!! This module implements Simpson's rule for numerical integration. +!! +!! Simpson's rule approximates the definite integral of a function by +!! dividing the area under the curve into parabolic segments and summing +!! their areas, providing a higher degree of accuracy than the Trapezoidal rule. +!! +!! Note: This implementation is valid for one-dimensional functions +!! +!! Input: +!! - `a`: Lower bound of the integration (real(dp)) +!! - `b`: Upper bound of the integration (real(dp)) +!! - `n`: Number of panels (integer, must be even) +!! - `func`: The function to integrate (interface) +!! +!! Output: +!! - `integral_result`: Approximate value of the definite integral (real(dp)) +!! + +module simpson_rule + implicit none + integer, parameter :: dp = kind(0.d0) !! Double precision parameter + +contains + + ! Simpson's rule with function passed via interface + subroutine simpson(integral_result, a, b, n, func) + implicit none + integer, intent(in) :: n + real(dp), intent(in) :: a, b + real(dp), intent(out) :: integral_result + + real(dp), dimension(:), allocatable :: x, fx + real(dp) :: h + integer :: i + + ! Interface for the function + interface + real(kind(0.d0)) function func(x) + real(kind(0.d0)), intent(in) :: x + end function func + end interface + + ! Check if n is even + if (mod(n, 2) /= 0) then + write(*, *) 'Error: The number of panels (n) must be even.' + stop + end if + + ! Step size + h = (b - a) / (1.0_dp*n) + + ! Allocate arrays + allocate(x(0:n), fx(0:n)) + + ! Create an array of x values, contains the endpoints and the midpoints. + x = [(a + i * h, i = 0, n)] + + ! Apply the function to each x value + do i = 0, n + fx(i) = func(x(i)) + end do + + ! Apply Simpson's rule using array slicing + integral_result = (fx(0) + fx(n) + 4.0_dp * sum(fx(1: n-1: 2)) + 2.0_dp * sum(fx(2: n-2: 2))) * (h / 3.0_dp) + + ! Deallocate arrays + deallocate(x, fx) + end subroutine simpson + +end module simpson_rule diff --git a/modules/maths/numerical_integration/trapezoid.f90 b/modules/maths/numerical_integration/trapezoid.f90 new file mode 100644 index 0000000..f2f3a11 --- /dev/null +++ b/modules/maths/numerical_integration/trapezoid.f90 @@ -0,0 +1,65 @@ +!> Trapezoidal Rule Module +!! +!! This module implements the Trapezoidal rule for numerical integration. +!! +!! The Trapezoidal rule approximates the definite integral of a function by +!! dividing the area under the curve into trapezoids and summing their areas. +!! +!! Note: This implementation is valid for one-dimensional functions +!! +!! Input: +!! - `a`: Lower bound of the integration (real(dp)) +!! - `b`: Upper bound of the integration (real(dp)) +!! - `n`: Number of panels (integer) +!! - `func`: The function to integrate (interface) +!! +!! Output: +!! - `integral_result`: Approximate value of the definite integral (real(dp)) +!! + +module trapezoidal_rule + implicit none + integer, parameter :: dp = kind(0.d0) !! Double precision parameter + +contains + + ! Trapezoidal rule with function passed via interface + subroutine trapezoid(integral_result, a, b, n, func) + implicit none + integer, intent(in) :: n + real(dp), intent(in) :: a, b + real(dp), intent(out) :: integral_result + + real(dp), dimension(:), allocatable :: x, fx + real(dp) :: h + integer :: i + + ! Interface for the function + interface + real(kind(0.d0)) function func(x) + real(kind(0.d0)), intent(in) :: x + end function func + end interface + + ! Step size + h = (b - a) / (1.0_dp*n) + + ! Allocate arrays + allocate(x(0:n), fx(0:n)) + + ! Create an array of x values + x = [(a + i * h, i = 0, n)] + + ! Apply the function to each x value + do i = 0, n + fx(i) = func(x(i)) + end do + + ! Apply trapezoidal rule using array slicing + integral_result = ((fx(0) + fx(n)) * 0.5_dp + sum(fx(1: n))) * h + + ! Deallocate arrays + deallocate(x, fx) + end subroutine trapezoid + +end module trapezoidal_rule From 12016aa69cfec534ec2313198e96bcc4195520a7 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 25 Sep 2024 00:04:26 +0200 Subject: [PATCH 2/6] fprettify styles --- .../numerical_integration/gaussian_legendre.f90 | 4 ++-- examples/maths/numerical_integration/midpoint.f90 | 4 ++-- examples/maths/numerical_integration/monte_carlo.f90 | 2 +- examples/maths/numerical_integration/simpson.f90 | 4 ++-- examples/maths/numerical_integration/trapezoid.f90 | 4 ++-- .../numerical_integration/gaussian_legendre.f90 | 10 +++++----- modules/maths/numerical_integration/midpoint.f90 | 10 +++++----- modules/maths/numerical_integration/monte_carlo.f90 | 10 +++++----- modules/maths/numerical_integration/simpson.f90 | 12 ++++++------ modules/maths/numerical_integration/trapezoid.f90 | 10 +++++----- 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/examples/maths/numerical_integration/gaussian_legendre.f90 b/examples/maths/numerical_integration/gaussian_legendre.f90 index c85c9c4..e510724 100644 --- a/examples/maths/numerical_integration/gaussian_legendre.f90 +++ b/examples/maths/numerical_integration/gaussian_legendre.f90 @@ -22,7 +22,7 @@ program example_gaussian_quadrature ! Call Gaussian quadrature to compute the integral call gauss_legendre_quadrature(integral_result, a, b, n, func) - write(*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574 + write (*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574 contains @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_gaussian_quadrature \ No newline at end of file diff --git a/examples/maths/numerical_integration/midpoint.f90 b/examples/maths/numerical_integration/midpoint.f90 index 3869da4..4e4ac56 100644 --- a/examples/maths/numerical_integration/midpoint.f90 +++ b/examples/maths/numerical_integration/midpoint.f90 @@ -21,7 +21,7 @@ program example_midpoint ! Call the midpoint rule subroutine with the function passed as an argument call midpoint(integral_result, a, b, n, func) - write(*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196 + write (*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196 contains @@ -30,7 +30,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_midpoint diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 index 31cebbc..15423a0 100644 --- a/examples/maths/numerical_integration/monte_carlo.f90 +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -22,7 +22,7 @@ program example_monte_carlo ! Call Monte Carlo integration call monte_carlo(integral_result, error_estimate, a, b, n, func) - write(*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421 + write (*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421 contains diff --git a/examples/maths/numerical_integration/simpson.f90 b/examples/maths/numerical_integration/simpson.f90 index 4b7a437..7fdaefc 100644 --- a/examples/maths/numerical_integration/simpson.f90 +++ b/examples/maths/numerical_integration/simpson.f90 @@ -22,7 +22,7 @@ program example_simpson ! Call Simpson's rule with the function passed as an argument call simpson(integral_result, a, b, n, func) - write(*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555 + write (*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555 contains @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_simpson diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 index 5c08c14..b46854d 100644 --- a/examples/maths/numerical_integration/trapezoid.f90 +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -22,7 +22,7 @@ program example_tapezoid ! Call the trapezoidal rule with the function passed as an argument call trapezoid(integral_result, a, b, n, func) - write(*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195 + write (*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195 contains @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_tapezoid diff --git a/modules/maths/numerical_integration/gaussian_legendre.f90 b/modules/maths/numerical_integration/gaussian_legendre.f90 index bc4c678..5f288bb 100644 --- a/modules/maths/numerical_integration/gaussian_legendre.f90 +++ b/modules/maths/numerical_integration/gaussian_legendre.f90 @@ -49,10 +49,10 @@ end function func call gauss_legendre_weights(t, w, n) ! Allocate the function value array - allocate(fx(n)) + allocate (fx(n)) ! Transform the nodes from the reference interval [-1, 1] to [a, b] - x = (b + a) / 2.0_dp + (b - a) * t / 2.0_dp + x = (b + a)/2.0_dp + (b - a)*t/2.0_dp ! Compute function values at the transformed points do i = 1, n @@ -60,10 +60,10 @@ end function func end do ! Apply the Gaussian-Legendre quadrature formula - integral_result = sum(w * fx) * (b - a) / 2.0_dp + integral_result = sum(w*fx)*(b - a)/2.0_dp ! Deallocate fx array - deallocate(fx) + deallocate (fx) end subroutine gauss_legendre_quadrature @@ -74,7 +74,7 @@ subroutine gauss_legendre_weights(t, w, n) real(dp), intent(out), dimension(n) :: t, w !! Nodes (t) and weights (w) ! Predefined nodes and weights for different values of n - select case(n) + select case (n) case (1) t = [0.0_dp] !! Single node at the center for n = 1 w = [2.0_dp] !! Weight of 2 for the single point diff --git a/modules/maths/numerical_integration/midpoint.f90 b/modules/maths/numerical_integration/midpoint.f90 index 2612851..85d82e4 100644 --- a/modules/maths/numerical_integration/midpoint.f90 +++ b/modules/maths/numerical_integration/midpoint.f90 @@ -41,13 +41,13 @@ end function func end interface ! Step size - h = (b - a) / (1.0_dp*n) + h = (b - a)/(1.0_dp*n) ! Allocate array for midpoints - allocate(x(1:n), fx(1:n)) + allocate (x(1:n), fx(1:n)) ! Calculate midpoints - x = [(a + (i - 0.5_dp) * h, i = 1, n)] + x = [(a + (i - 0.5_dp)*h, i=1, n)] ! Apply function to each midpoint do i = 1, n @@ -55,10 +55,10 @@ end function func end do ! Final integral value - integral_result = h * sum(fx) + integral_result = h*sum(fx) ! Deallocate arrays - deallocate(x, fx) + deallocate (x, fx) end subroutine midpoint diff --git a/modules/maths/numerical_integration/monte_carlo.f90 b/modules/maths/numerical_integration/monte_carlo.f90 index 8563e8c..8856180 100644 --- a/modules/maths/numerical_integration/monte_carlo.f90 +++ b/modules/maths/numerical_integration/monte_carlo.f90 @@ -43,11 +43,11 @@ end function func end interface ! Allocate arrays for random samples and function values - allocate(uniform_sample(1:n), fx(1:n)) + allocate (uniform_sample(1:n), fx(1:n)) ! Generate uniform random points in [a, b] call random_number(uniform_sample) - uniform_sample = a + (b - a) * uniform_sample !! Scale to the interval [a, b] + uniform_sample = a + (b - a)*uniform_sample !! Scale to the interval [a, b] ! Evaluate the function at all random points in parallel !$omp parallel do !! OpenMP parallelization to distribute the loop across multiple threads @@ -61,13 +61,13 @@ end function func sum_fx_squared = sum(fx**2) ! Compute the Monte Carlo estimate of the integral - integral_result = (b - a) * (sum_fx / real(n, dp)) + integral_result = (b - a)*(sum_fx/real(n, dp)) ! Estimate the error using the variance of the function values - error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2) / (n-1)) * (b-a) + error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2)/(n - 1))*(b - a) ! Deallocate arrays - deallocate(uniform_sample, fx) + deallocate (uniform_sample, fx) end subroutine monte_carlo diff --git a/modules/maths/numerical_integration/simpson.f90 b/modules/maths/numerical_integration/simpson.f90 index 1e2052a..a65b3d2 100644 --- a/modules/maths/numerical_integration/simpson.f90 +++ b/modules/maths/numerical_integration/simpson.f90 @@ -44,18 +44,18 @@ end function func ! Check if n is even if (mod(n, 2) /= 0) then - write(*, *) 'Error: The number of panels (n) must be even.' + write (*, *) 'Error: The number of panels (n) must be even.' stop end if ! Step size - h = (b - a) / (1.0_dp*n) + h = (b - a)/(1.0_dp*n) ! Allocate arrays - allocate(x(0:n), fx(0:n)) + allocate (x(0:n), fx(0:n)) ! Create an array of x values, contains the endpoints and the midpoints. - x = [(a + i * h, i = 0, n)] + x = [(a + i*h, i=0, n)] ! Apply the function to each x value do i = 0, n @@ -63,10 +63,10 @@ end function func end do ! Apply Simpson's rule using array slicing - integral_result = (fx(0) + fx(n) + 4.0_dp * sum(fx(1: n-1: 2)) + 2.0_dp * sum(fx(2: n-2: 2))) * (h / 3.0_dp) + integral_result = (fx(0) + fx(n) + 4.0_dp*sum(fx(1:n - 1:2)) + 2.0_dp*sum(fx(2:n - 2:2)))*(h/3.0_dp) ! Deallocate arrays - deallocate(x, fx) + deallocate (x, fx) end subroutine simpson end module simpson_rule diff --git a/modules/maths/numerical_integration/trapezoid.f90 b/modules/maths/numerical_integration/trapezoid.f90 index f2f3a11..8f13d7c 100644 --- a/modules/maths/numerical_integration/trapezoid.f90 +++ b/modules/maths/numerical_integration/trapezoid.f90 @@ -42,13 +42,13 @@ end function func end interface ! Step size - h = (b - a) / (1.0_dp*n) + h = (b - a)/(1.0_dp*n) ! Allocate arrays - allocate(x(0:n), fx(0:n)) + allocate (x(0:n), fx(0:n)) ! Create an array of x values - x = [(a + i * h, i = 0, n)] + x = [(a + i*h, i=0, n)] ! Apply the function to each x value do i = 0, n @@ -56,10 +56,10 @@ end function func end do ! Apply trapezoidal rule using array slicing - integral_result = ((fx(0) + fx(n)) * 0.5_dp + sum(fx(1: n))) * h + integral_result = ((fx(0) + fx(n))*0.5_dp + sum(fx(1:n)))*h ! Deallocate arrays - deallocate(x, fx) + deallocate (x, fx) end subroutine trapezoid end module trapezoidal_rule From 10f31032ae8916dea7f83ba73162c70cc2b1c4b2 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Fri, 27 Sep 2024 09:11:31 +0200 Subject: [PATCH 3/6] fprettify styles in example files --- examples/maths/numerical_integration/gaussian_legendre.f90 | 4 ++-- examples/maths/numerical_integration/midpoint.f90 | 2 +- examples/maths/numerical_integration/monte_carlo.f90 | 2 +- examples/maths/numerical_integration/simpson.f90 | 2 +- examples/maths/numerical_integration/trapezoid.f90 | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/maths/numerical_integration/gaussian_legendre.f90 b/examples/maths/numerical_integration/gaussian_legendre.f90 index e510724..f7752d5 100644 --- a/examples/maths/numerical_integration/gaussian_legendre.f90 +++ b/examples/maths/numerical_integration/gaussian_legendre.f90 @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate end function func -end program example_gaussian_quadrature \ No newline at end of file +end program example_gaussian_quadrature diff --git a/examples/maths/numerical_integration/midpoint.f90 b/examples/maths/numerical_integration/midpoint.f90 index 4e4ac56..9ce83b5 100644 --- a/examples/maths/numerical_integration/midpoint.f90 +++ b/examples/maths/numerical_integration/midpoint.f90 @@ -30,7 +30,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate end function func end program example_midpoint diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 index 15423a0..a4ea6da 100644 --- a/examples/maths/numerical_integration/monte_carlo.f90 +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate end function func end program example_monte_carlo diff --git a/examples/maths/numerical_integration/simpson.f90 b/examples/maths/numerical_integration/simpson.f90 index 7fdaefc..a7fbd07 100644 --- a/examples/maths/numerical_integration/simpson.f90 +++ b/examples/maths/numerical_integration/simpson.f90 @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate end function func end program example_simpson diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 index b46854d..a6e9983 100644 --- a/examples/maths/numerical_integration/trapezoid.f90 +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate end function func end program example_tapezoid From 4602fd7eb2e7189629d51fd276afad78f0d1c3e8 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Fri, 27 Sep 2024 16:10:11 +0200 Subject: [PATCH 4/6] Considered `-Wconversion-extra` compilation flag. Synced other files. --- examples/maths/factorial.f90 | 4 +- .../numerical_integration/monte_carlo.f90 | 2 +- .../maths/numerical_integration/trapezoid.f90 | 3 +- examples/searches/example_linear_search.f90 | 6 +- examples/searches/recursive_linear_search.f90 | 6 +- .../sorts/example_recursive_bubble_sort.f90 | 4 +- examples/sorts/example_usage_bubble_sort.f90 | 4 +- examples/sorts/example_usage_gnome_sort.f90 | 2 +- examples/sorts/example_usage_heap_sort.f90 | 2 +- examples/sorts/example_usage_merge_sort.f90 | 2 +- examples/sorts/example_usage_quick_sort.f90 | 2 +- examples/sorts/example_usage_radix_sort.f90 | 6 +- modules/maths/factorial.f90 | 4 +- .../maths/numerical_integration/midpoint.f90 | 4 +- .../numerical_integration/monte_carlo.f90 | 4 +- .../maths/numerical_integration/simpson.f90 | 4 +- .../maths/numerical_integration/trapezoid.f90 | 4 +- modules/searches/linear_search.f90 | 2 +- modules/searches/recursive_linear_search.f90 | 4 +- modules/sorts/bubble_sort.f90 | 8 +-- modules/sorts/heap_sort.f90 | 8 +-- modules/sorts/merge_sort.f90 | 14 ++-- modules/sorts/radix_sort.f90 | 14 ++-- tests/sorts/tests_gnome_sort.f90 | 46 +++++++------ tests/sorts/tests_heap_sort.f90 | 46 +++++++------ tests/sorts/tests_merge_sort.f90 | 46 +++++++------ tests/sorts/tests_quick_sort.f90 | 46 +++++++------ tests/sorts/tests_radix_sort.f90 | 65 +++++++++---------- 28 files changed, 178 insertions(+), 184 deletions(-) diff --git a/examples/maths/factorial.f90 b/examples/maths/factorial.f90 index 7d16eb7..af12be6 100644 --- a/examples/maths/factorial.f90 +++ b/examples/maths/factorial.f90 @@ -7,7 +7,7 @@ program factorial_program use factorial_module implicit none - Print*, factorial(5) - Print*, recursive_factorial(5) + Print *, factorial(5) + Print *, recursive_factorial(5) end program factorial_program diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 index a4ea6da..818a337 100644 --- a/examples/maths/numerical_integration/monte_carlo.f90 +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -17,7 +17,7 @@ program example_monte_carlo ! Set the integration limits and number of random samples a = -1.0_dp b = 1.0_dp - n = 1E6 !! Number of random samples + n = 1000000 !! 1E6 Number of random samples ! Call Monte Carlo integration call monte_carlo(integral_result, error_estimate, a, b, n, func) diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 index a6e9983..fb9090a 100644 --- a/examples/maths/numerical_integration/trapezoid.f90 +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -1,4 +1,5 @@ !> Example Program for Trapezoidal Rule +!! !! This program demonstrates the use of the Trapezoidal rule for numerical integration. !! !! It sets the integration limits and number of panels, and calls the @@ -17,7 +18,7 @@ program example_tapezoid ! Set the integration limits and number of panels a = -1.0_dp b = 1.0_dp - n = 1E6 !! Number of subdivisions + n = 1000000 !! 1E6 Number of subdivisions ! Call the trapezoidal rule with the function passed as an argument call trapezoid(integral_result, a, b, n, func) diff --git a/examples/searches/example_linear_search.f90 b/examples/searches/example_linear_search.f90 index 6e3a24d..2d7425c 100644 --- a/examples/searches/example_linear_search.f90 +++ b/examples/searches/example_linear_search.f90 @@ -8,12 +8,12 @@ program linear_search_program integer, dimension(5) :: array - array = (/ 540, 6, 10, 100, 3 /) + array = (/540, 6, 10, 100, 3/) !! Search for the number 6 in array. - print*, "Target = 6: ", linear_search(array, 6) !! Prints 2. + print *, "Target = 6: ", linear_search(array, 6) !! Prints 2. !! Search for the number 5 in array. - print*, "Target = 5: ", linear_search(array, 5) !! Prints -1 because item 5 is not found. + print *, "Target = 5: ", linear_search(array, 5) !! Prints -1 because item 5 is not found. end program linear_search_program diff --git a/examples/searches/recursive_linear_search.f90 b/examples/searches/recursive_linear_search.f90 index e616ab4..1251ea4 100644 --- a/examples/searches/recursive_linear_search.f90 +++ b/examples/searches/recursive_linear_search.f90 @@ -8,12 +8,12 @@ program recursive_linear_search_example integer, dimension(5) :: array - array = (/ 306, 1005, 5, 62, 0 /) + array = (/306, 1005, 5, 62, 0/) !! Search for the number 62 in the array - print*, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4. + print *, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4. !! Search for the number 10 in the array - print*, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found. + print *, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found. end program recursive_linear_search_example diff --git a/examples/sorts/example_recursive_bubble_sort.f90 b/examples/sorts/example_recursive_bubble_sort.f90 index 1afcd11..52b2cba 100644 --- a/examples/sorts/example_recursive_bubble_sort.f90 +++ b/examples/sorts/example_recursive_bubble_sort.f90 @@ -11,11 +11,11 @@ program recursive_bubble_sort_example !! Fill the array with random numbers. call random_number(array) - print*, "Before:", array + print *, "Before:", array !! Bubble sort subroutine call. call recursive_bubble_sort(array, size(array)) - print*, "After:", array + print *, "After:", array end program recursive_bubble_sort_example diff --git a/examples/sorts/example_usage_bubble_sort.f90 b/examples/sorts/example_usage_bubble_sort.f90 index 5e9a96b..13d5349 100644 --- a/examples/sorts/example_usage_bubble_sort.f90 +++ b/examples/sorts/example_usage_bubble_sort.f90 @@ -11,11 +11,11 @@ program bubble_sort_example !! Fill the array with random numbers call random_number(array) - print*, "Before:", array + print *, "Before:", array !! Call the bubble_sort subroutine to sort the array call bubble_sort(array) - print*, "After:", array + print *, "After:", array end program bubble_sort_example diff --git a/examples/sorts/example_usage_gnome_sort.f90 b/examples/sorts/example_usage_gnome_sort.f90 index d9cd0c4..5c2cb15 100644 --- a/examples/sorts/example_usage_gnome_sort.f90 +++ b/examples/sorts/example_usage_gnome_sort.f90 @@ -9,7 +9,7 @@ program test_gnome_sort integer :: n, i ! Initialize the test array - array = (/ -5, 2, 9, 1, 5, 6, -7, 8, 15, -20 /) + array = (/-5, 2, 9, 1, 5, 6, -7, 8, 15, -20/) n = size(array) ! Call gnome_sort from the module to sort the array diff --git a/examples/sorts/example_usage_heap_sort.f90 b/examples/sorts/example_usage_heap_sort.f90 index 8f3fd9a..24c705f 100644 --- a/examples/sorts/example_usage_heap_sort.f90 +++ b/examples/sorts/example_usage_heap_sort.f90 @@ -10,7 +10,7 @@ program test_heap_sort integer, dimension(n) :: array(n) ! Test array ! Initialize the test array - array = (/ 12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1 /) + array = (/12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1/) ! Print the original array print *, "Original array:" diff --git a/examples/sorts/example_usage_merge_sort.f90 b/examples/sorts/example_usage_merge_sort.f90 index 4991e8a..79893c3 100644 --- a/examples/sorts/example_usage_merge_sort.f90 +++ b/examples/sorts/example_usage_merge_sort.f90 @@ -9,7 +9,7 @@ program test_merge_sort integer :: n, i ! Initialize the test array - array = (/ -2, 3, -10, 11, 99, 100000, 100, -200 /) + array = (/-2, 3, -10, 11, 99, 100000, 100, -200/) n = size(array) ! Call merge_sort from the module to sort the array diff --git a/examples/sorts/example_usage_quick_sort.f90 b/examples/sorts/example_usage_quick_sort.f90 index 1c5bae2..c61b6dd 100644 --- a/examples/sorts/example_usage_quick_sort.f90 +++ b/examples/sorts/example_usage_quick_sort.f90 @@ -9,7 +9,7 @@ program test_quick_sort integer :: n, i ! Initialize the test array - array = (/ 10, 7, 8, 9, 1, 5, -2, 12, 0, -5 /) + array = (/10, 7, 8, 9, 1, 5, -2, 12, 0, -5/) n = size(array) ! Print the original array diff --git a/examples/sorts/example_usage_radix_sort.f90 b/examples/sorts/example_usage_radix_sort.f90 index 3424060..811819a 100644 --- a/examples/sorts/example_usage_radix_sort.f90 +++ b/examples/sorts/example_usage_radix_sort.f90 @@ -13,7 +13,7 @@ program test_radix_sort ! Test for base 10 print *, "Testing Radix Sort with base 10:" - array = (/ 170, 45, 75, 90, 802, 24, 2, 66, 15, 40 /) + array = (/170, 45, 75, 90, 802, 24, 2, 66, 15, 40/) n = size(array) call radix_sort(array, n, base10) print *, "Sorted array in base 10:" @@ -23,7 +23,7 @@ program test_radix_sort ! Test for base 2 print *, "Testing Radix Sort with base 2:" - array = (/ 1010, 1101, 1001, 1110, 0010, 0101, 1111, 0110, 1000, 0001 /) ! Binary values whose decimal: (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /) + array = (/1010, 1101, 1001, 1110, 0010, 0101, 1111, 0110, 1000, 0001/) ! Binary values whose decimal: (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /) n = size(array) call radix_sort(array, n, base2) print *, "Sorted binary array in Decimal:" @@ -33,7 +33,7 @@ program test_radix_sort ! Test for base 16 print *, "Testing Radix Sort with base 16:" - array = (/ 171, 31, 61, 255, 16, 5, 211, 42, 180, 0 /) ! Hexadecimal values as decimal + array = (/171, 31, 61, 255, 16, 5, 211, 42, 180, 0/) ! Hexadecimal values as decimal n = size(array) call radix_sort(array, n, base16) print *, "Sorted hexadecimal array in Decimal:" diff --git a/modules/maths/factorial.f90 b/modules/maths/factorial.f90 index 3f6bcf5..c23f259 100644 --- a/modules/maths/factorial.f90 +++ b/modules/maths/factorial.f90 @@ -19,7 +19,7 @@ function factorial(number) result(factorial_number) factorial_number = 1 do while (counter > 1) - factorial_number = factorial_number * counter + factorial_number = factorial_number*counter counter = counter - 1 end do @@ -33,7 +33,7 @@ recursive function recursive_factorial(number) result(factorial_number) if (number .lt. 1) then factorial_number = 1 else - factorial_number = number * recursive_factorial(number - 1) + factorial_number = number*recursive_factorial(number - 1) end if end function recursive_factorial diff --git a/modules/maths/numerical_integration/midpoint.f90 b/modules/maths/numerical_integration/midpoint.f90 index 85d82e4..b7e034b 100644 --- a/modules/maths/numerical_integration/midpoint.f90 +++ b/modules/maths/numerical_integration/midpoint.f90 @@ -41,13 +41,13 @@ end function func end interface ! Step size - h = (b - a)/(1.0_dp*n) + h = (b - a)/real(n, dp) ! Allocate array for midpoints allocate (x(1:n), fx(1:n)) ! Calculate midpoints - x = [(a + (i - 0.5_dp)*h, i=1, n)] + x = [(a + (real(i, dp) - 0.5_dp)*h, i=1, n)] ! Apply function to each midpoint do i = 1, n diff --git a/modules/maths/numerical_integration/monte_carlo.f90 b/modules/maths/numerical_integration/monte_carlo.f90 index 8856180..33b970e 100644 --- a/modules/maths/numerical_integration/monte_carlo.f90 +++ b/modules/maths/numerical_integration/monte_carlo.f90 @@ -28,6 +28,7 @@ module monte_carlo_integration subroutine monte_carlo(integral_result, error_estimate, a, b, n, func) implicit none integer, intent(in) :: n + real(dp) :: n_dp !! Hold n as double precision real(dp), intent(in) :: a, b real(dp), intent(out) :: integral_result, error_estimate @@ -64,7 +65,8 @@ end function func integral_result = (b - a)*(sum_fx/real(n, dp)) ! Estimate the error using the variance of the function values - error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2)/(n - 1))*(b - a) + n_dp = real(n, dp) + error_estimate = sqrt((sum_fx_squared/n_dp - (sum_fx/n_dp)**2)/(n_dp - 1))*(b - a) ! Deallocate arrays deallocate (uniform_sample, fx) diff --git a/modules/maths/numerical_integration/simpson.f90 b/modules/maths/numerical_integration/simpson.f90 index a65b3d2..b8ad07e 100644 --- a/modules/maths/numerical_integration/simpson.f90 +++ b/modules/maths/numerical_integration/simpson.f90 @@ -49,13 +49,13 @@ end function func end if ! Step size - h = (b - a)/(1.0_dp*n) + h = (b - a)/real(n, dp) ! Allocate arrays allocate (x(0:n), fx(0:n)) ! Create an array of x values, contains the endpoints and the midpoints. - x = [(a + i*h, i=0, n)] + x = [(a + (real(i, dp))*h, i=0, n)] ! Apply the function to each x value do i = 0, n diff --git a/modules/maths/numerical_integration/trapezoid.f90 b/modules/maths/numerical_integration/trapezoid.f90 index 8f13d7c..43f12eb 100644 --- a/modules/maths/numerical_integration/trapezoid.f90 +++ b/modules/maths/numerical_integration/trapezoid.f90 @@ -42,13 +42,13 @@ end function func end interface ! Step size - h = (b - a)/(1.0_dp*n) + h = (b - a)/real(n, dp) ! Allocate arrays allocate (x(0:n), fx(0:n)) ! Create an array of x values - x = [(a + i*h, i=0, n)] + x = [(a + (real(i, dp))*h, i=0, n)] ! Apply the function to each x value do i = 0, n diff --git a/modules/searches/linear_search.f90 b/modules/searches/linear_search.f90 index 05298fb..c314b51 100644 --- a/modules/searches/linear_search.f90 +++ b/modules/searches/linear_search.f90 @@ -8,7 +8,7 @@ module linear_search_module !! This function searches for a target in a given collection. !! Returns the index of the found target or -1 if target is not found. - function linear_search (collection, target) result(target_index) + function linear_search(collection, target) result(target_index) integer, dimension(:), intent(in) :: collection !! A collection for elements of type integer integer, intent(in) :: target !! Target value to be searched. integer :: target_index !! Target's index in the collection to return. diff --git a/modules/searches/recursive_linear_search.f90 b/modules/searches/recursive_linear_search.f90 index 29cfe38..49a21aa 100644 --- a/modules/searches/recursive_linear_search.f90 +++ b/modules/searches/recursive_linear_search.f90 @@ -26,9 +26,9 @@ recursive function recursive_linear_search(collection, index, target) result(tar target_index = index else !! Recursively search in the remaining part of the collection - target_index = recursive_linear_search(collection, index-1, target) + target_index = recursive_linear_search(collection, index - 1, target) end if end function recursive_linear_search -end module recursive_linear_search_module \ No newline at end of file +end module recursive_linear_search_module diff --git a/modules/sorts/bubble_sort.f90 b/modules/sorts/bubble_sort.f90 index b9cfb9f..3d29566 100644 --- a/modules/sorts/bubble_sort.f90 +++ b/modules/sorts/bubble_sort.f90 @@ -8,7 +8,7 @@ module bubble_sort_module contains !! This subroutine sorts the collection using bubble sort. - subroutine bubble_sort (collection) + subroutine bubble_sort(collection) real, dimension(:), intent(inout) :: collection !! A collection of real numbers to be sorted integer :: i, j, collection_size @@ -24,11 +24,11 @@ subroutine bubble_sort (collection) swapped = .false. do i = 1, j - if (collection(i) .gt. collection(i+1)) then + if (collection(i) .gt. collection(i + 1)) then !! Swap values if they are out of order in [i, i+1] region temp = collection(i) - collection(i) = collection(i+1) - collection(i+1) = temp + collection(i) = collection(i + 1) + collection(i + 1) = temp swapped = .true. !! Set swapped flag to true end if diff --git a/modules/sorts/heap_sort.f90 b/modules/sorts/heap_sort.f90 index 9ba1bf5..46b05d5 100644 --- a/modules/sorts/heap_sort.f90 +++ b/modules/sorts/heap_sort.f90 @@ -26,7 +26,7 @@ subroutine heap_sort(array, n) integer :: i ! Build the max heap - do i = n / 2, 1, -1 + do i = n/2, 1, -1 call heapify(array, n, i) end do @@ -50,8 +50,8 @@ recursive subroutine heapify(array, n, i) integer :: largest, left, right largest = i - left = 2 * i - right = 2 * i + 1 + left = 2*i + right = 2*i + 1 ! Is Left Child is larger than Root? if (left <= n .and. array(left) > array(largest)) then @@ -84,4 +84,4 @@ subroutine swap(array, i, j) end subroutine swap -end module heap_sort_module \ No newline at end of file +end module heap_sort_module diff --git a/modules/sorts/merge_sort.f90 b/modules/sorts/merge_sort.f90 index 43d3c6b..59c90f2 100644 --- a/modules/sorts/merge_sort.f90 +++ b/modules/sorts/merge_sort.f90 @@ -1,5 +1,5 @@ !> Merge Sort Algorithm - +!! !> This module implements the Merge Sort algorithm. !! !! Merge Sort is a divide-and-conquer algorithm. It divides the input array into two halves, recursively sorts them, @@ -14,7 +14,7 @@ !! - A sorted array of integers. !! module merge_sort_module -implicit none + implicit none contains @@ -23,21 +23,21 @@ recursive subroutine merge_sort(array, n) implicit none integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted integer, intent(in) :: n ! Size of the array - integer :: middle, i + integer :: middle integer, dimension(:), allocatable :: left_half, right_half, sorted_array ! Base case: return if the array has 1 or fewer elements if (n <= 1) return ! Calculate the middle point to split the array - middle = n / 2 + middle = n/2 ! Allocate space for the two halves - allocate(left_half(middle), right_half(n - middle), sorted_array(n)) + allocate (left_half(middle), right_half(n - middle), sorted_array(n)) ! Split array into two halves left_half = array(1:middle) - right_half = array(middle+1:n) + right_half = array(middle + 1:n) ! Recursively sort each half call merge_sort(left_half, middle) @@ -50,7 +50,7 @@ recursive subroutine merge_sort(array, n) array = sorted_array ! Deallocate the temporary arrays - deallocate(left_half, right_half, sorted_array) + deallocate (left_half, right_half, sorted_array) end subroutine merge_sort diff --git a/modules/sorts/radix_sort.f90 b/modules/sorts/radix_sort.f90 index 840265d..64fbeb6 100644 --- a/modules/sorts/radix_sort.f90 +++ b/modules/sorts/radix_sort.f90 @@ -37,9 +37,9 @@ subroutine radix_sort(array, n, base) exp = 1 ! Perform Counting Sort for each digit - do while (max_digit / exp >= 1) + do while (max_digit/exp >= 1) call counting_sort(array, n, exp, base) - exp = exp * base + exp = exp*base end do end subroutine radix_sort @@ -55,14 +55,14 @@ subroutine counting_sort(array, n, exp, base) integer, dimension(:), allocatable :: count, output count_size = base - allocate(count(count_size), output(n)) + allocate (count(count_size), output(n)) ! Initialize count array count = 0 ! Store count of occurrences do i = 1, n - digit = mod(array(i) / exp, base) + digit = mod(array(i)/exp, base) count(digit + 1) = count(digit + 1) + 1 end do @@ -73,7 +73,7 @@ subroutine counting_sort(array, n, exp, base) ! Build the output array do i = n, 1, -1 - digit = mod(array(i) / exp, base) + digit = mod(array(i)/exp, base) output(count(digit + 1)) = array(i) count(digit + 1) = count(digit + 1) - 1 end do @@ -82,8 +82,8 @@ subroutine counting_sort(array, n, exp, base) array = output ! Deallocate temporary arrays - deallocate(count, output) + deallocate (count, output) end subroutine counting_sort -end module radix_sort_module \ No newline at end of file +end module radix_sort_module diff --git a/tests/sorts/tests_gnome_sort.f90 b/tests/sorts/tests_gnome_sort.f90 index 1e2bdae..04ad524 100644 --- a/tests/sorts/tests_gnome_sort.f90 +++ b/tests/sorts/tests_gnome_sort.f90 @@ -6,60 +6,59 @@ program tests_gnome_sort use gnome_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_gnome_sort \ No newline at end of file +end program tests_gnome_sort diff --git a/tests/sorts/tests_heap_sort.f90 b/tests/sorts/tests_heap_sort.f90 index 1961d80..0b28b2a 100644 --- a/tests/sorts/tests_heap_sort.f90 +++ b/tests/sorts/tests_heap_sort.f90 @@ -6,60 +6,59 @@ program tests_heap_sort use heap_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_heap_sort \ No newline at end of file +end program tests_heap_sort diff --git a/tests/sorts/tests_merge_sort.f90 b/tests/sorts/tests_merge_sort.f90 index 45af1fa..a55ef6c 100644 --- a/tests/sorts/tests_merge_sort.f90 +++ b/tests/sorts/tests_merge_sort.f90 @@ -6,60 +6,59 @@ program tests_merge_sort use merge_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 4, 2, 7, 3, 1, 4, 9, 5, 10, 9, 2, 1/) + array = (/4, 2, 7, 3, 1, 4, 9, 5, 10, 9, 2, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -11, -55, -43, -70, -2, -1, -15, -9 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-11, -55, -43, -70, -2, -1, -15, -9/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 62 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/62/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 4, 4, 4, 4, 4 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/4, 4, 4, 4, 4/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 10, 2000, 20, 888, 30, 798 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/10, 2000, 20, 888, 30, 798/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_merge_sort \ No newline at end of file +end program tests_merge_sort diff --git a/tests/sorts/tests_quick_sort.f90 b/tests/sorts/tests_quick_sort.f90 index 262f251..547970a 100644 --- a/tests/sorts/tests_quick_sort.f90 +++ b/tests/sorts/tests_quick_sort.f90 @@ -6,60 +6,59 @@ program tests_quick_sort use quick_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_quick_sort \ No newline at end of file +end program tests_quick_sort diff --git a/tests/sorts/tests_radix_sort.f90 b/tests/sorts/tests_radix_sort.f90 index 26dcc04..22c04a2 100644 --- a/tests/sorts/tests_radix_sort.f90 +++ b/tests/sorts/tests_radix_sort.f90 @@ -6,84 +6,83 @@ program tests_radix_sort use radix_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array integer, parameter :: base10 = 10, base2 = 2, base16 = 16 ! Test 1: Base 10 print *, "Test 1: Radix Sort with base 10" - if (allocated(array)) deallocate(array) - allocate(array(10)) - array = (/ 170, 45, 75, 90, 802, 24, 2, 66, 15, 40 /) + if (allocated(array)) deallocate (array) + allocate (array(10)) + array = (/170, 45, 75, 90, 802, 24, 2, 66, 15, 40/) call run_test(array, base10) ! Test 2: Base 2 print *, "Test 2: Radix Sort with base 2" - if (allocated(array)) deallocate(array) - allocate(array(10)) - array = (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /) ! Binary values as decimal + if (allocated(array)) deallocate (array) + allocate (array(10)) + array = (/10, 13, 9, 14, 2, 5, 15, 6, 8, 1/) ! Binary values as decimal call run_test(array, base2) ! Test 3: Base 16 print *, "Test 3: Radix Sort with base 16" - if (allocated(array)) deallocate(array) - allocate(array(10)) - array = (/ 171, 31, 61, 255, 16, 5, 211, 42, 180, 0 /) ! Hexadecimal values as decimal + if (allocated(array)) deallocate (array) + allocate (array(10)) + array = (/171, 31, 61, 255, 16, 5, 211, 42, 180, 0/) ! Hexadecimal values as decimal call run_test(array, base16) ! Test 4: Repeated elements print *, "Test 4: Array with repeated elements" - if (allocated(array)) deallocate(array) - allocate(array(12)) - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(12)) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array, base10) ! Test 5: Already sorted array print *, "Test 5: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array, base10) ! Test 6: Reverse sorted array print *, "Test 6: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array, base10) ! Test 7: Array with all negative numbers (Note: Radix Sort only handles non-negative integers) print *, "Test 7: Array with all negative numbers (handled as base 10)" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array, base10) ! Test 8: Single element array print *, "Test 8: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array, base10) ! Test 9: Array with identical elements print *, "Test 9: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array, base10) ! Test 10: Array with alternating high and low values print *, "Test 10: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array, base10) ! Test 11: Empty array print *, "Test 11: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array, base10) contains From e2818892e184fae41d75bcdde75032fb6ed0ac0d Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Sat, 28 Sep 2024 16:21:36 +0200 Subject: [PATCH 5/6] Added comments to the Doc blocks for my files. --- .../numerical_integration/gaussian_legendre.f90 | 8 ++++++++ examples/maths/numerical_integration/midpoint.f90 | 8 ++++++++ examples/maths/numerical_integration/monte_carlo.f90 | 8 ++++++++ examples/maths/numerical_integration/simpson.f90 | 8 ++++++++ examples/maths/numerical_integration/trapezoid.f90 | 7 +++++++ examples/sorts/example_usage_gnome_sort.f90 | 9 ++++++++- examples/sorts/example_usage_heap_sort.f90 | 7 +++++++ examples/sorts/example_usage_merge_sort.f90 | 9 ++++++++- examples/sorts/example_usage_quick_sort.f90 | 8 ++++++++ examples/sorts/example_usage_radix_sort.f90 | 9 ++++++++- .../numerical_integration/gaussian_legendre.f90 | 7 +++++++ modules/maths/numerical_integration/midpoint.f90 | 7 +++++++ modules/maths/numerical_integration/monte_carlo.f90 | 7 +++++++ modules/maths/numerical_integration/simpson.f90 | 7 +++++++ modules/maths/numerical_integration/trapezoid.f90 | 7 +++++++ modules/sorts/gnome_sort.f90 | 12 ++++++++++-- modules/sorts/heap_sort.f90 | 12 ++++++++++-- modules/sorts/merge_sort.f90 | 9 ++++++++- modules/sorts/quick_sort.f90 | 8 ++++++++ modules/sorts/radix_sort.f90 | 12 ++++++++++-- tests/sorts/tests_gnome_sort.f90 | 9 ++++++++- tests/sorts/tests_heap_sort.f90 | 9 ++++++++- tests/sorts/tests_merge_sort.f90 | 9 ++++++++- tests/sorts/tests_quick_sort.f90 | 9 ++++++++- tests/sorts/tests_radix_sort.f90 | 9 ++++++++- 25 files changed, 199 insertions(+), 15 deletions(-) diff --git a/examples/maths/numerical_integration/gaussian_legendre.f90 b/examples/maths/numerical_integration/gaussian_legendre.f90 index f7752d5..145fab8 100644 --- a/examples/maths/numerical_integration/gaussian_legendre.f90 +++ b/examples/maths/numerical_integration/gaussian_legendre.f90 @@ -1,4 +1,12 @@ !> Example Program for Gaussian-Legendre Quadrature Module +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of Gaussian-Legendre Quadrature Module for numerical integration. !! !! It sets the integration limits and the number of quadrature points (n), and calls the diff --git a/examples/maths/numerical_integration/midpoint.f90 b/examples/maths/numerical_integration/midpoint.f90 index 9ce83b5..933b88c 100644 --- a/examples/maths/numerical_integration/midpoint.f90 +++ b/examples/maths/numerical_integration/midpoint.f90 @@ -1,4 +1,12 @@ !> Example Program for Midpoint Rule +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of Midpoint Rule for numerical integration. !! !! It sets the integration limits and number of subintervals (panels), and calls the diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 index 818a337..e3d6265 100644 --- a/examples/maths/numerical_integration/monte_carlo.f90 +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -1,4 +1,12 @@ !> Example Program for Monte Carlo Integration +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of Monte Carlo module for numerical integration. !! !! It sets the integration limits and number of random samples, and calls the diff --git a/examples/maths/numerical_integration/simpson.f90 b/examples/maths/numerical_integration/simpson.f90 index a7fbd07..5a7e3c7 100644 --- a/examples/maths/numerical_integration/simpson.f90 +++ b/examples/maths/numerical_integration/simpson.f90 @@ -1,4 +1,12 @@ !> Example Program for Simpson's Rule +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of Simpson's rule for numerical integration. !! !! It sets the integration limits and number of panels, and calls the diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 index fb9090a..353bbc5 100644 --- a/examples/maths/numerical_integration/trapezoid.f90 +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -1,5 +1,12 @@ !> Example Program for Trapezoidal Rule !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of the Trapezoidal rule for numerical integration. !! !! It sets the integration limits and number of panels, and calls the diff --git a/examples/sorts/example_usage_gnome_sort.f90 b/examples/sorts/example_usage_gnome_sort.f90 index 5c2cb15..67063e2 100644 --- a/examples/sorts/example_usage_gnome_sort.f90 +++ b/examples/sorts/example_usage_gnome_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Gnome Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #09 +!! https://github.com/TheAlgorithms/Fortran/pull/9 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of the gnome_sort_module by sorting an array of integers. program test_gnome_sort diff --git a/examples/sorts/example_usage_heap_sort.f90 b/examples/sorts/example_usage_heap_sort.f90 index 24c705f..a1ba0e5 100644 --- a/examples/sorts/example_usage_heap_sort.f90 +++ b/examples/sorts/example_usage_heap_sort.f90 @@ -1,5 +1,12 @@ !> Example program for the Heap Sort algorithm !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #8 +!! https://github.com/TheAlgorithms/Fortran/pull/8 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of the heap_sort_module by sorting an array of integers. program test_heap_sort diff --git a/examples/sorts/example_usage_merge_sort.f90 b/examples/sorts/example_usage_merge_sort.f90 index 79893c3..a43c9a5 100644 --- a/examples/sorts/example_usage_merge_sort.f90 +++ b/examples/sorts/example_usage_merge_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Merge Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #7 +!! https://github.com/TheAlgorithms/Fortran/pull/7 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of the merge_sort_module by sorting an array of integers. program test_merge_sort diff --git a/examples/sorts/example_usage_quick_sort.f90 b/examples/sorts/example_usage_quick_sort.f90 index c61b6dd..c482918 100644 --- a/examples/sorts/example_usage_quick_sort.f90 +++ b/examples/sorts/example_usage_quick_sort.f90 @@ -1,4 +1,12 @@ !> Example program for the Quick Sort algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #10 +!! https://github.com/TheAlgorithms/Fortran/pull/10 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of the quick_sort_module by sorting an array of integers. program test_quick_sort diff --git a/examples/sorts/example_usage_radix_sort.f90 b/examples/sorts/example_usage_radix_sort.f90 index 811819a..86f1ae0 100644 --- a/examples/sorts/example_usage_radix_sort.f90 +++ b/examples/sorts/example_usage_radix_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Radix Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #11 +!! https://github.com/TheAlgorithms/Fortran/pull/11 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program demonstrates the use of the radix_sort_module by sorting an array of integers. !! The base parameter affects the internal digit processing but does not change the final sorted order !! of decimal integers. The output is always in decimal form. diff --git a/modules/maths/numerical_integration/gaussian_legendre.f90 b/modules/maths/numerical_integration/gaussian_legendre.f90 index 5f288bb..27599f9 100644 --- a/modules/maths/numerical_integration/gaussian_legendre.f90 +++ b/modules/maths/numerical_integration/gaussian_legendre.f90 @@ -2,6 +2,13 @@ !! !! This module provides the implementation of Gaussian-Legendre Quadrature. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! The method approximates the definite integral of a function over a specified interval [a, b]. !! !! The quadrature method works by transforming nodes and weights from the reference interval [-1, 1] to the diff --git a/modules/maths/numerical_integration/midpoint.f90 b/modules/maths/numerical_integration/midpoint.f90 index b7e034b..4f6589f 100644 --- a/modules/maths/numerical_integration/midpoint.f90 +++ b/modules/maths/numerical_integration/midpoint.f90 @@ -2,6 +2,13 @@ !! !! This module implements Midpoint rule for numerical integration. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! The midpoint rule approximates the integral by calculating the function !! value at the midpoint of each subinterval and summing these values, multiplied !! by the width of the subintervals. diff --git a/modules/maths/numerical_integration/monte_carlo.f90 b/modules/maths/numerical_integration/monte_carlo.f90 index 33b970e..a649e7c 100644 --- a/modules/maths/numerical_integration/monte_carlo.f90 +++ b/modules/maths/numerical_integration/monte_carlo.f90 @@ -3,6 +3,13 @@ !! This module estimates the integral of a function over a specified range !! using the Monte Carlo method (with OpenMP parallelization) and provides an error estimate. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! The method works by randomly sampling points within the integration range [a, b] !! and evaluating the function at those points to estimate the integral. !! diff --git a/modules/maths/numerical_integration/simpson.f90 b/modules/maths/numerical_integration/simpson.f90 index b8ad07e..028abe0 100644 --- a/modules/maths/numerical_integration/simpson.f90 +++ b/modules/maths/numerical_integration/simpson.f90 @@ -2,6 +2,13 @@ !! !! This module implements Simpson's rule for numerical integration. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! Simpson's rule approximates the definite integral of a function by !! dividing the area under the curve into parabolic segments and summing !! their areas, providing a higher degree of accuracy than the Trapezoidal rule. diff --git a/modules/maths/numerical_integration/trapezoid.f90 b/modules/maths/numerical_integration/trapezoid.f90 index 43f12eb..955fb8b 100644 --- a/modules/maths/numerical_integration/trapezoid.f90 +++ b/modules/maths/numerical_integration/trapezoid.f90 @@ -2,6 +2,13 @@ !! !! This module implements the Trapezoidal rule for numerical integration. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #25 +!! https://github.com/TheAlgorithms/Fortran/pull/25 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! The Trapezoidal rule approximates the definite integral of a function by !! dividing the area under the curve into trapezoids and summing their areas. !! diff --git a/modules/sorts/gnome_sort.f90 b/modules/sorts/gnome_sort.f90 index b92e912..b89c773 100644 --- a/modules/sorts/gnome_sort.f90 +++ b/modules/sorts/gnome_sort.f90 @@ -1,6 +1,13 @@ !> Gnome Sort Algorithm - -!> This module implements the Gnome Sort algorithm. +!! +!! This module implements the Gnome Sort algorithm. +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #09 +!! https://github.com/TheAlgorithms/Fortran/pull/9 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! !! !! Gnome Sort is a simple comparison-based sorting algorithm. !! It iterates through the array, comparing and swapping elements if needed. @@ -12,6 +19,7 @@ !! !! Output: !! - A sorted array of integers. +!! module gnome_sort_module implicit none diff --git a/modules/sorts/heap_sort.f90 b/modules/sorts/heap_sort.f90 index 46b05d5..40f63b1 100644 --- a/modules/sorts/heap_sort.f90 +++ b/modules/sorts/heap_sort.f90 @@ -1,7 +1,14 @@ -!> ## Heap Sort Algorithm -!> +!> Heap Sort Algorithm +!! !! This module implements the Heap Sort algorithm. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #8 +!! https://github.com/TheAlgorithms/Fortran/pull/8 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure. !! It first builds a max heap from the input data and then repeatedly extracts the maximum !! element from the heap and reconstructs the heap until the array is sorted. @@ -13,6 +20,7 @@ !! !! Output: !! - A sorted array of integers. +!! module heap_sort_module implicit none diff --git a/modules/sorts/merge_sort.f90 b/modules/sorts/merge_sort.f90 index 59c90f2..a754efd 100644 --- a/modules/sorts/merge_sort.f90 +++ b/modules/sorts/merge_sort.f90 @@ -1,6 +1,13 @@ !> Merge Sort Algorithm !! -!> This module implements the Merge Sort algorithm. +!! This module implements the Merge Sort algorithm. +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #7 +!! https://github.com/TheAlgorithms/Fortran/pull/7 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! !! !! Merge Sort is a divide-and-conquer algorithm. It divides the input array into two halves, recursively sorts them, !! and then merges the two sorted halves. diff --git a/modules/sorts/quick_sort.f90 b/modules/sorts/quick_sort.f90 index fa1bbd4..0c5839f 100644 --- a/modules/sorts/quick_sort.f90 +++ b/modules/sorts/quick_sort.f90 @@ -1,7 +1,15 @@ !> Quick Sort Algorithm +!! !! This module implements the Quick Sort algorithm, a highly efficient !! sorting technique that uses the divide-and-conquer strategy. !! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #10 +!! https://github.com/TheAlgorithms/Fortran/pull/10 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! Quick Sort works by selecting a pivot element and partitioning the !! array into elements less than the pivot and elements greater than the pivot. !! diff --git a/modules/sorts/radix_sort.f90 b/modules/sorts/radix_sort.f90 index 64fbeb6..79a72de 100644 --- a/modules/sorts/radix_sort.f90 +++ b/modules/sorts/radix_sort.f90 @@ -1,6 +1,13 @@ !> Radix Sort Algorithm - -!> This module implements the Radix Sort algorithm with configurable base. +!! +!! This module implements the Radix Sort algorithm with configurable base. +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #11 +!! https://github.com/TheAlgorithms/Fortran/pull/11 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! !! !! Radix Sort is a non-comparison-based sorting algorithm that sorts numbers by processing individual digits. !! It is particularly efficient for sorting large lists of integers with a fixed number of digits. @@ -13,6 +20,7 @@ !! !! Output: !! - A sorted array of integers. +!! module radix_sort_module implicit none diff --git a/tests/sorts/tests_gnome_sort.f90 b/tests/sorts/tests_gnome_sort.f90 index 04ad524..d921c67 100644 --- a/tests/sorts/tests_gnome_sort.f90 +++ b/tests/sorts/tests_gnome_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Gnome Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #09 +!! https://github.com/TheAlgorithms/Fortran/pull/9 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program provides additional test cases to validate the gnome_sort_module. program tests_gnome_sort diff --git a/tests/sorts/tests_heap_sort.f90 b/tests/sorts/tests_heap_sort.f90 index 0b28b2a..18b2794 100644 --- a/tests/sorts/tests_heap_sort.f90 +++ b/tests/sorts/tests_heap_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Heap Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #8 +!! https://github.com/TheAlgorithms/Fortran/pull/8 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program provides additional test cases to validate the heap_sort_module. program tests_heap_sort diff --git a/tests/sorts/tests_merge_sort.f90 b/tests/sorts/tests_merge_sort.f90 index a55ef6c..5da8ad0 100644 --- a/tests/sorts/tests_merge_sort.f90 +++ b/tests/sorts/tests_merge_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Merge Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #7 +!! https://github.com/TheAlgorithms/Fortran/pull/7 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program provides additional test cases to validate the merge_sort_module. program tests_merge_sort diff --git a/tests/sorts/tests_quick_sort.f90 b/tests/sorts/tests_quick_sort.f90 index 547970a..a46835f 100644 --- a/tests/sorts/tests_quick_sort.f90 +++ b/tests/sorts/tests_quick_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Quick Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #10 +!! https://github.com/TheAlgorithms/Fortran/pull/10 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program provides additional test cases to validate the quick_sort_module. program tests_quick_sort diff --git a/tests/sorts/tests_radix_sort.f90 b/tests/sorts/tests_radix_sort.f90 index 22c04a2..056fd5c 100644 --- a/tests/sorts/tests_radix_sort.f90 +++ b/tests/sorts/tests_radix_sort.f90 @@ -1,5 +1,12 @@ !> Test program for the Radix Sort algorithm - +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #11 +!! https://github.com/TheAlgorithms/Fortran/pull/11 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! !! This program provides additional test cases to validate the radix_sort_module. !! The radix (base) parameter affects the internal digit processing for sorting, but the final output is always in decimal form. From 5645569f570d5eaa6b21a928273c78e68f26bcb7 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Mon, 30 Sep 2024 09:25:16 +0200 Subject: [PATCH 6/6] Renamed variables in examples files --- .../gaussian_legendre.f90 | 18 +++++++++--------- .../maths/numerical_integration/midpoint.f90 | 17 +++++++++-------- .../numerical_integration/monte_carlo.f90 | 18 +++++++++--------- .../maths/numerical_integration/simpson.f90 | 16 ++++++++-------- .../maths/numerical_integration/trapezoid.f90 | 16 ++++++++-------- 5 files changed, 43 insertions(+), 42 deletions(-) diff --git a/examples/maths/numerical_integration/gaussian_legendre.f90 b/examples/maths/numerical_integration/gaussian_legendre.f90 index 145fab8..d9935b9 100644 --- a/examples/maths/numerical_integration/gaussian_legendre.f90 +++ b/examples/maths/numerical_integration/gaussian_legendre.f90 @@ -19,27 +19,27 @@ program example_gaussian_quadrature use gaussian_legendre_quadrature implicit none - real(dp) :: a, b, integral_result - integer :: n + real(dp) :: lower_bound, upper_bound, integral_result + integer :: quadrature_points_number ! Set the integration limits and number of quadrature points - a = -1.0_dp - b = 1.0_dp - n = 5 !! Number of quadrature points + lower_bound = -1.0_dp + upper_bound = 1.0_dp + quadrature_points_number = 5 !! Number of quadrature points (order of accuracy) up to 5 - ! Call Gaussian quadrature to compute the integral - call gauss_legendre_quadrature(integral_result, a, b, n, func) + ! Call Gaussian quadrature to compute the integral with the function passed as an argument + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, quadrature_points_number, function) write (*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574 contains - function func(x) result(fx) + function function(x) result(fx) implicit none real(dp), intent(in) :: x real(dp) :: fx fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate - end function func + end function function end program example_gaussian_quadrature diff --git a/examples/maths/numerical_integration/midpoint.f90 b/examples/maths/numerical_integration/midpoint.f90 index 933b88c..e57a902 100644 --- a/examples/maths/numerical_integration/midpoint.f90 +++ b/examples/maths/numerical_integration/midpoint.f90 @@ -18,27 +18,28 @@ program example_midpoint use midpoint_rule implicit none - real(dp) :: a, b, integral_result - integer :: n + + real(dp) :: lower_bound, upper_bound, integral_result + integer :: panels_number ! Set the integration limits and number of panels - a = -1.0_dp - b = 1.0_dp - n = 400 !! Number of subdivisions + lower_bound = -1.0_dp + upper_bound = 1.0_dp + panels_number = 400 !! Number of subdivisions ! Call the midpoint rule subroutine with the function passed as an argument - call midpoint(integral_result, a, b, n, func) + call midpoint(integral_result, lower_bound, upper_bound, panels_number, function) write (*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196 contains - function func(x) result(fx) + function function(x) result(fx) implicit none real(dp), intent(in) :: x real(dp) :: fx fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate - end function func + end function function end program example_midpoint diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 index e3d6265..d4d2776 100644 --- a/examples/maths/numerical_integration/monte_carlo.f90 +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -19,27 +19,27 @@ program example_monte_carlo use monte_carlo_integration implicit none - real(dp) :: a, b, integral_result, error_estimate - integer :: n + real(dp) :: lower_bound, upper_bound, integral_result, error_estimate + integer :: random_samples_number ! Set the integration limits and number of random samples - a = -1.0_dp - b = 1.0_dp - n = 1000000 !! 1E6 Number of random samples + lower_bound = -1.0_dp + upper_bound = 1.0_dp + random_samples_number = 1000000 !! 1E6 Number of random samples - ! Call Monte Carlo integration - call monte_carlo(integral_result, error_estimate, a, b, n, func) + ! Call Monte Carlo integration with the function passed as an argument + call monte_carlo(integral_result, error_estimate, lower_bound, upper_bound, random_samples_number, function) write (*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421 contains - function func(x) result(fx) + function function(x) result(fx) implicit none real(dp), intent(in) :: x real(dp) :: fx fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate - end function func + end function function end program example_monte_carlo diff --git a/examples/maths/numerical_integration/simpson.f90 b/examples/maths/numerical_integration/simpson.f90 index 5a7e3c7..be81758 100644 --- a/examples/maths/numerical_integration/simpson.f90 +++ b/examples/maths/numerical_integration/simpson.f90 @@ -19,27 +19,27 @@ program example_simpson use simpson_rule implicit none - real(dp) :: a, b, integral_result - integer :: n + real(dp) :: lower_bound, upper_bound, integral_result + integer :: panels_number ! Set the integration limits and number of panels - a = -1.0_dp - b = 1.0_dp - n = 100 !! Number of subdivisions (must be even) + lower_bound = -1.0_dp + upper_bound = 1.0_dp + panels_number = 100 !! Number of subdivisions (must be even) ! Call Simpson's rule with the function passed as an argument - call simpson(integral_result, a, b, n, func) + call simpson(integral_result, lower_bound, upper_bound, panels_number, function) write (*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555 contains - function func(x) result(fx) + function function(x) result(fx) implicit none real(dp), intent(in) :: x real(dp) :: fx fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate - end function func + end function function end program example_simpson diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 index 353bbc5..4be1ced 100644 --- a/examples/maths/numerical_integration/trapezoid.f90 +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -19,27 +19,27 @@ program example_tapezoid use trapezoidal_rule implicit none - real(dp) :: a, b, integral_result - integer :: n + real(dp) :: lower_bound, upper_bound, integral_result + integer :: panels_number ! Set the integration limits and number of panels - a = -1.0_dp - b = 1.0_dp - n = 1000000 !! 1E6 Number of subdivisions + lower_bound = -1.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 !! 1E6 Number of subdivisions ! Call the trapezoidal rule with the function passed as an argument - call trapezoid(integral_result, a, b, n, func) + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, function) write (*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195 contains - function func(x) result(fx) + function function(x) result(fx) implicit none real(dp), intent(in) :: x real(dp) :: fx fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate - end function func + end function function end program example_tapezoid