-
Notifications
You must be signed in to change notification settings - Fork 191
[stdlib_math] Add function diff
#605
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2827e9a
Add procedure `diff`.
zoziha b6f0ab1
Merge branch 'master' of https://github.com/fortran-lang/stdlib into …
zoziha ec00dfe
Update implementaion of `diff` procedure.
zoziha 74c5c11
Improve the efficiency of diff.
zoziha cc2344e
Minor update of diff doc.
zoziha 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
!> Inspired by original code (MIT license) written in 2016 by Keurfon Luu (keurfonluu@outlook.com) | ||
!> https://github.com/keurfonluu/Forlab | ||
|
||
#:include "common.fypp" | ||
#:set RI_KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES | ||
submodule (stdlib_math) stdlib_math_diff | ||
|
||
implicit none | ||
|
||
contains | ||
|
||
!> `diff` computes differences of adjacent elements of an array. | ||
|
||
#:for k1, t1 in RI_KINDS_TYPES | ||
pure module function diff_1_${k1}$(x, n, prepend, append) result(y) | ||
${t1}$, intent(in) :: x(:) | ||
integer, intent(in), optional :: n | ||
${t1}$, intent(in), optional :: prepend(:), append(:) | ||
${t1}$, allocatable :: y(:) | ||
integer :: size_prepend, size_append, size_x, size_work | ||
integer :: n_, i | ||
|
||
n_ = optval(n, 1) | ||
if (n_ <= 0) then | ||
y = x | ||
return | ||
end if | ||
|
||
size_prepend = 0 | ||
size_append = 0 | ||
if (present(prepend)) size_prepend = size(prepend) | ||
if (present(append)) size_append = size(append) | ||
size_x = size(x) | ||
size_work = size_x + size_prepend + size_append | ||
|
||
if (size_work <= n_) then | ||
allocate(y(0)) | ||
return | ||
end if | ||
|
||
!> Use a quick exit for the common case, to avoid memory allocation. | ||
if (size_prepend == 0 .and. size_append == 0 .and. n_ == 1) then | ||
y = x(2:) - x(1:size_x-1) | ||
return | ||
end if | ||
|
||
block | ||
${t1}$ :: work(size_work) | ||
if (size_prepend > 0) work(:size_prepend) = prepend | ||
work(size_prepend+1:size_prepend+size_x) = x | ||
if (size_append > 0) work(size_prepend+size_x+1:) = append | ||
|
||
do i = 1, n_ | ||
work(1:size_work-i) = work(2:size_work-i+1) - work(1:size_work-i) | ||
end do | ||
|
||
y = work(1:size_work-n_) | ||
end block | ||
|
||
end function diff_1_${k1}$ | ||
|
||
pure module function diff_2_${k1}$(x, n, dim, prepend, append) result(y) | ||
${t1}$, intent(in) :: x(:, :) | ||
integer, intent(in), optional :: n, dim | ||
${t1}$, intent(in), optional :: prepend(:, :), append(:, :) | ||
${t1}$, allocatable :: y(:, :) | ||
integer :: size_prepend, size_append, size_x, size_work | ||
integer :: n_, dim_, i | ||
|
||
n_ = optval(n, 1) | ||
if (n_ <= 0) then | ||
y = x | ||
return | ||
end if | ||
|
||
size_prepend = 0 | ||
size_append = 0 | ||
if (present(dim)) then | ||
if (dim == 1 .or. dim == 2) then | ||
dim_ = dim | ||
else | ||
dim_ = 1 | ||
end if | ||
else | ||
dim_ = 1 | ||
end if | ||
|
||
if (present(prepend)) size_prepend = size(prepend, dim_) | ||
if (present(append)) size_append = size(append, dim_) | ||
size_x = size(x, dim_) | ||
size_work = size_x + size_prepend + size_append | ||
|
||
if (size_work <= n_) then | ||
allocate(y(0, 0)) | ||
return | ||
end if | ||
|
||
!> Use a quick exit for the common case, to avoid memory allocation. | ||
if (size_prepend == 0 .and. size_append == 0 .and. n_ == 1) then | ||
if (dim_ == 1) then | ||
y = x(2:, :) - x(1:size_x-1, :) | ||
elseif (dim_ == 2) then | ||
y = x(:, 2:) - x(:, 1:size_x-1) | ||
end if | ||
return | ||
end if | ||
|
||
if (dim_ == 1) then | ||
block | ||
${t1}$ :: work(size_work, size(x, 2)) | ||
if (size_prepend > 0) work(1:size_prepend, :) = prepend | ||
work(size_prepend+1:size_x+size_prepend, :) = x | ||
if (size_append > 0) work(size_x+size_prepend+1:, :) = append | ||
do i = 1, n_ | ||
work(1:size_work-i, :) = work(2:size_work-i+1, :) - work(1:size_work-i, :) | ||
end do | ||
|
||
y = work(1:size_work-n_, :) | ||
end block | ||
|
||
elseif (dim_ == 2) then | ||
block | ||
${t1}$ :: work(size(x, 1), size_work) | ||
if (size_prepend > 0) work(:, 1:size_prepend) = prepend | ||
work(:, size_prepend+1:size_x+size_prepend) = x | ||
if (size_append > 0) work(:, size_x+size_prepend+1:) = append | ||
do i = 1, n_ | ||
work(:, 1:size_work-i) = work(:, 2:size_work-i+1) - work(:, 1:size_work-i) | ||
end do | ||
|
||
y = work(:, 1:size_work-n_) | ||
end block | ||
|
||
end if | ||
|
||
end function diff_2_${k1}$ | ||
#:endfor | ||
|
||
end submodule stdlib_math_diff |
Oops, something went wrong.
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.