Skip to content

Commit a36c389

Browse files
committed
Update RiemannIntegration.java
used clang-format to format my java code
1 parent 6c14d8d commit a36c389

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/main/java/com/thealgorithms/maths/RiemannIntegration.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/**
55
* @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}
6+
* @Info https://math.libretexts.org/Bookshelves/Calculus/Calculus_3e_(Apex)/05%3A_Integration/5.03%3A_Riemann_Sums
77
*/
88
public class RiemannIntegration {
99
private final double deltaX;
@@ -13,17 +13,17 @@ public class RiemannIntegration {
1313
* @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.
1414
* @exception IllegalArgumentException when you pass a negative value.
1515
*/
16-
public RiemannIntegration (final double deltaX) {
16+
public RiemannIntegration(final double deltaX) {
1717
if (deltaX <= 0) {
18-
throw new IllegalArgumentException ("Accuracy must be a positive number. " + deltaX + " was passed instead.");
18+
throw new IllegalArgumentException("Accuracy must be a positive number. " + deltaX + " was passed instead.");
1919
}
2020
this.deltaX = deltaX;
2121
}
2222

2323
/**
2424
* Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals.
2525
*/
26-
public RiemannIntegration () {
26+
public RiemannIntegration() {
2727
this(0.000000001);
2828
}
2929

@@ -35,12 +35,12 @@ public RiemannIntegration () {
3535
* @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value.
3636
* @return The area under the curve between the given bounds.
3737
*/
38-
public double integrate(final Function<Double, Double> function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) {
38+
public double integrate(final Function < Double, Double > function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) {
3939
double value = 0;
4040
switch (riemannApproximationMethod) {
4141
case LEFT_RIEMANN_SUM: {
4242
for (double x = lowerBoundary; x < upperBoundary; x += deltaX) {
43-
value += this.deltaX * function.apply (x);
43+
value += this.deltaX * function.apply(x);
4444
x += deltaX;
4545
}
4646
break;
@@ -49,22 +49,22 @@ public double integrate(final Function<Double, Double> function, final RiemannAp
4949
double x = lowerBoundary;
5050
while (x < upperBoundary) {
5151
x += deltaX;
52-
value += this.deltaX * function.apply (x);
52+
value += this.deltaX * function.apply(x);
5353
}
5454
break;
5555
}
5656
case TRAPEZOIDAL_RIEMANN_SUM: {
57-
value += function.apply (lowerBoundary) * deltaX;
57+
value += function.apply(lowerBoundary) * deltaX;
5858
for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) {
59-
value += function.apply (x) * deltaX * 2;
59+
value += function.apply(x) * deltaX * 2;
6060
}
61-
value += function.apply (upperBoundary) * deltaX;
61+
value += function.apply(upperBoundary) * deltaX;
6262
value /= 2;
6363
break;
6464
}
6565
case MIDPOINT_RIEMANN_SUM: {
6666
for (double x = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) {
67-
value += deltaX * function.apply (x);
67+
value += deltaX * function.apply(x);
6868
}
6969
break;
7070
}
@@ -79,19 +79,18 @@ public enum RiemannApproximationMethod {
7979
TRAPEZOIDAL_RIEMANN_SUM
8080
}
8181

82-
public static void main (String[] args) {
83-
example ();
82+
public static void main(String[] args) {
83+
example();
8484
}
8585

86-
8786
/**
8887
* Feel free to look at how the implementation of this method to see how it works.
8988
*/
9089
public static final void example() {
91-
final Function<Double, Double> xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2
90+
final Function < Double, Double > xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2
9291
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);
92+
final RiemannIntegration riemannIntegration = new RiemannIntegration();
93+
final double result = riemannIntegration.integrate(xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3.
94+
System.out.println(result);
9695
}
9796
}

0 commit comments

Comments
 (0)