-
Notifications
You must be signed in to change notification settings - Fork 26.3k
numerical integration "trapz" function. #21610
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d6eb5a1
implement numerical integration with trapezoid rule
1b08e4a
do multiplication by scalar dx after sum
a353d46
foo
89d0e0c
better version for constant dx; add comments
c86ae48
test and fixes
f637977
flake8
ae7a142
fixes based on CR
35fad4e
fix test
26de417
fix includes
0f6de17
fix mis-copy-paste
4cd61cc
fixes based on cr
c01661c
Merge remote-tracking branch 'origin/master' into HEAD
d526517
typo in example
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 hidden or 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,72 @@ | ||
| #include <ATen/ATen.h> | ||
| #include <ATen/NativeFunctions.h> | ||
| #include <ATen/WrapDimUtils.h> | ||
| #include <ATen/core/DimVector.h> | ||
| #include <c10/util/Exception.h> | ||
|
|
||
| namespace at { | ||
| namespace native { | ||
| namespace { | ||
|
|
||
| // The estimated integral of a function y of x, | ||
| // sampled at points (y_1, ..., y_n) that are separated by distance (dx_1, ..., dx_{n-1}), | ||
| // is given by the trapezoid rule: | ||
| // | ||
| // \sum_{i=1}^{n-1} dx_i * (y_i + y_{i+1}) / 2 | ||
| // | ||
| // TODO: if we extend TensorIterator to accept 3 inputs, | ||
umanwizard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // we can probably make this a bit more performant. | ||
| Tensor do_trapz(const Tensor& y, const Tensor& dx, int64_t dim) { | ||
| Tensor left = y.slice(dim, 0, -1); | ||
| Tensor right = y.slice(dim, 1); | ||
|
|
||
| return ((left + right) * dx).sum(dim) / 2.; | ||
| } | ||
|
|
||
| // When dx is constant, the above formula simplifies | ||
| // to dx * [(\sum_{i=1}^n y_i) - (y_1 + y_n)/2] | ||
| Tensor do_trapz(const Tensor& y, double dx, int64_t dim) { | ||
| return (y.sum(dim) - (y.select(dim, 0) + y.select(dim, -1)) * (0.5)) * dx; | ||
umanwizard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Tensor zeros_like_except(const Tensor& y, int64_t dim) { | ||
| auto sizes = y.sizes().vec(); | ||
| dim = maybe_wrap_dim(dim, y.dim()); | ||
| sizes.erase(sizes.begin() + dim); | ||
| return at::zeros(sizes, y.options()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Tensor trapz(const Tensor& y, const Tensor& x, int64_t dim) { | ||
umanwizard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dim = maybe_wrap_dim(dim, y); | ||
| // asking for the integral with zero samples is a bit nonsensical, | ||
| // but we'll return "0" to match numpy behavior. | ||
| if (y.size(dim) == 0) { | ||
| return zeros_like_except(y, dim); | ||
| } | ||
| Tensor x_viewed; | ||
| if (x.dim() == 1) { | ||
| TORCH_CHECK(x.size(0) == y.size(dim), "trapz: There must be one `x` value for each sample point"); | ||
| DimVector sizes(y.dim(), 1); | ||
| sizes[dim] = x.size(0); | ||
| x_viewed = x.view(sizes); | ||
| } else { | ||
| x_viewed = x; | ||
| } | ||
| Tensor x_left = x_viewed.slice(dim, 0, -1); | ||
| Tensor x_right = x_viewed.slice(dim, 1); | ||
|
|
||
| Tensor dx = x_right - x_left; | ||
| return do_trapz(y, dx, dim); | ||
| } | ||
|
|
||
| Tensor trapz(const Tensor& y, double dx, int64_t dim) { | ||
| // see above | ||
| if (y.size(dim) == 0) { | ||
| return zeros_like_except(y, dim); | ||
| } | ||
| return do_trapz(y, dx, dim); | ||
| } | ||
|
|
||
| }} // namespace at::native | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.