Skip to content

Commit ac55536

Browse files
committed
Update kzeros and kones for SubArray type
1 parent e4f2835 commit ac55536

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/krylov_utils.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,13 @@ end
171171
172172
Create an AbstractVector of storage type `S` of length `n` only composed of zero.
173173
"""
174-
@inline kzeros(S, n) = fill!(S(undef, n), zero(eltype(S)))
175-
174+
@inline kzeros(S, n) = S <: SubArray ? fill!(S.types[1](undef, n), zero(eltype(S))) : fill!(S(undef, n), zero(eltype(S)))
176175
"""
177176
v = kones(S, n)
178177
179178
Create an AbstractVector of storage type `S` of length `n` only composed of one.
180179
"""
181-
@inline kones(S, n) = fill!(S(undef, n), one(eltype(S)))
180+
@inline kones(S, n) = S <: SubArray ? fill!(S.types[1](undef, n), one(eltype(S))) : fill!(similar(S, n), one(eltype(S)))
182181

183182
@inline display(iter, verbose) = (verbose > 0) && (mod(iter, verbose) == 0)
184183

test/test_aux.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@
9595
# test kzeros and kones
9696
Krylov.kzeros(Vector{Float64}, 10) == zeros(10)
9797
Krylov.kones(Vector{Float64}, 10) == ones(10)
98+
99+
a = rand(10)
100+
b = view(a, 1:4)
101+
S = typeof(b)
102+
Krylov.kzeros(S, 10) == zeros(10)
103+
Krylov.kones(S, 10) == ones(10)
98104
end

0 commit comments

Comments
 (0)