Skip to content

Commit bbfb4ba

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

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
@@ -116,12 +116,13 @@ zero_tangent(x::Number) = zero(x)
116116
return :($MutableTangent{$primal}($backing_expr))
117117
end
118118

119-
function zero_tangent(x::Array{P, N}) where {P, N}
120-
(isbitstype(P) || all(i->isassigned(x,i), eachindex(x))) && return map(zero_tangent, x)
121-
119+
function zero_tangent(x::Array{P,N}) where {P,N}
120+
(isbitstype(P) || all(i -> isassigned(x, i), eachindex(x))) &&
121+
return map(zero_tangent, x)
122+
122123
# Now we need to handle nonfully assigned arrays
123124
# see discussion at https://github.com/JuliaDiff/ChainRulesCore.jl/pull/626#discussion_r1345235265
124-
y = Array{guess_zero_tangent_type(P), N}(undef, size(x)...)
125+
y = Array{guess_zero_tangent_type(P),N}(undef, size(x)...)
125126
@inbounds for n in eachindex(y)
126127
if isassigned(x, n)
127128
y[n] = zero_tangent(x[n])
@@ -131,6 +132,8 @@ function zero_tangent(x::Array{P, N}) where {P, N}
131132
end
132133

133134
guess_zero_tangent_type(::Type{T}) where {T<:Number} = T
134-
guess_zero_tangent_type(::Type{<:Array{T,N}}) where {T,N} = Array{guess_zero_tangent_type(T), N}
135+
function guess_zero_tangent_type(::Type{<:Array{T,N}}) where {T,N}
136+
return Array{guess_zero_tangent_type(T),N}
137+
end
135138
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
136-
# TODO: we might be able to do better than this. even without.
139+
# 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
@@ -176,26 +176,24 @@ end
176176

177177
@testset "undef elements" begin
178178
x = Vector{Vector{Float64}}(undef, 3)
179-
x[2] = [1.0,2.0]
179+
x[2] = [1.0, 2.0]
180180
dx = zero_tangent(x)
181181
@test dx isa Vector{Vector{Float64}}
182182
@test length(dx) == 3
183183
@test !isassigned(dx, 1)
184184
@test dx[2] == [0.0, 0.0]
185185
@test !isassigned(dx, 3)
186186

187-
188187
a = Vector{MutDemo}(undef, 3)
189188
a[2] = MutDemo(1.5)
190189
da = zero_tangent(a)
191190
@test !isassigned(da, 1)
192191
@test iszero(da[2])
193192
@test !isassigned(da, 3)
194193

195-
196194
db = zero_tangent(Vector{MutDemo}(undef, 3))
197-
@test all(ii->!isassigned(db,ii), eachindex(db))
198-
@test length(db)==3
195+
@test all(ii -> !isassigned(db, ii), eachindex(db))
196+
@test length(db) == 3
199197
@test db isa Vector
200-
end
198+
end
201199
end

0 commit comments

Comments
 (0)