-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Support non-1 indices and fix type problems in DFT (fixes #17896) #17919
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,6 +489,7 @@ export | |
| cat, | ||
| checkbounds, | ||
| checkindex, | ||
| circcopy!, | ||
| circshift, | ||
| circshift!, | ||
| clamp!, | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -645,6 +645,22 @@ See also `circshift`. | |
| end | ||
| circshift!(dest::AbstractArray, src, shiftamt) = circshift!(dest, src, (shiftamt...,)) | ||
|
|
||
| # For each dimension, we copy the first half of src to the second half | ||
| # of dest, and the second half of src to the first half of dest. This | ||
| # uses a recursive bifurcation strategy so that these splits can be | ||
| # encoded by ranges, which means that we need only one call to `mod` | ||
| # per dimension rather than one call per index. | ||
| # `rdest` and `rsrc` are tuples-of-ranges that grow one dimension at a | ||
| # time; when all the dimensions have been filled in, you call `copy!` | ||
| # for that block. In other words, in two dimensions schematically we | ||
| # have the following call sequence (--> means a call): | ||
| # circshift!(dest, src, shiftamt) --> | ||
| # _circshift!(dest, src, ("first half of dim1",)) --> | ||
| # _circshift!(dest, src, ("first half of dim1", "first half of dim2")) --> copy! | ||
| # _circshift!(dest, src, ("first half of dim1", "second half of dim2")) --> copy! | ||
| # _circshift!(dest, src, ("second half of dim1",)) --> | ||
| # _circshift!(dest, src, ("second half of dim1", "first half of dim2")) --> copy! | ||
| # _circshift!(dest, src, ("second half of dim1", "second half of dim2")) --> copy! | ||
| @inline function _circshift!(dest, rdest, src, rsrc, | ||
| inds::Tuple{AbstractUnitRange,Vararg{Any}}, | ||
| shiftamt::Tuple{Integer,Vararg{Any}}) | ||
|
|
@@ -662,6 +678,68 @@ function _circshift!(dest, rdest, src, rsrc, inds, shiftamt) | |
| copy!(dest, CartesianRange(rdest), src, CartesianRange(rsrc)) | ||
| end | ||
|
|
||
| # circcopy! | ||
| """ | ||
| circcopy!(dest, src) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for 0.6 this should go in the rst manual if exported |
||
|
|
||
| Copy `src` to `dest`, indexing each dimension modulo its length. | ||
| `src` and `dest` must have the same size, but can be offset in | ||
| their indices; any offset results in a (circular) wraparound. If the | ||
| arrays have overlapping indices, then on the domain of the overlap | ||
| `dest` agrees with `src`. | ||
|
|
||
| ```julia | ||
| julia> src = reshape(collect(1:16), (4,4)) | ||
| 4×4 Array{Int64,2}: | ||
| 1 5 9 13 | ||
| 2 6 10 14 | ||
| 3 7 11 15 | ||
| 4 8 12 16 | ||
|
|
||
| julia> dest = OffsetArray{Int}((0:3,2:5)) | ||
|
|
||
| julia> circcopy!(dest, src) | ||
| OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}} with indices 0:3×2:5: | ||
| 8 12 16 4 | ||
| 5 9 13 1 | ||
| 6 10 14 2 | ||
| 7 11 15 3 | ||
|
|
||
| julia> dest[1:3,2:4] == src[1:3,2:4] | ||
| true | ||
| ``` | ||
| """ | ||
| function circcopy!(dest, src) | ||
| dest === src && throw(ArgumentError("dest and src must be separate arrays")) | ||
| indssrc, indsdest = indices(src), indices(dest) | ||
| if (szsrc = map(length, indssrc)) != (szdest = map(length, indsdest)) | ||
| throw(DimensionMismatch("src and dest must have the same sizes (got $szsrc and $szdest)")) | ||
| end | ||
| shift = map((isrc, idest)->first(isrc)-first(idest), indssrc, indsdest) | ||
| all(x->x==0, shift) && return copy!(dest, src) | ||
| _circcopy!(dest, (), indsdest, src, (), indssrc) | ||
| end | ||
|
|
||
| # This uses the same strategy described above for _circshift! | ||
| @inline function _circcopy!(dest, rdest, indsdest::Tuple{AbstractUnitRange,Vararg{Any}}, | ||
| src, rsrc, indssrc::Tuple{AbstractUnitRange,Vararg{Any}}) | ||
| indd1, inds1 = indsdest[1], indssrc[1] | ||
| l = length(indd1) | ||
| s = mod(first(inds1)-first(indd1), l) | ||
| sdf = first(indd1)+s | ||
| rd1, rd2 = first(indd1):sdf-1, sdf:last(indd1) | ||
| ssf = last(inds1)-s | ||
| rs1, rs2 = first(inds1):ssf, ssf+1:last(inds1) | ||
| tindsd, tindss = tail(indsdest), tail(indssrc) | ||
| _circcopy!(dest, (rdest..., rd1), tindsd, src, (rsrc..., rs2), tindss) | ||
| _circcopy!(dest, (rdest..., rd2), tindsd, src, (rsrc..., rs1), tindss) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could use some comments |
||
| end | ||
|
|
||
| # At least one of indsdest, indssrc are empty (and both should be, since we've checked) | ||
| function _circcopy!(dest, rdest, indsdest, src, rsrc, indssrc) | ||
| copy!(dest, CartesianRange(rdest), src, CartesianRange(rsrc)) | ||
| end | ||
|
|
||
| ### BitArrays | ||
|
|
||
| ## getindex | ||
|
|
||
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
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.
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 don't understand why it would be necessary to make a copy. FFTW doesn't care how Julia indexes the array, as long as the data is strided. It should be possible to make it work with any
DenseArray(i.e. supportingpointerandstrides) without copying.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.
Mathematical consistency. As you know better than almost anyone, here's the formula:
x[n]is to be interpreted with periodic boundary conditions.That summation range actually means something, and 1 is not synonymous with "the first value." If my array is defined over
-1:5, it's equivalent to it being defined over1:7but you need to circularly-permute the values. Nothing else makes any sense.Incidentally, this makes array-padding---extending the indices in either direction---an absolute dream. No more index gymnastics necessary.
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.
BTW, I do think we are a bit confused about
DenseArrayvsStridedArray: ifDenseArraymeans it supportspointerandstrides, what exactly doesStridedArraymean?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.
You're right, I meant
StridedArray.I see your point, you want the indexing to define the "origin" (the phase offset) of the DFT.
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.
Note that you could still do it without making a copy by multiplying the output by an appropriate phase. It's probably more expensive than a copy, though.
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.
In my tests, it was.