Skip to content

Ensure user-defined mutable version of dimensions always copied #45

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ end
function Base.getindex(A::QuantityArray, i...)
output_value = getindex(ustrip(A), i...)
if isa(output_value, AbstractArray)
return QuantityArray(output_value, dimension(A), quantity_type(A))
return QuantityArray(output_value, copy(dimension(A)), quantity_type(A))
else
return new_quantity(quantity_type(A), output_value, dimension(A))
return new_quantity(quantity_type(A), output_value, copy(dimension(A)))
end
end
function Base.setindex!(A::QuantityArray{T,N,D,Q}, v::Q, i...) where {T,N,D,Q<:AbstractQuantity}
Expand All @@ -122,14 +122,14 @@ unsafe_setindex!(A, v, i...) = setindex!(ustrip(A), ustrip(v), i...)
Base.IndexStyle(::Type{Q}) where {Q<:QuantityArray} = IndexStyle(array_type(Q))


Base.similar(A::QuantityArray) = QuantityArray(similar(ustrip(A)), dimension(A), quantity_type(A))
Base.similar(A::QuantityArray, ::Type{S}) where {S} = QuantityArray(similar(ustrip(A), S), dimension(A), quantity_type(A))
Base.similar(A::QuantityArray) = QuantityArray(similar(ustrip(A)), copy(dimension(A)), quantity_type(A))
Base.similar(A::QuantityArray, ::Type{S}) where {S} = QuantityArray(similar(ustrip(A), S), copy(dimension(A)), quantity_type(A))

# Unfortunately this mess of `similar` is required to avoid ambiguous methods.
# c.f. base/abstractarray.jl
for dim_type in (:(Dims), :(Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}}), :(Tuple{Integer, Vararg{Integer}}))
@eval Base.similar(A::QuantityArray, dims::$dim_type) = QuantityArray(similar(ustrip(A), dims), dimension(A), quantity_type(A))
@eval Base.similar(A::QuantityArray, ::Type{S}, dims::$dim_type) where {S} = QuantityArray(similar(ustrip(A), S, dims), dimension(A), quantity_type(A))
@eval Base.similar(A::QuantityArray, dims::$dim_type) = QuantityArray(similar(ustrip(A), dims), copy(dimension(A)), quantity_type(A))
@eval Base.similar(A::QuantityArray, ::Type{S}, dims::$dim_type) where {S} = QuantityArray(similar(ustrip(A), S, dims), copy(dimension(A)), quantity_type(A))
end

Base.BroadcastStyle(::Type{QA}) where {QA<:QuantityArray} = Broadcast.ArrayStyle{QA}()
Expand All @@ -138,7 +138,7 @@ function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{QA}}, ::Typ
T = value_type(ElType)
output_array = similar(bc, T)
first_output::ElType = materialize_first(bc)
return QuantityArray(output_array, dimension(first_output)::dim_type(ElType), ElType)
return QuantityArray(output_array, copy(dimension(first_output))::dim_type(ElType), ElType)
end
function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{QuantityArray{T,N,D,Q,V}}}, ::Type{ElType}) where {T,N,D,Q,V<:Array{T,N},ElType}
return similar(Array{ElType}, axes(bc))
Expand Down Expand Up @@ -182,12 +182,16 @@ Base.showarg(io::IO, v::QuantityArray, _) = _print_array_type(io, typeof(v))
Base.show(io::IO, ::MIME"text/plain", ::Type{QA}) where {QA<:QuantityArray} = _print_array_type(io, QA)

# Other array operations:
Base.resize!(A::QuantityArray, n) = (resize!(ustrip(A), n); A)
Base.empty!(A::QuantityArray) = (empty!(ustrip(A)); A)
Base.empty(A::QuantityArray) = QuantityArray(empty(ustrip(A)), copy(dimension(A)), quantity_type(A))
Base.zero(A::QuantityArray) = QuantityArray(zero(ustrip(A)), copy(dimension(A)), quantity_type(A))
Base.copy(A::QuantityArray) = QuantityArray(copy(ustrip(A)), copy(dimension(A)), quantity_type(A))
for f in (:cat, :hcat, :vcat)
preamble = quote
allequal(dimension.(A)) || throw(DimensionError(A[begin], A[begin+1:end]))
A = promote(A...)
dimensions = dimension(A[begin])
dimensions = copy(dimension(A[begin]))
Q = quantity_type(A[begin])
end
if f == :cat
Expand All @@ -202,8 +206,8 @@ for f in (:cat, :hcat, :vcat)
end
end
end
Base.fill(x::AbstractQuantity, dims::Dims...) = QuantityArray(fill(ustrip(x), dims...), dimension(x), typeof(x))
Base.fill(x::AbstractQuantity, t::Tuple{}) = QuantityArray(fill(ustrip(x), t), dimension(x), typeof(x))
Base.fill(x::AbstractQuantity, dims::Dims...) = QuantityArray(fill(ustrip(x), dims...), copy(dimension(x)), typeof(x))
Base.fill(x::AbstractQuantity, t::Tuple{}) = QuantityArray(fill(ustrip(x), t), copy(dimension(x)), typeof(x))

ulength(q::QuantityArray) = ulength(dimension(q))
umass(q::QuantityArray) = umass(dimension(q))
Expand Down
16 changes: 8 additions & 8 deletions src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ Base.:*(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(+, l, r)
Base.:*(l::AbstractQuantity, r::AbstractQuantity) = new_quantity(typeof(l), ustrip(l) * ustrip(r), dimension(l) * dimension(r))
Base.:*(l::AbstractQuantity, r::AbstractDimensions) = new_quantity(typeof(l), ustrip(l), dimension(l) * r)
Base.:*(l::AbstractDimensions, r::AbstractQuantity) = new_quantity(typeof(r), ustrip(r), l * dimension(r))
Base.:*(l::AbstractQuantity, r) = new_quantity(typeof(l), ustrip(l) * r, dimension(l))
Base.:*(l, r::AbstractQuantity) = new_quantity(typeof(r), l * ustrip(r), dimension(r))
Base.:*(l::AbstractQuantity, r) = new_quantity(typeof(l), ustrip(l) * r, copy(dimension(l)))
Base.:*(l, r::AbstractQuantity) = new_quantity(typeof(r), l * ustrip(r), copy(dimension(r)))
Base.:*(l::AbstractDimensions, r) = error("Please use an `AbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
Base.:*(l, r::AbstractDimensions) = error("Please use an `AbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")

Base.:/(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(-, l, r)
Base.:/(l::AbstractQuantity, r::AbstractQuantity) = new_quantity(typeof(l), ustrip(l) / ustrip(r), dimension(l) / dimension(r))
Base.:/(l::AbstractQuantity, r::AbstractDimensions) = new_quantity(typeof(l), ustrip(l), dimension(l) / r)
Base.:/(l::AbstractDimensions, r::AbstractQuantity) = new_quantity(typeof(r), inv(ustrip(r)), l / dimension(r))
Base.:/(l::AbstractQuantity, r) = new_quantity(typeof(l), ustrip(l) / r, dimension(l))
Base.:/(l::AbstractQuantity, r) = new_quantity(typeof(l), ustrip(l) / r, copy(dimension(l)))
Base.:/(l, r::AbstractQuantity) = l * inv(r)
Base.:/(l::AbstractDimensions, r) = error("Please use an `AbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
Base.:/(l, r::AbstractDimensions) = error("Please use an `AbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")

Base.:+(l::AbstractQuantity, r::AbstractQuantity) =
let
dimension(l) == dimension(r) || throw(DimensionError(l, r))
new_quantity(typeof(l), ustrip(l) + ustrip(r), dimension(l))
new_quantity(typeof(l), ustrip(l) + ustrip(r), copy(dimension(l)))
end
Base.:-(l::AbstractQuantity) = new_quantity(typeof(l), -ustrip(l), dimension(l))
Base.:-(l::AbstractQuantity) = new_quantity(typeof(l), -ustrip(l), copy(dimension(l)))
Base.:-(l::AbstractQuantity, r::AbstractQuantity) = l + (-r)

Base.:+(l::AbstractQuantity, r) =
let
iszero(dimension(l)) || throw(DimensionError(l, r))
new_quantity(typeof(l), ustrip(l) + r, dimension(l))
new_quantity(typeof(l), ustrip(l) + r, copy(dimension(l)))
end
Base.:+(l, r::AbstractQuantity) =
let
iszero(dimension(r)) || throw(DimensionError(l, r))
new_quantity(typeof(r), l + ustrip(r), dimension(r))
new_quantity(typeof(r), l + ustrip(r), copy(dimension(r)))
end
Base.:-(l::AbstractQuantity, r) = l + (-r)
Base.:-(l, r::AbstractQuantity) = l + (-r)
Expand Down Expand Up @@ -77,6 +77,6 @@ Base.sqrt(q::AbstractQuantity) = new_quantity(typeof(q), sqrt(ustrip(q)), sqrt(d
Base.cbrt(d::AbstractDimensions{R}) where {R} = d^inv(convert(R, 3))
Base.cbrt(q::AbstractQuantity) = new_quantity(typeof(q), cbrt(ustrip(q)), cbrt(dimension(q)))

Base.abs(q::AbstractQuantity) = new_quantity(typeof(q), abs(ustrip(q)), dimension(q))
Base.abs(q::AbstractQuantity) = new_quantity(typeof(q), abs(ustrip(q)), copy(dimension(q)))
Base.abs2(q::AbstractQuantity) = new_quantity(typeof(q), abs2(ustrip(q)), dimension(q)^2)
Base.angle(q::AbstractQuantity{T}) where {T<:Complex} = angle(ustrip(q))
25 changes: 13 additions & 12 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
return output
end

Base.float(q::AbstractQuantity) = new_quantity(typeof(q), float(ustrip(q)), dimension(q))
Base.float(q::AbstractQuantity) = new_quantity(typeof(q), float(ustrip(q)), copy(dimension(q)))
Base.convert(::Type{T}, q::AbstractQuantity) where {T<:Real} =
let
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
Expand All @@ -45,12 +45,12 @@ Base.axes(q::AbstractQuantity) = axes(ustrip(q))
Base.iterate(qd::AbstractQuantity, maybe_state...) =
let subiterate=iterate(ustrip(qd), maybe_state...)
subiterate === nothing && return nothing
return new_quantity(typeof(qd), subiterate[1], dimension(qd)), subiterate[2]
return new_quantity(typeof(qd), subiterate[1], copy(dimension(qd))), subiterate[2]
end
Base.ndims(::Type{<:AbstractQuantity{T}}) where {T} = ndims(T)
Base.ndims(q::AbstractQuantity) = ndims(ustrip(q))
Base.broadcastable(q::AbstractQuantity) = new_quantity(typeof(q), Base.broadcastable(ustrip(q)), dimension(q))
Base.getindex(q::AbstractQuantity, i...) = new_quantity(typeof(q), getindex(ustrip(q), i...), dimension(q))
Base.broadcastable(q::AbstractQuantity) = new_quantity(typeof(q), Base.broadcastable(ustrip(q)), copy(dimension(q)))
Base.getindex(q::AbstractQuantity, i...) = new_quantity(typeof(q), getindex(ustrip(q), i...), copy(dimension(q)))
Base.keys(q::AbstractQuantity) = keys(ustrip(q))


Expand Down Expand Up @@ -103,7 +103,7 @@ end

# Simple operations which return a full quantity (same dimensions)
for f in (:real, :imag, :conj, :adjoint, :unsigned, :nextfloat, :prevfloat)
@eval Base.$f(q::AbstractQuantity) = new_quantity(typeof(q), $f(ustrip(q)), dimension(q))
@eval Base.$f(q::AbstractQuantity) = new_quantity(typeof(q), $f(ustrip(q)), copy(dimension(q)))
end

# Base.one, typemin, typemax
Expand All @@ -116,20 +116,20 @@ for f in (:one, :typemin, :typemax)
if f == :one # Return empty dimensions, as should be multiplicative identity.
@eval Base.$f(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, $f(ustrip(q)), one(dimension(q)))
else
@eval Base.$f(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, $f(ustrip(q)), dimension(q))
@eval Base.$f(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, $f(ustrip(q)), copy(dimension(q)))
end
end
Base.one(::Type{D}) where {D<:AbstractDimensions} = D()
Base.one(::D) where {D<:AbstractDimensions} = one(D)

# Additive identities (zero)
Base.zero(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, zero(ustrip(q)), dimension(q))
Base.zero(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, zero(ustrip(q)), copy(dimension(q)))
Base.zero(::AbstractDimensions) = error("There is no such thing as an additive identity for a `AbstractDimensions` object, as + is only defined for `AbstractQuantity`.")
Base.zero(::Type{<:AbstractQuantity}) = error("Cannot create an additive identity for a `AbstractQuantity` type, as the dimensions are unknown. Please use `zero(::AbstractQuantity)` instead.")
Base.zero(::Type{<:AbstractDimensions}) = error("There is no such thing as an additive identity for a `AbstractDimensions` type, as + is only defined for `AbstractQuantity`.")

# Dimensionful 1 (oneunit)
Base.oneunit(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, oneunit(ustrip(q)), dimension(q))
Base.oneunit(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, oneunit(ustrip(q)), copy(dimension(q)))
Base.oneunit(::AbstractDimensions) = error("There is no such thing as a dimensionful 1 for a `AbstractDimensions` object, as + is only defined for `AbstractQuantity`.")
Base.oneunit(::Type{<:AbstractQuantity}) = error("Cannot create a dimensionful 1 for a `AbstractQuantity` type without knowing the dimensions. Please use `oneunit(::AbstractQuantity)` instead.")
Base.oneunit(::Type{<:AbstractDimensions}) = error("There is no such thing as a dimensionful 1 for a `AbstractDimensions` type, as + is only defined for `AbstractQuantity`.")
Expand Down Expand Up @@ -177,14 +177,15 @@ tryrationalize(::Type{R}, x) where {R} = isinteger(x) ? convert(R, round(Int, x)
Base.showerror(io::IO, e::DimensionError) = print(io, "DimensionError: ", e.q1, " and ", e.q2, " have incompatible dimensions")

Base.convert(::Type{Q}, q::AbstractQuantity) where {Q<:AbstractQuantity} = q
Base.convert(::Type{Q}, q::AbstractQuantity) where {T,Q<:AbstractQuantity{T}} = new_quantity(Q, convert(T, ustrip(q)), dimension(q))
Base.convert(::Type{Q}, q::AbstractQuantity) where {T,D,Q<:AbstractQuantity{T,D}} = new_quantity(Q, convert(T, ustrip(q)), convert(D, dimension(q)))
Base.convert(::Type{Q}, q::AbstractQuantity) where {T,Q<:AbstractQuantity{T}} = new_quantity(Q, convert(T, ustrip(q)), copy(dimension(q)))
Base.convert(::Type{Q}, q::AbstractQuantity) where {T,D,Q<:AbstractQuantity{T,D}} = new_quantity(Q, convert(T, ustrip(q)), convert(D, copy(dimension(q))))

Base.convert(::Type{D}, d::AbstractDimensions) where {D<:AbstractDimensions} = d
Base.convert(::Type{D}, d::AbstractDimensions) where {R,D<:AbstractDimensions{R}} = D(d)

Base.copy(d::D) where {D<:AbstractDimensions} = map_dimensions(copy, d)
Base.copy(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, copy(ustrip(q)), copy(dimension(q)))
@inline Base.copy(d::D) where {D<:AbstractDimensions} = map_dimensions(copy, d)
@inline Base.copy(d::D) where {D<:Dimensions} = d
@inline Base.copy(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, copy(ustrip(q)), copy(dimension(q)))

"""
ustrip(q::AbstractQuantity)
Expand Down
24 changes: 24 additions & 0 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,30 @@ end
@test x != xc
end

@testset "Array resizing" begin
qa = QuantityArray([1., 2., 3.], u"m/s")

@test size(qa) == (3,)
@test size(empty(qa)) == (0,)
@test typeof(empty(qa)) == typeof(qa)

push!(qa, 4.0u"m/s")

@test qa[4] == 4.0u"m/s"

@test size(zero(qa)) == (4,)
@test dimension(zero(qa)) == dimension(qa)
@test dimension(empty(qa)) == dimension(qa)

empty!(qa)

@test size(qa) == (0,)

resize!(qa, 3)

@test size(qa) == (3,)
end

@testset "Utilities" begin
@test fill(u"m/s", 10) == QuantityArray(fill(1.0, 10) .* u"m/s")
@test ndims(fill(u"m/s", ())) == 0
Expand Down