Skip to content

Commit

Permalink
Considered -Wconversion-extra compilation flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramy-Badr-Ahmed committed Sep 27, 2024
1 parent fe82279 commit 8359576
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 20 deletions.
18 changes: 10 additions & 8 deletions examples/searches/example_ternary_search_function_based.f90
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
! Example Program: Function-based Ternary Search for Minimum and Maximum
! This program demonstrates how to use the function-based ternary search algorithm
! from the `ternary_search` module to find the minimum and maximum of unimodal functions.
!> Example Program: Function-based Ternary Search for Minimum and Maximum
!!
!! This program demonstrates how to use the function-based ternary search algorithm
!! from the `ternary_search` module to find the minimum and maximum of unimodal functions.

program ternary_search_function_based
use ternary_search
implicit none

! Define the variables
integer, parameter :: dp = kind(0.0d0) ! Define double precision kind
real(8) :: result_min, result_max ! Results for minimum and maximum values
real(8) :: min_point, max_point ! Points where minimum and maximum occur
real(8) :: left, right, tol ! Left and right bounds, and tolerance
Expand All @@ -25,11 +27,11 @@ end function f_max

! The boundary values can vary depending on the problem context.
! In this example, they are chosen arbitrarily.
left = 0.0
right = 10.0
left = 0.0d0
right = 10.0d0

! The tolerance value defines how close the left and right bounds must be for the search to terminate.
tol = 1.0e-6
tol = 1.0e-6_dp

! Call the ternary search to find the minimum point of f_min
min_point = ternary_search_minimum(f_min, left, right, tol)
Expand All @@ -51,7 +53,7 @@ end program ternary_search_function_based

real(8) function f_min(x)
real(8), intent(in) :: x
f_min = (x - 5.0)**2 + cos(x) ! Example of a quadratic function with a cosine oscillation
f_min = (x - 5.0d0)**2 + cos(x) ! Example of a quadratic function with a cosine oscillation
end function f_min

! Define the unimodal function f_max with a maximum near x = 5.0
Expand All @@ -61,5 +63,5 @@ end function f_min

real(8) function f_max(x)
real(8), intent(in) :: x
f_max = -(x - 5.0)**2 + cos(x) ! Example of a quadratic function with a cosine oscillation
f_max = -(x - 5.0d0)**2 + cos(x) ! Example of a quadratic function with a cosine oscillation
end function f_max
12 changes: 6 additions & 6 deletions modules/searches/ternary_search_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ end function f

! Termination condition based on tolerance
do while (r - l > tol)
mid1 = l + (r - l)/3.0
mid2 = r - (r - l)/3.0
mid1 = l + (r - l)/3.0d0
mid2 = r - (r - l)/3.0d0

! Compare function values at midpoints
if (f(mid1) < f(mid2)) then
Expand All @@ -105,7 +105,7 @@ end function f
end do

! The minimum point is approximately at the midpoint
minimum = (l + r)/2.0
minimum = (l + r)/2.0d0
end function ternary_search_minimum

!> Function-based ternary search to find the maximum of a unimodal function
Expand Down Expand Up @@ -134,8 +134,8 @@ end function f

! Termination condition based on tolerance
do while (r - l > tol)
mid1 = l + (r - l)/3.0
mid2 = r - (r - l)/3.0
mid1 = l + (r - l)/3.0d0
mid2 = r - (r - l)/3.0d0

! Compare function values at midpoints
if (f(mid1) > f(mid2)) then
Expand All @@ -146,7 +146,7 @@ end function f
end do

! The maximum point is approximately at the midpoint
maximum = (l + r)/2.0
maximum = (l + r)/2.0d0
end function ternary_search_maximum

end module ternary_search
2 changes: 1 addition & 1 deletion modules/sorts/merge_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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
Expand Down
1 change: 0 additions & 1 deletion tests/sorts/tests_gnome_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ program tests_gnome_sort

use gnome_sort_module
implicit none
integer :: i
integer, dimension(:), allocatable :: array

! Test 1: Repeated elements
Expand Down
1 change: 0 additions & 1 deletion tests/sorts/tests_heap_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ program tests_heap_sort

use heap_sort_module
implicit none
integer :: i
integer, dimension(:), allocatable :: array

! Test 1: Repeated elements
Expand Down
1 change: 0 additions & 1 deletion tests/sorts/tests_merge_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ program tests_merge_sort

use merge_sort_module
implicit none
integer :: i
integer, dimension(:), allocatable :: array

! Test 1: Repeated elements
Expand Down
1 change: 0 additions & 1 deletion tests/sorts/tests_quick_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ program tests_quick_sort

use quick_sort_module
implicit none
integer :: i
integer, dimension(:), allocatable :: array

! Test 1: Repeated elements
Expand Down
1 change: 0 additions & 1 deletion tests/sorts/tests_radix_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
program tests_radix_sort
use radix_sort_module
implicit none
integer :: i
integer, dimension(:), allocatable :: array
integer, parameter :: base10 = 10, base2 = 2, base16 = 16

Expand Down

0 comments on commit 8359576

Please sign in to comment.