From 86f768171694fcc669ab96c7b1886468d23de3b0 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 19:57:54 +0200 Subject: [PATCH 1/7] test for trapezoid.f90 --- .../maths/numerical_integration/trapezoid.f90 | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/maths/numerical_integration/trapezoid.f90 diff --git a/tests/maths/numerical_integration/trapezoid.f90 b/tests/maths/numerical_integration/trapezoid.f90 new file mode 100644 index 0000000..6c91c89 --- /dev/null +++ b/tests/maths/numerical_integration/trapezoid.f90 @@ -0,0 +1,76 @@ +!> Test program for the Trapezoidal Rule module +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #32 +!! https://github.com/TheAlgorithms/Fortran/pull/32 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides test cases to validate the trapezoidal_rule module against known integral values. + + +program test_trapezoidal_rule + use trapezoidal_rule + implicit none + + real(dp) :: lower_bound, upper_bound, integral_result, expected + real(dp), parameter :: pi = 4.d0*DATAN(1.d0) ! Define Pi. Ensures maximum precision available on any architecture + + integer :: panels_number + + ! Test 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 + expected = 1.0_dp / 3.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") + + ! Test 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) + lower_bound = 0.0_dp + upper_bound = 2.0_dp + panels_number = 1000000 + expected = 8.0_dp / 3.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") + + ! Test 3: ∫ sin(x) dx from 0 to π (Exact result = 2) + lower_bound = 0.0_dp + upper_bound = pi + panels_number = 1000000 + expected = 2.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function) + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") + +contains + + ! Function for x^2 + real(dp) function f_x_squared(x) + real(dp), intent(in) :: x + f_x_squared = x**2 + end function f_x_squared + + ! Function for sin(x) + real(dp) function sin_function(x) + real(dp), intent(in) :: x + sin_function = sin(x) + end function sin_function + + ! Assertion subroutine + subroutine assert_test(result, expected, test_name) + real(dp), intent(in) :: result, expected + character(len=*), intent(in) :: test_name + real(dp), parameter :: tol = 1.0e-5_dp + + if (abs(result - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, " Expected: ", expected + print *, " Got: ", result + stop 1 + end if + end subroutine assert_test + +end program test_trapezoidal_rule From 0775c7c970678a6417a865e0435068e83ca8603f Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 20:20:22 +0200 Subject: [PATCH 2/7] Test for trapezoid.f90 --- .../maths/numerical_integration/trapezoid.f90 | 188 +++++++++++++++--- 1 file changed, 155 insertions(+), 33 deletions(-) diff --git a/tests/maths/numerical_integration/trapezoid.f90 b/tests/maths/numerical_integration/trapezoid.f90 index 6c91c89..f704ffe 100644 --- a/tests/maths/numerical_integration/trapezoid.f90 +++ b/tests/maths/numerical_integration/trapezoid.f90 @@ -9,68 +9,190 @@ !! !! This program provides test cases to validate the trapezoidal_rule module against known integral values. +!> Test program for the Trapezoidal Rule module +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #32 +!! https://github.com/TheAlgorithms/Fortran/pull/32 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides test cases to validate the trapezoidal_rule module against known integral values. program test_trapezoidal_rule use trapezoidal_rule implicit none - real(dp) :: lower_bound, upper_bound, integral_result, expected - real(dp), parameter :: pi = 4.d0*DATAN(1.d0) ! Define Pi. Ensures maximum precision available on any architecture - - integer :: panels_number - - ! Test 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) - lower_bound = 0.0_dp - upper_bound = 1.0_dp - panels_number = 1000000 - expected = 1.0_dp / 3.0_dp - call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) - call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") - - ! Test 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) - lower_bound = 0.0_dp - upper_bound = 2.0_dp - panels_number = 1000000 - expected = 8.0_dp / 3.0_dp - call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) - call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") - - ! Test 3: ∫ sin(x) dx from 0 to π (Exact result = 2) - lower_bound = 0.0_dp - upper_bound = pi - panels_number = 1000000 - expected = 2.0_dp - call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function) - call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") + ! Run test cases + call test_integral_x_squared_0_to_1() + call test_integral_x_squared_0_to_2() + call test_integral_sin_0_to_pi() + call test_integral_e_x_0_to_1() + call test_integral_1_over_x_1_to_e() + call test_integral_cos_0_to_pi_over_2() + call test_integral_x_cubed_0_to_1() + call test_integral_sin_x_squared_0_to_1() + + print *, "All tests completed." contains + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) + subroutine test_integral_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 + expected = 1.0_dp/3.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") + end subroutine test_integral_x_squared_0_to_1 + + ! Test case 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) + subroutine test_integral_x_squared_0_to_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 2.0_dp + panels_number = 1000000 + expected = 8.0_dp/3.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") + end subroutine test_integral_x_squared_0_to_2 + + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) + subroutine test_integral_sin_0_to_pi() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + lower_bound = 0.0_dp + upper_bound = pi + panels_number = 1000000 + expected = 2.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function) + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") + end subroutine test_integral_sin_0_to_pi + + ! Test case 4: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) + subroutine test_integral_e_x_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 + expected = exp(1.0_dp) - 1.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, exp_function) + call assert_test(integral_result, expected, "Test 4: ∫ e^x dx from 0 to 1") + end subroutine test_integral_e_x_0_to_1 + + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) + subroutine test_integral_1_over_x_1_to_e() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 1.0_dp + upper_bound = exp(1.0_dp) + panels_number = 1000000 + expected = 1.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, log_function) + call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e") + end subroutine test_integral_1_over_x_1_to_e + + ! Test case 6: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) + subroutine test_integral_cos_0_to_pi_over_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + lower_bound = 0.0_dp + upper_bound = pi/2.0_dp + panels_number = 1000000 + expected = 1.0_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, cos_function) + call assert_test(integral_result, expected, "Test 6: ∫ cos(x) dx from 0 to π/2") + end subroutine test_integral_cos_0_to_pi_over_2 + + ! Test case 7: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) + subroutine test_integral_x_cubed_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 + expected = 0.25_dp + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed) + call assert_test(integral_result, expected, "Test 7: ∫ x^3 dx from 0 to 1") + end subroutine test_integral_x_cubed_0_to_1 + + ! Test case 8: ∫ sin(x^2) dx from 0 to 1 (Approximate value) + subroutine test_integral_sin_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 + expected = 0.31026_dp ! Approximate value, you can adjust tolerance as needed + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) + call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1") + end subroutine test_integral_sin_x_squared_0_to_1 + ! Function for x^2 real(dp) function f_x_squared(x) real(dp), intent(in) :: x f_x_squared = x**2 end function f_x_squared + ! Function for e^x + real(dp) function exp_function(x) + real(dp), intent(in) :: x + exp_function = exp(x) + end function exp_function + + ! Function for 1/x + real(dp) function log_function(x) + real(dp), intent(in) :: x + log_function = 1.0_dp/x + end function log_function + + ! Function for cos(x) + real(dp) function cos_function(x) + real(dp), intent(in) :: x + cos_function = cos(x) + end function cos_function + + ! Function for x^3 + real(dp) function f_x_cubed(x) + real(dp), intent(in) :: x + f_x_cubed = x**3 + end function f_x_cubed + + ! Function for sin(x^2) + real(dp) function sin_squared_function(x) + real(dp), intent(in) :: x + sin_squared_function = sin(x**2) + end function sin_squared_function + ! Function for sin(x) real(dp) function sin_function(x) real(dp), intent(in) :: x sin_function = sin(x) end function sin_function - ! Assertion subroutine - subroutine assert_test(result, expected, test_name) - real(dp), intent(in) :: result, expected + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + real(dp), intent(in) :: actual, expected character(len=*), intent(in) :: test_name real(dp), parameter :: tol = 1.0e-5_dp - if (abs(result - expected) < tol) then + if (abs(actual - expected) < tol) then print *, test_name, " PASSED" else print *, test_name, " FAILED" print *, " Expected: ", expected - print *, " Got: ", result + print *, " Got: ", actual stop 1 end if end subroutine assert_test end program test_trapezoidal_rule + From 224442551e4e0b792d57e7a3bf99242faa0ef923 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 20:25:45 +0200 Subject: [PATCH 3/7] Tests for simpson.f90 --- tests/maths/numerical_integration/simpson.f90 | 183 ++++++++++++++++++ .../maths/numerical_integration/trapezoid.f90 | 11 -- 2 files changed, 183 insertions(+), 11 deletions(-) create mode 100644 tests/maths/numerical_integration/simpson.f90 diff --git a/tests/maths/numerical_integration/simpson.f90 b/tests/maths/numerical_integration/simpson.f90 new file mode 100644 index 0000000..4796840 --- /dev/null +++ b/tests/maths/numerical_integration/simpson.f90 @@ -0,0 +1,183 @@ +!> Test program for the Simpson Rule module +!! +!! Created by: Your Name (https://github.com/YourGitHub) +!! in Pull Request: #32 +!! https://github.com/TheAlgorithms/Fortran/pull/32 +!! +!! This program provides test cases to validate the simpson_rule module against known integral values. + +program test_simpson_rule + use simpson_rule + implicit none + + ! Run test cases + call test_integral_x_squared_0_to_1() + call test_integral_x_squared_0_to_2() + call test_integral_sin_0_to_pi() + call test_integral_e_x_0_to_1() + call test_integral_1_over_x_1_to_e() + call test_integral_cos_0_to_pi_over_2() + call test_integral_x_cubed_0_to_1() + call test_integral_sin_x_squared_0_to_1() + + print *, "All tests completed." + +contains + + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) + subroutine test_integral_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be even + expected = 1.0_dp/3.0_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") + end subroutine test_integral_x_squared_0_to_1 + + ! Test case 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) + subroutine test_integral_x_squared_0_to_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 2.0_dp + panels_number = 1000000 ! Must be even + expected = 8.0_dp/3.0_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") + end subroutine test_integral_x_squared_0_to_2 + + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) + subroutine test_integral_sin_0_to_pi() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi + lower_bound = 0.0_dp + upper_bound = pi + panels_number = 1000000 ! Must be even + expected = 2.0_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, sin_function) + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") + end subroutine test_integral_sin_0_to_pi + + ! Test case 4: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) + subroutine test_integral_e_x_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be even + expected = exp(1.0_dp) - 1.0_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, exp_function) + call assert_test(integral_result, expected, "Test 4: ∫ e^x dx from 0 to 1") + end subroutine test_integral_e_x_0_to_1 + + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) + subroutine test_integral_1_over_x_1_to_e() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 1.0_dp + upper_bound = exp(1.0_dp) + panels_number = 1000000 ! Must be even + expected = 1.0_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, log_function) + call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e") + end subroutine test_integral_1_over_x_1_to_e + + ! Test case 6: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) + subroutine test_integral_cos_0_to_pi_over_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = pi/2.0_dp + panels_number = 1000000 ! Must be even + expected = 1.0_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, cos_function) + call assert_test(integral_result, expected, "Test 6: ∫ cos(x) dx from 0 to π/2") + end subroutine test_integral_cos_0_to_pi_over_2 + + ! Test case 7: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) + subroutine test_integral_x_cubed_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be even + expected = 0.25_dp + call simpson(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed) + call assert_test(integral_result, expected, "Test 7: ∫ x^3 dx from 0 to 1") + end subroutine test_integral_x_cubed_0_to_1 + + ! Test case 8: ∫ sin(x^2) dx from 0 to 1 (Approximate value) + subroutine test_integral_sin_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be even + expected = 0.310268_dp ! Approximate value, adjust tolerance as needed + call simpson(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) + call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1") + end subroutine test_integral_sin_x_squared_0_to_1 + + ! Function for x^2 + real(dp) function f_x_squared(x) + real(dp), intent(in) :: x + f_x_squared = x**2 + end function f_x_squared + + ! Function for e^x + real(dp) function exp_function(x) + real(dp), intent(in) :: x + exp_function = exp(x) + end function exp_function + + ! Function for 1/x + real(dp) function log_function(x) + real(dp), intent(in) :: x + log_function = 1.0_dp/x + end function log_function + + ! Function for cos(x) + real(dp) function cos_function(x) + real(dp), intent(in) :: x + cos_function = cos(x) + end function cos_function + + ! Function for x^3 + real(dp) function f_x_cubed(x) + real(dp), intent(in) :: x + f_x_cubed = x**3 + end function f_x_cubed + + ! Function for sin(x^2) + real(dp) function sin_squared_function(x) + real(dp), intent(in) :: x + sin_squared_function = sin(x**2) + end function sin_squared_function + + ! Function for sin(x) + real(dp) function sin_function(x) + real(dp), intent(in) :: x + sin_function = sin(x) + end function sin_function + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + real(dp), intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + real(dp), parameter :: tol = 1.0e-6_dp + + if (abs(actual - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, " Expected: ", expected + print *, " Got: ", actual + stop 1 + end if + end subroutine assert_test + +end program test_simpson_rule diff --git a/tests/maths/numerical_integration/trapezoid.f90 b/tests/maths/numerical_integration/trapezoid.f90 index f704ffe..dfa1c40 100644 --- a/tests/maths/numerical_integration/trapezoid.f90 +++ b/tests/maths/numerical_integration/trapezoid.f90 @@ -9,17 +9,6 @@ !! !! This program provides test cases to validate the trapezoidal_rule module against known integral values. -!> Test program for the Trapezoidal Rule module -!! -!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) -!! in Pull Request: #32 -!! https://github.com/TheAlgorithms/Fortran/pull/32 -!! -!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request -!! addressing bugs/corrections to this file. Thank you! -!! -!! This program provides test cases to validate the trapezoidal_rule module against known integral values. - program test_trapezoidal_rule use trapezoidal_rule implicit none From 48d903c5c3dc6aa0dab807b7a33423cbc28ab2c6 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 20:30:18 +0200 Subject: [PATCH 4/7] Tests for midpoint.f90 --- .../maths/numerical_integration/midpoint.f90 | 183 ++++++++++++++++++ tests/maths/numerical_integration/simpson.f90 | 2 +- 2 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 tests/maths/numerical_integration/midpoint.f90 diff --git a/tests/maths/numerical_integration/midpoint.f90 b/tests/maths/numerical_integration/midpoint.f90 new file mode 100644 index 0000000..65bdeb7 --- /dev/null +++ b/tests/maths/numerical_integration/midpoint.f90 @@ -0,0 +1,183 @@ +!> Test program for the Midpoint Rule module +!! +!! Created by: Your Name (https://github.com/YourGitHub) +!! in Pull Request: #XX +!! https://github.com/TheAlgorithms/Fortran/pull/XX +!! +!! This program provides test cases to validate the midpoint_rule module against known integral values. + +program test_midpoint_rule + use midpoint_rule + implicit none + + ! Run test cases + call test_integral_x_squared_0_to_1() + call test_integral_x_squared_0_to_2() + call test_integral_sin_0_to_pi() + call test_integral_e_x_0_to_1() + call test_integral_1_over_x_1_to_e() + call test_integral_cos_0_to_pi_over_2() + call test_integral_x_cubed_0_to_1() + call test_integral_sin_x_squared_0_to_1() + + print *, "All tests completed." + +contains + + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) + subroutine test_integral_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be a positive integer + expected = 1.0_dp / 3.0_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") + end subroutine test_integral_x_squared_0_to_1 + + ! Test case 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) + subroutine test_integral_x_squared_0_to_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 2.0_dp + panels_number = 1000000 ! Must be a positive integer + expected = 8.0_dp / 3.0_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") + end subroutine test_integral_x_squared_0_to_2 + + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) + subroutine test_integral_sin_0_to_pi() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + real(dp), parameter :: pi = 4.D0 * DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + lower_bound = 0.0_dp + upper_bound = pi + panels_number = 1000000 ! Must be a positive integer + expected = 2.0_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, sin_function) + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") + end subroutine test_integral_sin_0_to_pi + + ! Test case 4: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) + subroutine test_integral_e_x_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be a positive integer + expected = exp(1.0_dp) - 1.0_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, exp_function) + call assert_test(integral_result, expected, "Test 4: ∫ e^x dx from 0 to 1") + end subroutine test_integral_e_x_0_to_1 + + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) + subroutine test_integral_1_over_x_1_to_e() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 1.0_dp + upper_bound = exp(1.0_dp) + panels_number = 1000000 ! Must be a positive integer + expected = 1.0_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, log_function) + call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e") + end subroutine test_integral_1_over_x_1_to_e + + ! Test case 6: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) + subroutine test_integral_cos_0_to_pi_over_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = pi / 2.0_dp + panels_number = 1000000 ! Must be a positive integer + expected = 1.0_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, cos_function) + call assert_test(integral_result, expected, "Test 6: ∫ cos(x) dx from 0 to π/2") + end subroutine test_integral_cos_0_to_pi_over_2 + + ! Test case 7: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) + subroutine test_integral_x_cubed_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be a positive integer + expected = 0.25_dp + call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed) + call assert_test(integral_result, expected, "Test 7: ∫ x^3 dx from 0 to 1") + end subroutine test_integral_x_cubed_0_to_1 + + ! Test case 8: ∫ sin(x^2) dx from 0 to 1 (Approximate value) + subroutine test_integral_sin_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 1000000 ! Must be a positive integer + expected = 0.310268_dp ! Approximate value, adjust tolerance as needed + call midpoint(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) + call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1") + end subroutine test_integral_sin_x_squared_0_to_1 + + ! Function for x^2 + real(dp) function f_x_squared(x) + real(dp), intent(in) :: x + f_x_squared = x**2 + end function f_x_squared + + ! Function for e^x + real(dp) function exp_function(x) + real(dp), intent(in) :: x + exp_function = exp(x) + end function exp_function + + ! Function for 1/x + real(dp) function log_function(x) + real(dp), intent(in) :: x + log_function = 1.0_dp / x + end function log_function + + ! Function for cos(x) + real(dp) function cos_function(x) + real(dp), intent(in) :: x + cos_function = cos(x) + end function cos_function + + ! Function for x^3 + real(dp) function f_x_cubed(x) + real(dp), intent(in) :: x + f_x_cubed = x**3 + end function f_x_cubed + + ! Function for sin(x^2) + real(dp) function sin_squared_function(x) + real(dp), intent(in) :: x + sin_squared_function = sin(x**2) + end function sin_squared_function + + ! Function for sin(x) + real(dp) function sin_function(x) + real(dp), intent(in) :: x + sin_function = sin(x) + end function sin_function + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + real(dp), intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + real(dp), parameter :: tol = 1.0e-6_dp + + if (abs(actual - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, " Expected: ", expected + print *, " Got: ", actual + stop 1 + end if + end subroutine assert_test + +end program test_midpoint_rule diff --git a/tests/maths/numerical_integration/simpson.f90 b/tests/maths/numerical_integration/simpson.f90 index 4796840..1f4b364 100644 --- a/tests/maths/numerical_integration/simpson.f90 +++ b/tests/maths/numerical_integration/simpson.f90 @@ -52,7 +52,7 @@ end subroutine test_integral_x_squared_0_to_2 subroutine test_integral_sin_0_to_pi() real(dp) :: lower_bound, upper_bound, integral_result, expected integer :: panels_number - real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. lower_bound = 0.0_dp upper_bound = pi panels_number = 1000000 ! Must be even From 7532928ce0b9f6a8ace939ed83dd86ddc4189e86 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 20:49:25 +0200 Subject: [PATCH 5/7] Tests for gaussin_legendre.f90 --- .../gaussin_legendre.f90 | 172 ++++++++++++++++++ .../maths/numerical_integration/midpoint.f90 | 8 +- 2 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 tests/maths/numerical_integration/gaussin_legendre.f90 diff --git a/tests/maths/numerical_integration/gaussin_legendre.f90 b/tests/maths/numerical_integration/gaussin_legendre.f90 new file mode 100644 index 0000000..3be0d94 --- /dev/null +++ b/tests/maths/numerical_integration/gaussin_legendre.f90 @@ -0,0 +1,172 @@ +!> Test program for the Gaussian Legendre Quadrature module +!! +!! Created by: Your Name (https://github.com/YourGitHub) +!! in Pull Request: #32 +!! https://github.com/TheAlgorithms/Fortran/pull/32 +!! +!! This program provides test cases to validate the gaussian_legendre_quadrature module against known integral values. + +program test_gaussian_legendre_quadrature + use gaussian_legendre_quadrature + implicit none + + ! Run test cases + call test_integral_x_squared_0_to_1() + call test_integral_e_x_0_to_1() + call test_integral_sin_0_to_pi() + call test_integral_cos_0_to_pi_over_2() + call test_integral_1_over_x_1_to_e() + call test_integral_x_cubed_0_to_1() + call test_integral_sin_squared_0_to_pi() + call test_integral_1_over_x_1_to_e() + + print *, "All tests completed." + +contains + + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) + subroutine test_integral_x_squared_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 5 ! Adjust the number of quadrature points as needed from 1 to 5 + expected = 1.0_dp / 3.0_dp + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") + end subroutine test_integral_x_squared_0_to_1 + + ! Test case 2: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) + subroutine test_integral_e_x_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 3 + expected = exp(1.0_dp) - 1.0_dp + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, exp_function) + call assert_test(integral_result, expected, "Test 2: ∫ e^x dx from 0 to 1") + end subroutine test_integral_e_x_0_to_1 + + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) + subroutine test_integral_sin_0_to_pi() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + lower_bound = 0.0_dp + upper_bound = pi + panels_number = 5 + expected = 2.0_dp + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, sin_function) + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") + end subroutine test_integral_sin_0_to_pi + + ! Test case 4: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) + subroutine test_integral_cos_0_to_pi_over_2() + real(dp) :: lower_bound, upper_bound, integral_result, expected + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = pi / 2.0_dp + panels_number = 5 + expected = 1.0_dp + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, cos_function) + call assert_test(integral_result, expected, "Test 4: ∫ cos(x) dx from 0 to π/2") + end subroutine test_integral_cos_0_to_pi_over_2 + + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) + subroutine test_integral_1_over_x_1_to_e() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 1.0_dp + upper_bound = exp(1.0_dp) + panels_number = 5 + expected = 1.0_dp + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, log_function) + call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e") + end subroutine test_integral_1_over_x_1_to_e + + ! Test case 6: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) + subroutine test_integral_x_cubed_0_to_1() + real(dp) :: lower_bound, upper_bound, integral_result, expected + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = 1.0_dp + panels_number = 4 + expected = 0.25_dp + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed) + call assert_test(integral_result, expected, "Test 6: ∫ x^3 dx from 0 to 1") + end subroutine test_integral_x_cubed_0_to_1 + + ! Test case 7: ∫ sin^2(x) dx from 0 to π (Exact result = π/2 ≈ 1.5708) + subroutine test_integral_sin_squared_0_to_pi() + real(dp) :: lower_bound, upper_bound, integral_result, expected + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + integer :: panels_number + lower_bound = 0.0_dp + upper_bound = pi + panels_number = 5 + expected = 1.57084_dp ! Approximate value, adjust tolerance as needed + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) + call assert_test(integral_result, expected, "Test 7: ∫ sin^2(x) dx from 0 to π") + end subroutine test_integral_sin_squared_0_to_pi + + ! Function for x^2 + real(dp) function f_x_squared(x) + real(dp), intent(in) :: x + f_x_squared = x**2 + end function f_x_squared + + ! Function for e^x + real(dp) function exp_function(x) + real(dp), intent(in) :: x + exp_function = exp(x) + end function exp_function + + ! Function for 1/x + real(dp) function log_function(x) + real(dp), intent(in) :: x + log_function = 1.0_dp / x + end function log_function + + ! Function for cos(x) + real(dp) function cos_function(x) + real(dp), intent(in) :: x + cos_function = cos(x) + end function cos_function + + ! Function for x^3 + real(dp) function f_x_cubed(x) + real(dp), intent(in) :: x + f_x_cubed = x**3 + end function f_x_cubed + + ! Function for sin(x) + real(dp) function sin_function(x) + real(dp), intent(in) :: x + sin_function = sin(x) + end function sin_function + + ! Function for sin^2(x) + real(dp) function sin_squared_function(x) + real(dp), intent(in) :: x + sin_squared_function = sin(x)**2 + end function sin_squared_function + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + real(dp), intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + real(dp), parameter :: tol = 1.0e-5_dp + + if (abs(actual - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, " Expected: ", expected + print *, " Got: ", actual + stop 1 + end if + end subroutine assert_test + +end program test_gaussian_legendre_quadrature diff --git a/tests/maths/numerical_integration/midpoint.f90 b/tests/maths/numerical_integration/midpoint.f90 index 65bdeb7..247ba15 100644 --- a/tests/maths/numerical_integration/midpoint.f90 +++ b/tests/maths/numerical_integration/midpoint.f90 @@ -1,8 +1,8 @@ !> Test program for the Midpoint Rule module !! !! Created by: Your Name (https://github.com/YourGitHub) -!! in Pull Request: #XX -!! https://github.com/TheAlgorithms/Fortran/pull/XX +!! in Pull Request: #32 +!! https://github.com/TheAlgorithms/Fortran/pull/32 !! !! This program provides test cases to validate the midpoint_rule module against known integral values. @@ -116,8 +116,8 @@ subroutine test_integral_sin_x_squared_0_to_1() integer :: panels_number lower_bound = 0.0_dp upper_bound = 1.0_dp - panels_number = 1000000 ! Must be a positive integer - expected = 0.310268_dp ! Approximate value, adjust tolerance as needed + panels_number = 1000000 ! Must be a positive integer + expected = 0.310268_dp ! Approximate value, adjust tolerance as needed call midpoint(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1") end subroutine test_integral_sin_x_squared_0_to_1 From f8b3fe916abccf819a4e6a95ba4b0ce2fd852512 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 21:22:38 +0200 Subject: [PATCH 6/7] Implementing ctest-friendly tests for /maths/numerical_integration. Tests for monte_carlo.f90 --- .../numerical_integration/monte_carlo.f90 | 189 ++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 tests/maths/numerical_integration/monte_carlo.f90 diff --git a/tests/maths/numerical_integration/monte_carlo.f90 b/tests/maths/numerical_integration/monte_carlo.f90 new file mode 100644 index 0000000..6252e7e --- /dev/null +++ b/tests/maths/numerical_integration/monte_carlo.f90 @@ -0,0 +1,189 @@ +!> Test program for the Monte Carlo Integration module +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #32 +!! https://github.com/TheAlgorithms/Fortran/pull/32 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides test cases to validate the monte_carlo_integration module against known integral values. + +program test_monte_carlo_integration + use monte_carlo_integration + implicit none + + ! Run test cases + call test_integral_x_squared_0_to_1() + call test_integral_e_x_0_to_1() + call test_integral_sin_0_to_pi() + call test_integral_cos_0_to_pi_over_2() + call test_integral_1_over_x_1_to_e() + call test_integral_x_cubed_0_to_1() + call test_integral_sin_squared_0_to_1() + + print *, "All tests completed." + +contains + + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) + subroutine test_integral_x_squared_0_to_1() + real(dp) :: a, b, integral_result, error_estimate, expected + integer :: n + a = 0.0_dp + b = 1.0_dp + n = 1000000 + expected = 1.0_dp / 3.0_dp + + call monte_carlo(integral_result, error_estimate, a, b, n, f_x_squared) + call assert_test(integral_result, expected, error_estimate, "Test 1: ∫ x^2 dx from 0 to 1") + + end subroutine test_integral_x_squared_0_to_1 + + ! Test case 2: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) + subroutine test_integral_e_x_0_to_1() + real(dp) :: a, b, integral_result, error_estimate, expected + integer :: n + a = 0.0_dp + b = 1.0_dp + n = 1000000 + expected = exp(1.0_dp) - 1.0_dp + + call monte_carlo(integral_result, error_estimate, a, b, n, exp_function) + call assert_test(integral_result, expected, error_estimate, "Test 2: ∫ e^x dx from 0 to 1") + + end subroutine test_integral_e_x_0_to_1 + + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) + subroutine test_integral_sin_0_to_pi() + real(dp) :: a, b, integral_result, error_estimate, expected + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + integer :: n + a = 0.0_dp + b = pi + n = 1000000 + expected = 2.0_dp + + call monte_carlo(integral_result, error_estimate, a, b, n, sin_function) + call assert_test(integral_result, expected, error_estimate, "Test 3: ∫ sin(x) dx from 0 to π") + + end subroutine test_integral_sin_0_to_pi + + ! Test case 4: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) + subroutine test_integral_cos_0_to_pi_over_2() + real(dp) :: a, b, integral_result, error_estimate, expected + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + integer :: n + a = 0.0_dp + b = pi / 2.0_dp + n = 1000000 + expected = 1.0_dp + + call monte_carlo(integral_result, error_estimate, a, b, n, cos_function) + call assert_test(integral_result, expected, error_estimate, "Test 4: ∫ cos(x) dx from 0 to π/2") + + end subroutine test_integral_cos_0_to_pi_over_2 + + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) + subroutine test_integral_1_over_x_1_to_e() + real(dp) :: a, b, integral_result, error_estimate, expected + integer :: n + a = 1.0_dp + b = exp(1.0_dp) + n = 1000000 + expected = 1.0_dp + + call monte_carlo(integral_result, error_estimate, a, b, n, log_function) + call assert_test(integral_result, expected, error_estimate, "Test 5: ∫ (1/x) dx from 1 to e") + + end subroutine test_integral_1_over_x_1_to_e + + ! Test case 6: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) + subroutine test_integral_x_cubed_0_to_1() + real(dp) :: a, b, integral_result, error_estimate, expected + integer :: n + a = 0.0_dp + b = 1.0_dp + n = 1000000 + expected = 0.25_dp + + call monte_carlo(integral_result, error_estimate, a, b, n, f_x_cubed) + call assert_test(integral_result, expected, error_estimate, "Test 6: ∫ x^3 dx from 0 to 1") + + end subroutine test_integral_x_cubed_0_to_1 + + ! Test case 7: ∫ sin(x^2) dx from 0 to 1 (Approximate value) + subroutine test_integral_sin_squared_0_to_1() + real(dp) :: a, b, integral_result, error_estimate, expected + integer :: n + a = 0.0_dp + b = 1.0_dp + n = 1000000 + expected = 0.31026_dp ! Approximate value, adjust tolerance as needed + call monte_carlo(integral_result, error_estimate, a, b, n, sin_squared_function) + call assert_test(integral_result, expected, error_estimate, "Test 7: ∫ sin(x^2) dx from 0 to 1") + + end subroutine test_integral_sin_squared_0_to_1 + + ! Function for x^2 + real(dp) function f_x_squared(x) + real(dp), intent(in) :: x + f_x_squared = x**2 + end function f_x_squared + + ! Function for e^x + real(dp) function exp_function(x) + real(dp), intent(in) :: x + exp_function = exp(x) + end function exp_function + + ! Function for 1/x + real(dp) function log_function(x) + real(dp), intent(in) :: x + log_function = 1.0_dp/x + end function log_function + + ! Function for cos(x) + real(dp) function cos_function(x) + real(dp), intent(in) :: x + cos_function = cos(x) + end function cos_function + + ! Function for x^3 + real(dp) function f_x_cubed(x) + real(dp), intent(in) :: x + f_x_cubed = x**3 + end function f_x_cubed + + ! Function for sin(x^2) + real(dp) function sin_squared_function(x) + real(dp), intent(in) :: x + sin_squared_function = sin(x**2) + end function sin_squared_function + + ! Function for sin(x) + real(dp) function sin_function(x) + real(dp), intent(in) :: x + sin_function = sin(x) + end function sin_function + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, error_estimate, test_name) + real(dp), intent(in) :: actual, expected, error_estimate + character(len=*), intent(in) :: test_name + real(dp) :: tol + + ! Set the tolerance based on the error estimate + tol = max(1.0e-5_dp, 10.0_dp * error_estimate) ! Adjust as needed + + if (abs(actual - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, " Expected: ", expected + print *, " Got: ", actual + stop 1 + end if + end subroutine assert_test + +end program test_monte_carlo_integration From 3782898941d97e3e639955be6095a8b268c6bfe1 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 21:29:01 +0200 Subject: [PATCH 7/7] code_style fix --- tests/maths/numerical_integration/gaussin_legendre.f90 | 6 +++--- tests/maths/numerical_integration/midpoint.f90 | 10 +++++----- tests/maths/numerical_integration/monte_carlo.f90 | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/maths/numerical_integration/gaussin_legendre.f90 b/tests/maths/numerical_integration/gaussin_legendre.f90 index 3be0d94..2dbc6d2 100644 --- a/tests/maths/numerical_integration/gaussin_legendre.f90 +++ b/tests/maths/numerical_integration/gaussin_legendre.f90 @@ -31,7 +31,7 @@ subroutine test_integral_x_squared_0_to_1() lower_bound = 0.0_dp upper_bound = 1.0_dp panels_number = 5 ! Adjust the number of quadrature points as needed from 1 to 5 - expected = 1.0_dp / 3.0_dp + expected = 1.0_dp/3.0_dp call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") end subroutine test_integral_x_squared_0_to_1 @@ -67,7 +67,7 @@ subroutine test_integral_cos_0_to_pi_over_2() real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. integer :: panels_number lower_bound = 0.0_dp - upper_bound = pi / 2.0_dp + upper_bound = pi/2.0_dp panels_number = 5 expected = 1.0_dp call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, cos_function) @@ -126,7 +126,7 @@ end function exp_function ! Function for 1/x real(dp) function log_function(x) real(dp), intent(in) :: x - log_function = 1.0_dp / x + log_function = 1.0_dp/x end function log_function ! Function for cos(x) diff --git a/tests/maths/numerical_integration/midpoint.f90 b/tests/maths/numerical_integration/midpoint.f90 index 247ba15..9541796 100644 --- a/tests/maths/numerical_integration/midpoint.f90 +++ b/tests/maths/numerical_integration/midpoint.f90 @@ -31,7 +31,7 @@ subroutine test_integral_x_squared_0_to_1() lower_bound = 0.0_dp upper_bound = 1.0_dp panels_number = 1000000 ! Must be a positive integer - expected = 1.0_dp / 3.0_dp + expected = 1.0_dp/3.0_dp call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") end subroutine test_integral_x_squared_0_to_1 @@ -43,7 +43,7 @@ subroutine test_integral_x_squared_0_to_2() lower_bound = 0.0_dp upper_bound = 2.0_dp panels_number = 1000000 ! Must be a positive integer - expected = 8.0_dp / 3.0_dp + expected = 8.0_dp/3.0_dp call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") end subroutine test_integral_x_squared_0_to_2 @@ -52,7 +52,7 @@ end subroutine test_integral_x_squared_0_to_2 subroutine test_integral_sin_0_to_pi() real(dp) :: lower_bound, upper_bound, integral_result, expected integer :: panels_number - real(dp), parameter :: pi = 4.D0 * DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. lower_bound = 0.0_dp upper_bound = pi panels_number = 1000000 ! Must be a positive integer @@ -91,7 +91,7 @@ subroutine test_integral_cos_0_to_pi_over_2() real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. integer :: panels_number lower_bound = 0.0_dp - upper_bound = pi / 2.0_dp + upper_bound = pi/2.0_dp panels_number = 1000000 ! Must be a positive integer expected = 1.0_dp call midpoint(integral_result, lower_bound, upper_bound, panels_number, cos_function) @@ -137,7 +137,7 @@ end function exp_function ! Function for 1/x real(dp) function log_function(x) real(dp), intent(in) :: x - log_function = 1.0_dp / x + log_function = 1.0_dp/x end function log_function ! Function for cos(x) diff --git a/tests/maths/numerical_integration/monte_carlo.f90 b/tests/maths/numerical_integration/monte_carlo.f90 index 6252e7e..adea049 100644 --- a/tests/maths/numerical_integration/monte_carlo.f90 +++ b/tests/maths/numerical_integration/monte_carlo.f90 @@ -33,7 +33,7 @@ subroutine test_integral_x_squared_0_to_1() a = 0.0_dp b = 1.0_dp n = 1000000 - expected = 1.0_dp / 3.0_dp + expected = 1.0_dp/3.0_dp call monte_carlo(integral_result, error_estimate, a, b, n, f_x_squared) call assert_test(integral_result, expected, error_estimate, "Test 1: ∫ x^2 dx from 0 to 1") @@ -75,7 +75,7 @@ subroutine test_integral_cos_0_to_pi_over_2() real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. integer :: n a = 0.0_dp - b = pi / 2.0_dp + b = pi/2.0_dp n = 1000000 expected = 1.0_dp @@ -174,7 +174,7 @@ subroutine assert_test(actual, expected, error_estimate, test_name) real(dp) :: tol ! Set the tolerance based on the error estimate - tol = max(1.0e-5_dp, 10.0_dp * error_estimate) ! Adjust as needed + tol = max(1.0e-5_dp, 10.0_dp*error_estimate) ! Adjust as needed if (abs(actual - expected) < tol) then print *, test_name, " PASSED"