Skip to content

Commit f7213c8

Browse files
style
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent c810dd8 commit f7213c8

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/tangent_types/abstract_zero.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ zero_tangent(x::Number) = zero(x)
120120
return :($MutableTangent{$primal}($backing_expr))
121121
end
122122

123-
function zero_tangent(x::Array{P, N}) where {P, N}
124-
(isbitstype(P) || all(i->isassigned(x,i), eachindex(x))) && return map(zero_tangent, x)
125-
123+
function zero_tangent(x::Array{P,N}) where {P,N}
124+
(isbitstype(P) || all(i -> isassigned(x, i), eachindex(x))) &&
125+
return map(zero_tangent, x)
126+
126127
# Now we need to handle nonfully assigned arrays
127128
# see discussion at https://github.com/JuliaDiff/ChainRulesCore.jl/pull/626#discussion_r1345235265
128-
y = Array{guess_zero_tangent_type(P), N}(undef, size(x)...)
129+
y = Array{guess_zero_tangent_type(P),N}(undef, size(x)...)
129130
@inbounds for n in eachindex(y)
130131
if isassigned(x, n)
131132
y[n] = zero_tangent(x[n])
@@ -135,6 +136,8 @@ function zero_tangent(x::Array{P, N}) where {P, N}
135136
end
136137

137138
guess_zero_tangent_type(::Type{T}) where {T<:Number} = T
138-
guess_zero_tangent_type(::Type{<:Array{T,N}}) where {T,N} = Array{guess_zero_tangent_type(T), N}
139+
function guess_zero_tangent_type(::Type{<:Array{T,N}}) where {T,N}
140+
return Array{guess_zero_tangent_type(T),N}
141+
end
139142
guess_zero_tangent_type(::Any) = Any # if we had a general way to handle determining tangent type # https://github.com/JuliaDiff/ChainRulesCore.jl/issues/634
140-
# TODO: we might be able to do better than this. even without.
143+
# TODO: we might be able to do better than this. even without.

test/tangent_types/abstract_zero.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,24 @@ end
182182

183183
@testset "undef elements" begin
184184
x = Vector{Vector{Float64}}(undef, 3)
185-
x[2] = [1.0,2.0]
185+
x[2] = [1.0, 2.0]
186186
dx = zero_tangent(x)
187187
@test dx isa Vector{Vector{Float64}}
188188
@test length(dx) == 3
189189
@test !isassigned(dx, 1)
190190
@test dx[2] == [0.0, 0.0]
191191
@test !isassigned(dx, 3)
192192

193-
194193
a = Vector{MutDemo}(undef, 3)
195194
a[2] = MutDemo(1.5)
196195
da = zero_tangent(a)
197196
@test !isassigned(da, 1)
198197
@test iszero(da[2])
199198
@test !isassigned(da, 3)
200199

201-
202200
db = zero_tangent(Vector{MutDemo}(undef, 3))
203-
@test all(ii->!isassigned(db,ii), eachindex(db))
204-
@test length(db)==3
201+
@test all(ii -> !isassigned(db, ii), eachindex(db))
202+
@test length(db) == 3
205203
@test db isa Vector
206-
end
204+
end
207205
end

0 commit comments

Comments
 (0)