Skip to content

register_primitive doesn't work for Uniful Quantity objects wrapping Particles #68

@klaff

Description

@klaff

Consider the functions f1 and f2:

using MonteCarloMeasurements
using Unitful

function f1(Vi)
    if Vi ≤ 0.0
        return 0.0
    elseif Vi ≥ 1.0
        return 1.0
    else
        Vi
    end
end

function f2(Vi)
    if Vi ≤ 0.0u"V"
        return 0.0u"V"
    elseif Vi ≥ 1.0u"V"
        return 1.0u"V"
    else
        return Vi
    end
end

then f1(0.0..1.0) succeeds, but f1(-0.5..1.5) fails. This is understandable as the second distribution spans the discontinuities. If I then register_primitive(f1) and subsequently execute f1(-0.5..1.5), it succeeds and inspection of the result shows it is as I expect.

However, if I try the same thing with f2 (which I intend to be the exact same function except that now I’m using Unitful and everything should be in units compatible with u"V"), f2 fails, regardless of register_primitive(f2). Specifically f2((0.0..1.0)u"V") works, but f2((-0.5..1.5)u"V") always fails.

@simeonschaub diagnosed this "The problem here is that (-0.5..1.5)u"V" is creating an object of type Quantity{Particles}, whereas register_primitive only overloads f2 for Particles, so that second method never gets called."

See https://discourse.julialang.org/t/problem-combining-unitful-and-montecarlomeasurements/35418

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions