-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Closed
gaussian_quadrature #662
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4c6e7da
gaussian_quadrature
5fb20a3
first alg fix
bae3ef4
first alg fix
4861caa
Update gaussian_quadrature.rs
todi-mih 157b938
Merge branch 'master' into new-feature-branch
todi-mih 7ce2a5c
Update gaussian_quadrature.rs
todi-mih 0232070
Update gaussian_quadrature.rs
todi-mih cf6b7fd
Update gaussian_quadrature.rs
todi-mih cf763f1
Update gaussian_quadrature.rs
todi-mih e53f81a
Update gaussian_quadrature.rs
todi-mih 39e08b6
Update gaussian_quadrature.rs
todi-mih 8bf426a
Update gaussian_quadrature.rs
todi-mih 1d2e6f9
Update gaussian_quadrature.rs
todi-mih File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Gaussian Quadrature Module | ||
// https://en.wikipedia.org/wiki/Gaussian_quadrature | ||
|
||
pub fn gaussian_quadrature(a: f64, b: f64, f: impl Fn(f64) -> f64, order: usize) -> f64 { | ||
let (points, weights) = get_gaussian_quadrature_points_weights(order); | ||
|
||
let result: f64 = points | ||
.iter() | ||
.zip(weights.iter()) | ||
.map(|(&point, &weight)| { | ||
let x_i = 0.5 * ((a + b) + (b - a) * point); | ||
weight * f(x_i) | ||
}) | ||
.sum(); | ||
|
||
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"), | ||
} | ||
} | ||
|
||
#[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 order = 3; | ||
let expected = 1.0 / 3.0; | ||
let eps = 0.0001; | ||
|
||
let result = gaussian_quadrature(a, b, f, order); | ||
assert!((result - expected).abs() < eps); | ||
|
||
let result_neg = gaussian_quadrature(b, a, f, order); | ||
assert!((result_neg + expected).abs() < eps); | ||
} | ||
todi-mih marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
withorder
being20
and obtain a valid result.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.