Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Several Numerical Integration Algorithms in maths/ #25

Conversation

Ramy-Badr-Ahmed
Copy link
Member

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed commented Sep 24, 2024

Description:

This pull request introduces the implementation of several numerical integration methods for approximating definite integrals over a specified interval.

Definition:

The following numerical integration methods are included:

  • Trapezoidal Rule (trapezoid.f90):
    Approximates the integral by dividing the interval into trapezoids and summing their areas.

  • Simpson's Rule (simpson.f90):
    Approximates the integral by fitting parabolas to segments of the function and is typically more accurate than the Trapezoidal Rule

  • Midpoint Rule (midpoint.f90):
    Approximates the integral by evaluating the function at the midpoint of each subinterval.

  • Monte Carlo Integration (with OpenMP parallelization, monte_carlo.f90):
    A probabilistic method that estimates the integral by sampling random points in the interval and averaging the function values.
    It utilizes the omp_lib with the !$omp parallel do directive for parallel execution.

  • Gaussian-Legendre Quadrature (using precomputed table values, gauss_legendre.f90):
    A highly accurate method that approximates the integral using a weighted sum of function values at specified points (nodes) within the integration interval.
    Implements Gauss-Legendre quadrature using precomputed nodes and weights for orders n=1 through n=5.

Implementation Details

  • An interface has been implemented in each module method to accept any user-defined mathematical function for integration, where functions can be passed as arguments.
  • Each implementation follows the structured module approach, including interfaces for passing the integration limits, number of points/samples, and the function to integrate.
  • The implementations leverage array slicing and vectorized operations to reduce manual loops.

Example Usage

Examples for each method are provided, demonstrating how to call the respective subroutines to approximate a definite integral of this sample function:

Input:
f(x) = exp(-x^2) * cos(2.0_dp * x)
a = -1.0_dp    !! Lower bound
b = 1.0_dp     !! Upper bound

examples/../trapezoidal.f90:

Input:
n = 1E6    !! Number of subdivisions

Output: Trapezoidal rule yields:     0.858195

examples/../simpson.f90:

Input:
n = 100    !! Number of subdivisions (must be even)

Output: Simpson's rule yields:   0.85819555

examples/../midpoint.f90:

Input:
n = 400    !! Number of subdivisionss

Output: Midpoint rule yields:     0.858196

examples/../monte_carlo.f90:

Input:
n = 1E6   !! Number of random samples

Output: Monte Carlo result:     0.857180 +-     0.000794

examples/../gaussian_legendre.f90:

Input:
n = 5    !! Number of quadrature points

Output: Gaussian Quadrature result:     0.858574

Reference

Numerical Recipes: The Art of Scientific Computing, 3rd Edition

Copy link
Member Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SatinWukerORIG ,
Looking forward to your review 🙂

Ramy-Badr-Ahmed

This comment was marked as outdated.

@Ramy-Badr-Ahmed

This comment was marked as outdated.

Ramy-Badr-Ahmed

This comment was marked as outdated.

Copy link
Member

@SatinWukerORIG SatinWukerORIG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great implementation!

Copy link
Member Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflict resolved, ready for merge.

ci
check_code_format

Copy link
Member

@SatinWukerORIG SatinWukerORIG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great. A lot of changes lol.
Although the contribution guideline suggests not using variable names like a, b, and i, it is understandable in this case because it is for math modules. Do you think it is more readable if you change these variable names to full name?

@Ramy-Badr-Ahmed
Copy link
Member Author

Ramy-Badr-Ahmed commented Sep 30, 2024

It looks great. A lot of changes lol. Although the contribution guideline suggests not using variable names like a, b, and i, it is understandable in this case because it is for math modules. Do you think it is more readable if you change these variable names to full name?

It depends 🙂

I have renamed them in the examples files, but left them shortened in the modules files as there are explainations in the input section of the doc blocs for what they stand for. I think the forumlae in the module files would better stay in compact form with single-letter variables where comments will be helpful in this case.

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed changed the title Implemented Several Numerical Integration Algorithms Implemented Several Numerical Integration Algorithms in maths/ Sep 30, 2024
@Ramy-Badr-Ahmed

This comment was marked as outdated.

@SatinWukerORIG SatinWukerORIG merged commit 056b2d3 into TheAlgorithms:main Oct 9, 2024
3 checks passed
@Ramy-Badr-Ahmed
Copy link
Member Author

Ramy-Badr-Ahmed commented Oct 12, 2024

Note

PR is complemented with #32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants