Skip to content

Commit 68ce876

Browse files
authored
Merge pull request #44 from jishnub/broadcast
Lazy broadcasting for Frequencies
2 parents 4686dc3 + 5f1cef3 commit 68ce876

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/definitions.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ Base.step(x::Frequencies) = x.multiplier
415415

416416
Base.copy(x::Frequencies) = x
417417

418+
# Retain the lazy representation upon scalar multiplication
419+
Broadcast.broadcasted(::typeof(*), f::Frequencies, x::Number) = Frequencies(f.n_nonnegative, f.n, f.multiplier * x)
420+
Broadcast.broadcasted(::typeof(*), x::Number, f::Frequencies) = Broadcast.broadcasted(*, f, x)
421+
Broadcast.broadcasted(::typeof(/), f::Frequencies, x::Number) = Frequencies(f.n_nonnegative, f.n, f.multiplier / x)
422+
Broadcast.broadcasted(::typeof(\), x::Number, f::Frequencies) = Broadcast.broadcasted(/, f, x)
423+
418424
"""
419425
fftfreq(n, fs=1)
420426
Return the discrete Fourier transform (DFT) sample frequencies for a DFT of length `n`. The returned

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ end
101101
@test eltype(fftfreq(5, ComplexF64(2))) == ComplexF64
102102

103103
@test_throws ArgumentError Frequencies(12, 10, 1)
104+
105+
@testset "scaling" begin
106+
@test fftfreq(4, 1) * 2 === fftfreq(4, 2)
107+
@test fftfreq(4, 1) .* 2 === fftfreq(4, 2)
108+
@test 2 * fftfreq(4, 1) === fftfreq(4, 2)
109+
@test 2 .* fftfreq(4, 1) === fftfreq(4, 2)
110+
111+
@test fftfreq(4, 1) / 2 === fftfreq(4, 1/2)
112+
@test fftfreq(4, 1) ./ 2 === fftfreq(4, 1/2)
113+
114+
@test 2 \ fftfreq(4, 1) === fftfreq(4, 1/2)
115+
@test 2 .\ fftfreq(4, 1) === fftfreq(4, 1/2)
116+
end
104117
end
105118

106119
@testset "normalization" begin

0 commit comments

Comments
 (0)