Skip to content

Commit

Permalink
Cleanup simpsons (TheAlgorithms#658)
Browse files Browse the repository at this point in the history
* tests: move tests from `simpson_integration.rs` to `simpson.rs`

* refactor: remove `simpson_integration.rs`

* style: move `simpsons_rule` to `math`
  • Loading branch information
vil02 authored Jan 14, 2024
1 parent 0d8e86b commit 2bbc45c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 73 deletions.
3 changes: 1 addition & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
* Optimization
* [Adam](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/optimization/adam.rs)
* [Gradient Descent](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/optimization/gradient_descent.rs)
* [Simpson](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/simpson.rs)
* Math
* [Abs](https://github.com/TheAlgorithms/Rust/blob/master/src/math/abs.rs)
* [Aliquot Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/aliquot_sum.rs)
Expand Down Expand Up @@ -218,7 +217,7 @@
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs)
* [Sigmoid](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sigmoid.rs)
* [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs)
* [Simpson Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpson_integration.rs)
* [Simpsons Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpsons_integration.rs)
* [Softmax](https://github.com/TheAlgorithms/Rust/blob/master/src/math/softmax.rs)
* [Sprague Grundy Theorem](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sprague_grundy_theorem.rs)
* [Square Pyramidal Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_pyramidal_numbers.rs)
Expand Down
2 changes: 0 additions & 2 deletions src/machine_learning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod k_means;
mod linear_regression;
mod loss_function;
mod optimization;
mod simpson;

pub use self::cholesky::cholesky;
pub use self::k_means::k_means;
Expand All @@ -13,4 +12,3 @@ pub use self::loss_function::mae_loss;
pub use self::loss_function::mse_loss;
pub use self::optimization::gradient_descent;
pub use self::optimization::Adam;
pub use self::simpson::simpsons_rule;
4 changes: 2 additions & 2 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod relu;
mod sieve_of_eratosthenes;
mod sigmoid;
mod signum;
mod simpson_integration;
mod simpsons_integration;
mod softmax;
mod sprague_grundy_theorem;
mod square_pyramidal_numbers;
Expand Down Expand Up @@ -154,7 +154,7 @@ pub use self::relu::relu;
pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes;
pub use self::sigmoid::sigmoid;
pub use self::signum::signum;
pub use self::simpson_integration::simpson_integration;
pub use self::simpsons_integration::simpsons_integration;
pub use self::softmax::softmax;
pub use self::sprague_grundy_theorem::calculate_grundy_number;
pub use self::square_pyramidal_numbers::square_pyramidal_number;
Expand Down
54 changes: 0 additions & 54 deletions src/math/simpson_integration.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn simpsons_rule<F>(f: F, a: f64, b: f64, n: usize) -> f64
pub fn simpsons_integration<F>(f: F, a: f64, b: f64, n: usize) -> f64
where
F: Fn(f64) -> f64,
{
Expand All @@ -18,12 +18,12 @@ mod tests {
use super::*;

#[test]
fn test_simpsons_rule() {
fn test_simpsons_integration() {
let f = |x: f64| x.powi(2);
let a = 0.0;
let b = 1.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
assert!((result - 1.0 / 3.0).abs() < 1e-6);
}

Expand All @@ -33,7 +33,7 @@ mod tests {
let a = 0.0;
let b = 1.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
let error = (1.0 / 3.0 - result).abs();
assert!(error < 1e-6);
}
Expand All @@ -44,10 +44,10 @@ mod tests {
let a = 0.0;
let b = 1.0;
let n = 100;
let result1 = simpsons_rule(f, a, b, n);
let result2 = simpsons_rule(f, a, b, 2 * n);
let result3 = simpsons_rule(f, a, b, 4 * n);
let result4 = simpsons_rule(f, a, b, 8 * n);
let result1 = simpsons_integration(f, a, b, n);
let result2 = simpsons_integration(f, a, b, 2 * n);
let result3 = simpsons_integration(f, a, b, 4 * n);
let result4 = simpsons_integration(f, a, b, 8 * n);
assert!((result1 - result2).abs() < 1e-6);
assert!((result2 - result3).abs() < 1e-6);
assert!((result3 - result4).abs() < 1e-6);
Expand All @@ -59,7 +59,7 @@ mod tests {
let a = 0.0;
let b = 1.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
assert!((result + 1.0 / 3.0).abs() < 1e-6);
}

Expand All @@ -69,7 +69,7 @@ mod tests {
let a = 1.0;
let b = 2.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
assert!((result - 7.0 / 3.0).abs() < 1e-6);
}

Expand All @@ -79,7 +79,7 @@ mod tests {
let a = 0.0;
let b = 2.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
assert!((result - 8.0 / 3.0).abs() < 1e-6);
}

Expand All @@ -89,7 +89,7 @@ mod tests {
let a = 1.0;
let b = 2.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
assert!((result - 7.0 / 3.0).abs() < 1e-6);
}

Expand All @@ -99,7 +99,29 @@ mod tests {
let a = 1.0;
let b = 2.0;
let n = 100;
let result = simpsons_rule(f, a, b, n);
let result = simpsons_integration(f, a, b, n);
assert!((result + 7.0 / 3.0).abs() < 1e-6);
}

#[test]
fn parabola_curve_length() {
// Calculate the length of the curve f(x) = x^2 for -5 <= x <= 5
// We should integrate sqrt(1 + (f'(x))^2)
let function = |x: f64| -> f64 { (1.0 + 4.0 * x * x).sqrt() };
let result = simpsons_integration(function, -5.0, 5.0, 1_000);
let integrated = |x: f64| -> f64 { (x * function(x) / 2.0) + ((2.0 * x).asinh() / 4.0) };
let expected = integrated(5.0) - integrated(-5.0);
assert!((result - expected).abs() < 1e-9);
}

#[test]
fn area_under_cosine() {
use std::f64::consts::PI;
// Calculate area under f(x) = cos(x) + 5 for -pi <= x <= pi
// cosine should cancel out and the answer should be 2pi * 5
let function = |x: f64| -> f64 { x.cos() + 5.0 };
let result = simpsons_integration(function, -PI, PI, 1_000);
let expected = 2.0 * PI * 5.0;
assert!((result - expected).abs() < 1e-9);
}
}

0 comments on commit 2bbc45c

Please sign in to comment.