@@ -15,7 +15,7 @@ and so can be used in most places where a normal array would be used.
15
15
# Constructors
16
16
17
17
- `QuantityArray(value::AbstractArray, dimensions::AbstractDimensions)`: Create a `QuantityArray` with value `value` and dimensions `dimensions`.
18
- - `QuantityArray(value::AbstractArray, quantity::Quantity)`: Create a `QuantityArray` with value `value` and dimensions inferred
18
+ - `QuantityArray(value::AbstractArray, quantity::Quantity)`: Create a `QuantityArray` with value `value` and dimensions inferred
19
19
with `dimension(quantity)`. This is so that you can easily create an array with the units module, like so:
20
20
```julia
21
21
julia> A = QuantityArray(randn(32), 1u"m")
@@ -121,22 +121,19 @@ unsafe_setindex!(A, v, i...) = setindex!(ustrip(A), ustrip(v), i...)
121
121
122
122
Base. IndexStyle (:: Type{Q} ) where {Q<: QuantityArray } = IndexStyle (array_type (Q))
123
123
124
- const IntOrOneTo = Union{Integer, Base. OneTo}
125
124
126
- # Unfortunately this mess of `similar` is required to avoid ambiguous methods.
127
- # c.f. base/abstractarray.jl
128
125
Base. similar (A:: QuantityArray ) = QuantityArray (similar (ustrip (A)), dimension (A), quantity_type (A))
129
126
Base. similar (A:: QuantityArray , :: Type{S} ) where {S} = QuantityArray (similar (ustrip (A), S), dimension (A), quantity_type (A))
130
- Base. similar (A:: QuantityArray , dims:: Dims{N} ) where {N} = QuantityArray (similar (ustrip (A), dims), dimension (A), quantity_type (A))
131
- Base. similar (A:: QuantityArray , dims:: Tuple{IntOrOneTo, Vararg{IntOrOneTo}} ) = QuantityArray (similar (ustrip (A), dims), dimension (A), quantity_type (A))
132
- Base. similar (A:: QuantityArray , dims:: Tuple{Integer, Vararg{Integer}} ) = QuantityArray (similar (ustrip (A), dims), dimension (A), quantity_type (A))
133
- Base. similar (A:: QuantityArray , :: Type{S} , dims:: Dims{N} ) where {S,N} = QuantityArray (similar (ustrip (A), S, dims), dimension (A), quantity_type (A))
134
- Base. similar (A:: QuantityArray , :: Type{S} , dims:: Tuple{IntOrOneTo, Vararg{IntOrOneTo}} ) where {S} = QuantityArray (similar (ustrip (A), S, dims), dimension (A), quantity_type (A))
135
- Base. similar (A:: QuantityArray , :: Type{S} , dims:: Tuple{Integer, Vararg{Integer}} ) where {S} = QuantityArray (similar (ustrip (A), S, dims), dimension (A), quantity_type (A))
136
-
137
- function Base. BroadcastStyle (:: Type{QA} ) where {QA<: QuantityArray }
138
- return Broadcast. ArrayStyle {QA} ()
127
+
128
+ # Unfortunately this mess of `similar` is required to avoid ambiguous methods.
129
+ # c.f. base/abstractarray.jl
130
+ for dim_type in (:(Dims), :(Tuple{Union{Integer,Base. OneTo},Vararg{Union{Integer,Base. OneTo}}}), :(Tuple{Integer, Vararg{Integer}}))
131
+ @eval Base. similar (A:: QuantityArray , dims:: $dim_type ) = QuantityArray (similar (ustrip (A), dims), dimension (A), quantity_type (A))
132
+ @eval Base. similar (A:: QuantityArray , :: Type{S} , dims:: $dim_type ) where {S} = QuantityArray (similar (ustrip (A), S, dims), dimension (A), quantity_type (A))
139
133
end
134
+
135
+ Base. BroadcastStyle (:: Type{QA} ) where {QA<: QuantityArray } = Broadcast. ArrayStyle {QA} ()
136
+
140
137
function Base. similar (bc:: Broadcast.Broadcasted{Broadcast.ArrayStyle{QA}} , :: Type{ElType} ) where {QA<: QuantityArray ,ElType<: AbstractQuantity }
141
138
T = value_type (ElType)
142
139
output_array = similar (bc, T)
0 commit comments