|
| 1 | +package com.thealgorithms.maths; |
| 2 | +import java.util.function.Function; |
| 3 | + |
| 4 | +/** |
| 5 | + * @author https://github.com/il798li/ |
| 6 | + * For more information on Riemann's approximation methods for integrals, visit {@link https://en.wikipedia.org/wiki/Riemann_sum this website} |
| 7 | + */ |
| 8 | +public class RiemannIntegration { |
| 9 | + private final double deltaX; |
| 10 | + |
| 11 | + /** |
| 12 | + * Creating the integration class. |
| 13 | + * @param deltaX This is essentially the change in each rectangle. You ideally want a very small positive values. If you want an extremely high accuracy, use {@code Double.MIN_DOUBLE}, but be warned: this will take an extremely long time. |
| 14 | + * @exception IllegalArgumentException when you pass a negative value. |
| 15 | + */ |
| 16 | + public RiemannIntegration (final double deltaX) { |
| 17 | + if (deltaX <= 0) { |
| 18 | + throw new IllegalArgumentException ("Accuracy must be a positive number. " + deltaX + " was passed instead."); |
| 19 | + } |
| 20 | + this.deltaX = deltaX; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals. |
| 25 | + */ |
| 26 | + public RiemannIntegration () { |
| 27 | + this(0.000000001); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Integrates a function. |
| 32 | + * @param function You will need to define this function, using {@code Function<Double, Double> function = x -> {...}}. |
| 33 | + * @param riemannApproximationMethod Each sub-interval can use different shapes to approximate the integral. It is recommended to use Trapezoidal sum. |
| 34 | + * @param lowerBoundary The lower bound of where your integration will start. Conventionally, this is the {@code a} value. |
| 35 | + * @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value. |
| 36 | + * @return The area under the curve between the given bounds. |
| 37 | + */ |
| 38 | + public double integrate(final Function<Double, Double> function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) { |
| 39 | + double value = 0; |
| 40 | + switch (riemannApproximationMethod) { |
| 41 | + case LEFT_RIEMANN_SUM: { |
| 42 | + for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { |
| 43 | + value += this.deltaX * function.apply (x); |
| 44 | + x += deltaX; |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | + case RIGHT_RIEMANN_SUM: { |
| 49 | + double x = lowerBoundary; |
| 50 | + while (x < upperBoundary) { |
| 51 | + x += deltaX; |
| 52 | + value += this.deltaX * function.apply (x); |
| 53 | + } |
| 54 | + break; |
| 55 | + } |
| 56 | + case TRAPEZOIDAL_RIEMANN_SUM: { |
| 57 | + value += function.apply (lowerBoundary) * deltaX; |
| 58 | + for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { |
| 59 | + value += function.apply (x) * deltaX * 2; |
| 60 | + } |
| 61 | + value += function.apply (upperBoundary) * deltaX; |
| 62 | + value /= 2; |
| 63 | + break; |
| 64 | + } |
| 65 | + case MIDPOINT_RIEMANN_SUM: { |
| 66 | + for (double x = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) { |
| 67 | + value += deltaX * function.apply (x); |
| 68 | + } |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + return value; |
| 73 | + } |
| 74 | + |
| 75 | + public enum RiemannApproximationMethod { |
| 76 | + LEFT_RIEMANN_SUM, |
| 77 | + RIGHT_RIEMANN_SUM, |
| 78 | + MIDPOINT_RIEMANN_SUM, |
| 79 | + TRAPEZOIDAL_RIEMANN_SUM |
| 80 | + } |
| 81 | + |
| 82 | + public static void main (String[] args) { |
| 83 | + example (); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * Feel free to look at how the implementation of this method to see how it works. |
| 89 | + */ |
| 90 | + public static final void example() { |
| 91 | + final Function<Double, Double> xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 |
| 92 | + final RiemannApproximationMethod riemannApproximationMethod = RiemannApproximationMethod.TRAPEZOIDAL_RIEMANN_SUM; // Chooses the Trapezoidal method for approximating the integral. |
| 93 | + final RiemannIntegration riemannIntegration = new RiemannIntegration (); |
| 94 | + final double result = riemannIntegration.integrate (xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3. |
| 95 | + System.out.println (result); |
| 96 | + } |
| 97 | +} |
0 commit comments