Skip to content

Commit 7a27553

Browse files
authored
zero with element instead of eltype in pad (#176)
* zero with element instead of eltype in pad * add tests * call inplace variant in pad * use loop instead of broadcast in inplace pad * bugfix in test
1 parent b6f97f2 commit 7a27553

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.6.16"
3+
version = "0.6.17"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/LinearAlgebra/helper.jl

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,31 +216,23 @@ function unsafe_resize!(W::AbstractMatrix,n::Integer,m::Integer)
216216
end
217217

218218

219-
function pad!(f::AbstractVector{T},n::Integer) where T
220-
if n > length(f)
221-
append!(f,zeros(T,n - length(f)))
222-
else
223-
resize!(f,n)
219+
function pad!(f::AbstractVector, n::Integer)
220+
m = length(f)
221+
resize!(f,n)
222+
if n > m
223+
z = m > 0 ? zero(f[1]) : zero(eltype(f))
224+
for i in m+1:n
225+
f[i] = z
226+
end
224227
end
228+
f
225229
end
226230

227-
228-
function pad(f::AbstractVector{T},n::Integer) where T
229-
if n > length(f)
230-
ret=Vector{T}(undef, n)
231-
ret[1:length(f)]=f
232-
for j=length(f)+1:n
233-
ret[j]=zero(T)
234-
end
235-
ret
236-
else
237-
f[1:n]
238-
end
239-
end
231+
pad(f::AbstractVector, n::Integer) = pad!(Vector(f), n)
240232

241233
function pad(f::AbstractVector{Any},n::Integer)
242234
if n > length(f)
243-
Any[f...,zeros(n - length(f))...]
235+
Any[f; zeros(n - length(f))]
244236
else
245237
f[1:n]
246238
end

test/runtests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ApproxFunBase, LinearAlgebra, Random, Test
22
import ApproxFunBase:
3+
using ApproxFunOrthogonalPolynomials
34

45
@testset "Helper" begin
56
@testset "interlace" begin
@@ -42,6 +43,27 @@ import ApproxFunBase: ∞
4243
# check that unknown types don't lead to a stack overflow
4344
@test_throws MethodError ApproxFunBase.dot(DotTester())
4445
end
46+
@testset "pad" begin
47+
@testset "float" begin
48+
@testset for T in [Float64, Any]
49+
a = T[1,2,3]
50+
b = @inferred pad(a, 4)
51+
@test length(b) == 4
52+
@test @view(b[1:3]) == a
53+
@test b[end] == 0
54+
@test pad(a, 2) == @view(a[1:2])
55+
end
56+
end
57+
@testset "Fun" begin
58+
f = Fun()
59+
zf = zero(f)
60+
@test (@inferred pad([f], 3)) == [f, zf, zf]
61+
@test (@inferred pad([f, zf], 1)) == [f]
62+
v = [f, zf]
63+
@test @inferred pad!(v, 1) == [f]
64+
@test length(v) == 1
65+
end
66+
end
4567

4668
# TODO: Tensorizer tests
4769
end

0 commit comments

Comments
 (0)