Skip to content

Integrals with incorrect dimensions for Unitful input #385

@Ickaser

Description

@Ickaser

Describe the bug 🐞

An interpolator with Unitful input errors when integration is attempted.

Minimal Reproducible Example 👇

using Unitful
using DataInterpolations
A = ConstantInterpolation(rand(5)u"m", (1:5)u"s")
DataInterpolations.integral(A, 4u"s")

Error & Stacktrace ⚠️

ERROR: ArgumentError: zero(Quantity{Float64}) not defined.
Stacktrace:
 [1] zero(x::Type{Quantity{Float64}})
   @ Unitful C:\Users\iwheeler\.julia\packages\Unitful\nwwOk\src\quantities.jl:370
 [2] integral(A::ConstantInterpolation{…}, t1::Quantity{…}, t2::Quantity{…})
   @ DataInterpolations C:\Users\iwheeler\.julia\packages\DataInterpolations\zmpkg\src\integrals.jl:21
 [3] integral(A::ConstantInterpolation{…}, t::Quantity{…})
   @ DataInterpolations C:\Users\iwheeler\.julia\packages\DataInterpolations\zmpkg\src\integrals.jl:2
 [4] top-level scope
   @ REPL[19]:1
Some type information was truncated. Use `show(err)` to see complete types.

Environment (please complete the following information):

Occurs in DataInterpolations 7.1.0.

Suggested fix

function cumulative_integral(A, cache_parameters)
if cache_parameters && hasmethod(_integral, Tuple{typeof(A), Number, Number, Number})
integral_values = _integral.(
Ref(A), 1:(length(A.t) - 1), A.t[1:(end - 1)], A.t[2:end])
cumsum(integral_values)
else
promote_type(eltype(A.u), eltype(A.t))[]
end
end

promote_type(typeof(0.5u"m"), typeof(1u"s"))

yields a generic Quantity{Float64} without any units, since there is no way to decide what units the result type should have. A reasonable alternative to using promote_type might be

typeof(zero(eltype(A.u) * zero(eltype(A.t)))

which returns a concrete type with appropriate units for integration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions