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

gaussian_quadrature #662

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
* [Frizzy Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/frizzy_number.rs)
* [Gaussian Elimination](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gaussian_elimination.rs)
* [Gaussian Error Linear Unit](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gaussian_error_linear_unit.rs)
* [Gaussian_quadrature](https://github.com/Rust/blob/new-feature-branch/src/math/gaussian_quadrature.rs)
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gcd_of_n_numbers.rs)
* [Geometric Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/geometric_series.rs)
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Rust/blob/master/src/math/greatest_common_divisor.rs)
Expand Down
46 changes: 46 additions & 0 deletions src/math/gaussian_quadrature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Gaussian Quadrature Module
//https://en.wikipedia.org/wiki/Gaussian_quadrature

pub fn gaussian_quadrature(a: f64, b: f64, f: impl Fn(f64) -> f64, num_points: usize) -> f64 {
let (points, weights) = get_gaussian_quadrature_points_weights(num_points);

let mut result = 0.0;

for i in 0..num_points {
let x_i = 0.5 * (a + b) + 0.5 * (b - a) * points[i];
result += weights[i] * f(x_i);
}

0.5 * (b - a) * result
}

fn get_gaussian_quadrature_points_weights(num_points: usize) -> (Vec<f64>, Vec<f64>) {
// Hardcoded values for Gaussian Quadrature points and weights
match num_points {
1 => (vec![0.0], vec![2.0]),
2 => (vec![-1.0 / 3.0, 1.0 / 3.0], vec![1.0, 1.0]),
3 => (
vec![-0.7745966692414834, 0.0, 0.7745966692414834],
vec![0.5555555555555556, 0.8888888888888888, 0.5555555555555556],
),
_ => unimplemented!("Gaussian Quadrature not implemented for this number of points"),
}
}
Comment on lines +19 to +30
Copy link
Member

Choose a reason for hiding this comment

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

I would highly suggest to generalize it for any number of points/order. It also seems that it uses Legendre polynomials. It also could be more general.

Copy link
Author

Choose a reason for hiding this comment

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

Hmm,i am kinda cofused here ,can you please tell me more information about what i should add?

Copy link
Member

Choose a reason for hiding this comment

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

Let's start with basic stuff: it would be great to call gaussian_quadrature with order being 20 and obtain a valid result.

Copy link
Author

Choose a reason for hiding this comment

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

Ahh, i see what you mean,sorry for being late ,been kinda busy,thanks for the explanation my man.

Copy link
Author

Choose a reason for hiding this comment

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

i keep trying to add the options but the result just keeps getting more complicated and wrong ,my purpose in here wat to give an example of another numerical approach to integrals,after some research 3 is the most command order for this method (and most accurate) ,mind if i leave it like this?

Copy link
Member

Choose a reason for hiding this comment

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

I think we should go for the general case.


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_gaussian_quadrature_basic() {
let a = 0.0;
let b = 1.0;
let f = |x: f64| x.powi(2);
let num_points = 3;
let expected = 1.0 / 3.0;
let eps = 0.0001;

let result = gaussian_quadrature(a, b, f, num_points);
assert!((result - expected).abs() < eps);
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod field;
mod frizzy_number;
mod gaussian_elimination;
mod gaussian_error_linear_unit;
mod gaussian_quadrature;
mod gcd_of_n_numbers;
mod geometric_series;
mod greatest_common_divisor;
Expand Down Expand Up @@ -115,6 +116,7 @@ pub use self::field::{Field, PrimeField};
pub use self::frizzy_number::get_nth_frizzy;
pub use self::gaussian_elimination::gaussian_elimination;
pub use self::gaussian_error_linear_unit::gaussian_error_linear_unit;
pub use self::gaussian_quadrature::gaussian_quadrature;
pub use self::gcd_of_n_numbers::gcd;
pub use self::geometric_series::geometric_series;
pub use self::greatest_common_divisor::{
Expand Down