-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Base.isapprox fails for Vector{Quantity} #111
Comments
Looks like I mis-stated in #76: the method function isapprox(x::AbstractArray, y::AbstractArray;
atol::Real=0,
rtol::Real=Base.rtoldefault(promote_leaf_eltypes(x),promote_leaf_eltypes(y),atol),
nans::Bool=false, norm::Function=norm)
d = norm(x - y)
if isfinite(d)
return d <= max(atol, rtol*max(norm(x), norm(y)))
else
# Fall back to a component-wise approximate comparison
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol, nans=nans), zip(x, y))
end
end For the MWE, the Base.rtoldefault(
Quantity{Float64, Dimensions{DynamicQuantities.FixedRational{Int32, 25200}}},
Quantity{Float64, Dimensions{DynamicQuantities.FixedRational{Int32, 25200}}},
atol
) |
Hm, does it work if you do e.g., RealQuantity(1.0u"m") |
I also realised our current implementation of DynamicQuantities.jl/src/utils.jl Lines 224 to 236 in 9681769
So maybe we want to have custom logic for What is the assumption in Unitful? We can just match the existing API on this sort of thing. |
This is definitely an option, but it’s also pretty natural in practice to write something like |
It looks like they defined a bunch of A call to |
It seems like the most robust way to handle For defining default rtoldefault(::Type{T}) where {T<:AbstractFloat} = sqrt(eps(T))
rtoldefault(::Type{<:Real}) = 0 These should be reasonably simple to reimplement to something like: rtoldefault(x::UnionAbstractQuantity{T,D}) where {T<:AbstractFloat,D} = sqrt(eps(T)) * x.dimensions
rtoldefault(x::UnionAbstractQuantity{T,D}) where {T<:Real,D} = zero(x) |
That sounds good to me!! Or even something like rtoldefault(q::UnionAbstractQuantity) = new_quantity(typeof(q), rtoldefault(ustrip(q)), dimension(q)) to be even more generic, as it would preserve the quantity type and also take the underlying |
In this case I think we can simply add DynamicQuantities.jl/src/math.jl Lines 193 to 194 in 9681769
since it has the same structure |
Sounds good to me. There’s definitely going to be a little more involved since the default |
Alright, I started working more on an implementation for this and realized I needed to do some disambiguation in my head about what exactly For the case of For the case of |
This seems pretty simple when written out, but what tripped me up was trying to think about something like an I think the most practical answer in this kind of situation is to require that the quantity-dimensions of the vector elements be equivalent in order to compute |
I think this is a good idea. You could do something like (allequal(dimension.(u)) && allequal(dimension.(v)) && dimension(first(u)) == dimension(first(v))) ||
throw(DimensionError(u, v)) Basically whatever prevents the user from shooting themselves in the foot here! Actually if you just compute Even if that means basically throwing an error on |
Update: I’m still working on this as time permits. I think I’ve gotten the dimensionful Upon further reflection, I don’t think I’ve got a rough sketch of the vector comparison version written that I suspect will work fine once I’ve gotten the regular version nailed down and any lessons learned incorporated. |
Fixed with merge of PR #114 |
Extending this from a comment in #76, I reduced the issue to a MWE. The issue seems to occur specifically when running
Base.isapprox
on aVector{Quantity}
.The text was updated successfully, but these errors were encountered: