Skip to content

Commit 1a870e8

Browse files
Merge pull request #1112 from SciML/dw/totallength
Import `totallength` in ForwardDiff extension
2 parents b5faf40 + 907508a commit 1a870e8

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DiffEqBase"
22
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "6.162.1"
4+
version = "6.162.2"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

ext/DiffEqBaseForwardDiffExt.jl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,19 +501,19 @@ unitfulvalue(x::Type{ForwardDiff.Dual{T, V, N}}) where {T, V, N} = V
501501
unitfulvalue(x::ForwardDiff.Dual) = unitfulvalue(ForwardDiff.unitfulvalue(x))
502502

503503
sse(x::ForwardDiff.Dual) = sse(ForwardDiff.value(x)) + sum(sse, ForwardDiff.partials(x))
504-
function totallength(x::ForwardDiff.Dual)
505-
totallength(ForwardDiff.value(x)) + sum(totallength, ForwardDiff.partials(x))
504+
function DiffEqBase.totallength(x::ForwardDiff.Dual)
505+
return DiffEqBase.totallength(ForwardDiff.value(x)) + sum(DiffEqBase.totallength, ForwardDiff.partials(x))
506506
end
507507

508508
@inline ODE_DEFAULT_NORM(u::ForwardDiff.Dual, ::Any) = sqrt(sse(u))
509509
@inline function ODE_DEFAULT_NORM(u::AbstractArray{<:ForwardDiff.Dual{Tag, T}},
510510
t::Any) where {Tag, T}
511-
sqrt(__sum(sse, u; init = sse(zero(T))) / totallength(u))
511+
sqrt(DiffEqBase.__sum(sse, u; init = sse(zero(T))) / DiffEqBase.totallength(u))
512512
end
513513
@inline ODE_DEFAULT_NORM(u::ForwardDiff.Dual, ::ForwardDiff.Dual) = sqrt(sse(u))
514514
@inline function ODE_DEFAULT_NORM(u::AbstractArray{<:ForwardDiff.Dual{Tag, T}},
515515
::ForwardDiff.Dual) where {Tag, T}
516-
sqrt(__sum(sse, u; init = sse(zero(T))) / totallength(u))
516+
sqrt(DiffEqBase.__sum(sse, u; init = sse(zero(T))) / DiffEqBase.totallength(u))
517517
end
518518

519519
if !hasmethod(nextfloat, Tuple{ForwardDiff.Dual})
@@ -528,13 +528,6 @@ end
528528

529529
# bisection(f, tup::Tuple{T,T}, t_forward::Bool) where {T<:ForwardDiff.Dual} = find_zero(f, tup, Roots.AlefeldPotraShi())
530530

531-
# Static Arrays don't support the `init` keyword argument for `sum`
532-
@inline __sum(f::F, args...; init, kwargs...) where {F} = sum(f, args...; init, kwargs...)
533-
@inline function __sum(
534-
f::F, a::DiffEqBase.StaticArraysCore.StaticArray...; init, kwargs...) where {F}
535-
return mapreduce(f, +, a...; init, kwargs...)
536-
end
537-
538531
# Differentiation of internal solver
539532

540533
function scalar_nlsolve_ad(prob, alg::InternalITP, args...; kwargs...)

ext/DiffEqBaseReverseDiffExt.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ using DiffEqBase
44
import DiffEqBase: value
55
import ReverseDiff
66
import DiffEqBase.ArrayInterface
7-
import DiffEqBase.ForwardDiff
87

98
function DiffEqBase.anyeltypedual(::Type{T},
109
::Type{Val{counter}} = Val{0}) where {counter} where {

src/utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ unitfulvalue(x) = x
44
isdistribution(u0) = false
55
sse(x::Number) = abs2(x)
66

7+
# Static Arrays don't support the `init` keyword argument for `sum`
8+
@inline __sum(f::F, args...; init, kwargs...) where {F} = sum(f, args...; init, kwargs...)
9+
@inline function __sum(
10+
f::F, a::StaticArraysCore.StaticArray...; init, kwargs...) where {F}
11+
return mapreduce(f, +, a...; init, kwargs...)
12+
end
13+
714
totallength(x::Number) = 1
815
totallength(x::AbstractArray) = __sum(totallength, x; init = 0)
916

test/forwarddiff_dual_detection.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,13 @@ end
379379
ReverseDiff.TrackedReal{<:ForwardDiff.Dual}
380380
@test DiffEqBase.promote_u0(NaN, [NaN], 0.0) isa Float64
381381
@test DiffEqBase.promote_u0([1.0], [NaN], 0.0) isa Vector{Float64}
382+
383+
# totallength
384+
val = rand(10)
385+
par = rand(10)
386+
u = Dual.(val, par)
387+
@test DiffEqBase.totallength(val[1]) == 1
388+
@test DiffEqBase.totallength(val) == length(val)
389+
@test DiffEqBase.totallength(par) == length(par)
390+
@test DiffEqBase.totallength(u[1]) == DiffEqBase.totallength(val[1]) + DiffEqBase.totallength(par[1])
391+
@test DiffEqBase.totallength(u) == sum(DiffEqBase.totallength, u)

0 commit comments

Comments
 (0)