Skip to content

Commit f09fc3e

Browse files
added booleans the the two types FourierJoin and FourierSplit
1 parent 000735e commit f09fc3e

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/custom_fourier_types.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
## This View checks for the index to be L1 or the mirrored version (L2)
22
# and then replaces the value by half of the parent at L1
3+
# do_split is a Bool that indicates whether this mechanism is active. It is needed for type stability reasons of functions returnting this type
34
struct FourierSplit{T,N, AA<:AbstractArray{T, N}} <: AbstractArray{T,N}
45
parent::AA # holds the data (or is another view)
56
D::Int # dimension along which to apply to copy
67
L1::Int # low index position to copy from (and half)
78
L2::Int # high index positon to copy to (and half)
9+
do_split::Bool
810

911
# This version below is needed to avoid a split for the firs rft dimension but still return half the value
1012
# FFTs and other RFT dimension should use the version without L2
11-
function FourierSplit(parent::AA, D::Int,L1::Int,L2::Int) where {T,N, AA<:AbstractArray{T, N}}
12-
return new{T,N, AA}(parent, D, L1, L2)
13+
function FourierSplit(parent::AA, D::Int,L1::Int,L2::Int, do_split::Bool) where {T,N, AA<:AbstractArray{T, N}}
14+
return new{T,N, AA}(parent, D, L1, L2, do_split)
1315
end
14-
function FourierSplit(parent::AA, D::Int,L1::Int) where {T,N, AA<:AbstractArray{T, N}}
16+
function FourierSplit(parent::AA, D::Int,L1::Int, do_split::Bool) where {T,N, AA<:AbstractArray{T, N}}
1517
mid = fft_center(size(parent)[D])
1618
L2 = mid + (mid-L1)
17-
return FourierSplit(parent, D,L1,L2)
19+
return FourierSplit(parent, D,L1,L2, do_split)
1820
end
1921
end
2022

@@ -25,7 +27,7 @@ Base.parent(A::FourierSplit) = A.parent
2527
Base.size(A::FourierSplit) = size(parent(A))
2628

2729
@inline function Base.getindex(A::FourierSplit{T,N, <:AbstractArray{T, N}}, i::Vararg{Int,N}) where {T,N}
28-
if i[A.D]==A.L2 || i[A.D]==A.L1 # index along this dimension A.D corrsponds to slice L2
30+
if (i[A.D]==A.L2 || i[A.D]==A.L1) && A.do_split # index along this dimension A.D corrsponds to slice L2
2931
# not that "setindex" in the line below modifies only the index, not the array
3032
@inbounds return parent(A)[Base.setindex(i,A.L1, A.D)...] / 2
3133
else i[A.D]==A.L2
@@ -36,22 +38,24 @@ end
3638

3739
## This View checks for the index to be L1
3840
# and then replaces the value by add the value at the mirrored position L2
41+
# do_join is a Bool that indicates whether this mechanism is active. It is needed for type stability reasons of functions returnting this type
3942
struct FourierJoin{T,N, AA<:AbstractArray{T, N}} <: AbstractArray{T, N}
4043
parent::AA
4144
D::Int # dimension along which to apply to copy
4245
L1::Int # low index position to copy from (and half)
4346
L2::Int # high index positon to copy to (and half)
47+
do_join::Bool
4448

4549
# This version below is needed to avoid a split for the firs rft dimension but still return half the value
4650
# FFTs and other RFT dimension should use the version without L2
47-
function FourierJoin(parent::AA, D::Int, L1::Int, L2::Int) where {T, N, AA<:AbstractArray{T, N}}
48-
return new{T, N, AA}(parent, D, L1, L2)
51+
function FourierJoin(parent::AA, D::Int, L1::Int, L2::Int, do_join::Bool) where {T, N, AA<:AbstractArray{T, N}}
52+
return new{T, N, AA}(parent, D, L1, L2, do_join)
4953
end
5054

51-
function FourierJoin(parent::AA, D::Int,L1::Int) where {T, N, AA<:AbstractArray{T, N}}
55+
function FourierJoin(parent::AA, D::Int,L1::Int, do_join::Bool) where {T, N, AA<:AbstractArray{T, N}}
5256
mid = fft_center(size(parent)[D])
5357
L2 = mid + (mid-L1)
54-
return FourierJoin(parent, D, L1, L2)
58+
return FourierJoin(parent, D, L1, L2, do_join)
5559
end
5660
end
5761
Base.IndexStyle(::Type{FS}) where {FS<:FourierJoin} = IndexStyle(parenttype(FS))
@@ -61,7 +65,7 @@ Base.parent(A::FourierJoin) = A.parent
6165
Base.size(A::FourierJoin) = size(parent(A))
6266

6367
@inline function Base.getindex(A::FourierJoin{T,N, <:AbstractArray{T, N}}, i::Vararg{Int,N}) where {T,N}
64-
if i[A.D]==A.L1
68+
if i[A.D]==A.L1 && A.do_join
6569
@inbounds return (parent(A)[i...] + parent(A)[Base.setindex(i, A.L2, A.D)...])
6670
else
6771
@inbounds return (parent(A)[i...])

src/fourier_resizing.jl

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,9 @@ function ft_fix_before(mat, size_old, size_new; start_dim=1)
142142
for d = start_dim:ndims(mat)
143143
sn = size_new[d]
144144
so = size_old[d]
145-
if sn < so && iseven(sn)
146-
L1 = (size_old[d] -size_new[d] )÷2 +1
147-
mat = FourierJoin(mat, d, L1)
148-
end
145+
do_join = (sn < so && iseven(sn))
146+
L1 = (size_old[d] -size_new[d] )÷2 +1
147+
mat = FourierJoin(mat, d, L1, do_join)
149148
end
150149
return mat
151150
end
@@ -156,32 +155,29 @@ function ft_fix_after(mat,size_old,size_new; start_dim=1)
156155
for d=start_dim:ndims(mat)
157156
sn = size_new[d]
158157
so = size_old[d]
159-
if sn > so && iseven(so)
160-
L1 = (size_new[d]-size_old[d])÷2+1
161-
mat = FourierSplit(mat,d,L1)
162-
end
163158
# if equal do nothing
159+
do_split = (sn > so && iseven(so))
160+
L1 = (size_new[d]-size_old[d])÷2+1
161+
mat = FourierSplit(mat,d,L1, do_split)
164162
end
165163
return mat
166164
end
167165

168166
function rft_fix_first_dim_before(mat,size_old,size_new;dim=1)
169167
sn = size_new[dim] # Note that this dim is the corresponding real-space size
170168
so = size_old[dim] # Note that this dim is the corresponding real-space size
171-
if sn < so && iseven(sn) # result size is even upon cropping
172-
L1 = size_new[dim] ÷ 2 + 1
173-
mat = FourierJoin(mat, dim, L1, L1) # a hack to dublicate the value
174-
end
169+
do_join = (sn < so && iseven(sn)) # result size is even upon cropping
170+
L1 = size_new[dim] ÷ 2 + 1
171+
mat = FourierJoin(mat, dim, L1, L1, do_join) # a hack to dublicate the value
175172
return mat
176173
end
177174

178175
function rft_fix_first_dim_after(mat,size_old,size_new;dim=1)
179176
sn = size_new[dim] # Note that this dim is the corresponding real-space size
180177
so = size_old[dim] # Note that this dim is the corresponding real-space size
181-
if sn > so && iseven(so) # source size is even upon padding
182-
L1 = size_old[dim] ÷ 2 + 1
183-
mat = FourierSplit(mat,dim,L1,-1) # This hack prevents a second position to be affected
184-
end
178+
do_split = (sn > so && iseven(so)) # source size is even upon padding
179+
L1 = size_old[dim] ÷ 2 + 1
180+
mat = FourierSplit(mat,dim,L1,-1, do_split) # This hack prevents a second position to be affected
185181
# if equal do nothing
186182
return mat
187183
end

test/custom_fourier_types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
@testset "Custom Fourier Types" begin
33
N = 5
44
x = randn((N, N))
5-
fs = FourierTools.FourierSplit(x, 2, 2, 4)
5+
fs = FourierTools.FourierSplit(x, 2, 2, 4, true)
66
@test FourierTools.parenttype(fs) == typeof(x)
77

8-
fj = FourierTools.FourierJoin(x, 2, 2, 4)
8+
fj = FourierTools.FourierJoin(x, 2, 2, 4, true)
99

1010
@test FourierTools.parenttype(fj) == typeof(x)
1111
@test FourierTools.parenttype(typeof(fj)) == typeof(x)

0 commit comments

Comments
 (0)