Skip to content
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,33 @@ julia> @btime $f.(qa) setup=(xa = randn(100000) .* u"km/s"; qa = QuantityArray(x

So we can see the `QuantityArray` version saves on both time and memory.

By default, DynamicQuantities will create a `QuantityArray` from an `AbstractArray`, similarly to how a `Quantity` is created from a scalar in the [Usage](@ref) examples:

```julia
julia> x = [0.3, 0.4, 0.5]u"km/s"
3-element QuantityArray(::Vector{Float64}, ::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}):
300.0 m s⁻¹
400.0 m s⁻¹
500.0 m s⁻¹

julia> y = (42:45) * u"kg"
4-element QuantityArray(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, ::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}):
42.0 kg
43.0 kg
44.0 kg
45.0 kg
```

This can be overridden to produce a vector of `Quantity`s by explicitly broadcasting the unit:

```julia
julia> z = [0.3, 0.4, 0.5] .* u"km/s"
3-element Vector{Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}}:
300.0 m s⁻¹
400.0 m s⁻¹
500.0 m s⁻¹
```

### Unitful

DynamicQuantities allows you to convert back and forth from Unitful.jl:
Expand Down
3 changes: 3 additions & 0 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ the same dimension) by passing an array and a single quantity:

```julia
x = QuantityArray(randn(32), u"km/s")
# or x = randn(32)u"km/s"
```

or, by passing an array of individual quantities:
Expand Down Expand Up @@ -273,6 +274,8 @@ f_square(v) = v^2 * 1.5 - v^2
println("Applying function to y_q: ", sum(f_square.(y_q)))
```

See [Home > Arrays](index.md#Arrays) for more.

### Fill

We can also make `QuantityArray` using `fill`:
Expand Down
26 changes: 25 additions & 1 deletion ext/DynamicQuantitiesLinearAlgebraExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module DynamicQuantitiesLinearAlgebraExt

using LinearAlgebra: LinearAlgebra as LA
using DynamicQuantities
using DynamicQuantities: DynamicQuantities as DQ, quantity_type, new_quantity, DimensionError
using DynamicQuantities: DynamicQuantities as DQ, quantity_type, new_quantity, DimensionError, ABSTRACT_QUANTITY_TYPES
using TestItems: @testitem

DQ.is_ext_loaded(::Val{:LinearAlgebra}) = true
Expand Down Expand Up @@ -35,6 +35,30 @@ for op in (:(Base.:*), :(Base.:/), :(Base.:\)),
@eval $op(l::$L, r::$R) = DQ.array_op($op, l, r)
end

for ARRAY_TYPE in (
LA.Bidiagonal,
LA.Diagonal,
LA.Hermitian,
LA.LowerTriangular,
LA.LowerTriangular{<:Any, <:Union{LA.Adjoint{<:Any, <:StridedMatrix{T}}, LA.Transpose{<:Any, <:StridedMatrix{T}}, StridedArray{T, 2}} where T},
LA.Symmetric,
LA.SymTridiagonal,
LA.Tridiagonal,
LA.UnitLowerTriangular,
LA.UnitUpperTriangular,
LA.UpperTriangular,
LA.UpperTriangular{<:Any, <:Union{LA.Adjoint{<:Any, <:StridedMatrix{T}}, LA.Transpose{<:Any, <:StridedMatrix{T}}, StridedArray{T, 2}} where T},
LA.UpperHessenberg,
),
(type, base_type, ) in ABSTRACT_QUANTITY_TYPES

@eval begin
Base.:*(A::$ARRAY_TYPE, q::$type) = QuantityArray(A, q)
Base.:*(q::$type, A::$ARRAY_TYPE) = QuantityArray(A, q)
Base.:/(A::$ARRAY_TYPE, q::$type) = A * inv(q)
end
end

function Base.:*(
l::LA.Transpose{Q,<:AbstractVector},
r::DQ.QuantityArray{T2,1,D,Q,<:AbstractVector{T2}}
Expand Down
14 changes: 14 additions & 0 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ and so can be used in most places where a normal array would be used, including

# Constructors

The most convenient way to create a `QuantityArray` is by multiplying your array-like object by the desired units, e.g.,

```julia
x = [3, 4, 5]u"km/s"
```

For more control, the following constructors are available:

- `QuantityArray(v::AbstractArray, d::AbstractDimensions)`: Create a `QuantityArray` with value `v` and dimensions `d`,
using `Quantity` if the eltype of `v` is numeric, and `GenericQuantity` otherwise.

Expand Down Expand Up @@ -71,6 +79,12 @@ for (type, base_type, default_type) in ABSTRACT_QUANTITY_TYPES
if type in (AbstractQuantity, AbstractGenericQuantity)
@eval QuantityArray(v::AbstractArray{<:$base_type}, d::AbstractDimensions) = QuantityArray(v, d, $default_type)
end

@eval begin
Base.:*(A::AbstractArray{T}, q::$type) where {T<:Number} = QuantityArray(A, q)
Base.:*(q::$type, A::AbstractArray{T}) where {T<:Number} = A * q
Base.:/(A::AbstractArray{T}, q::$type) where {T<:Number} = A * inv(q)
end
end
QuantityArray(v::QA) where {Q<:UnionAbstractQuantity,QA<:AbstractArray{Q}} =
let
Expand Down
6 changes: 6 additions & 0 deletions src/disambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ for (type, _, _) in ABSTRACT_QUANTITY_TYPES, numeric_type in (Bool, BigFloat)
end
end
end
# Cover method ambiguities from, e.g., op(::Array, ::Quantity)::QuantityArray`
Base.:*(A::StepRangeLen{<:Real, <:Base.TwicePrecision}, q::AbstractRealQuantity) = QuantityArray(A, q)
Base.:*(q::AbstractRealQuantity, A::StepRangeLen{<:Real, <:Base.TwicePrecision}) = A * q
Base.:/(A::BitArray, q::AbstractRealQuantity) = A * inv(q)
Base.:/(A::BitArray, q::AbstractQuantity) = A * inv(q)
Base.:/(A::StepRangeLen{<:Real, <:Base.TwicePrecision}, q::AbstractRealQuantity) = A * inv(q)
36 changes: 27 additions & 9 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ end
end

@testset "Arrays" begin
T_QA_GenericQuantity(T, N) = QuantityArray{T, N, D, Q, V} where {T, D<:Dimensions, Q<:UnionAbstractQuantity, V<:AbstractArray{T, N}}
for T in [Float16, Float32, Float64], R in [Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
D = Dimensions{R}

Expand All @@ -288,9 +289,9 @@ end
@test ustrip(x + ones(T, 32))[32] == 2
@test typeof(x + ones(T, 32)) <: GenericQuantity{Vector{T}}
@test typeof(x - ones(T, 32)) <: GenericQuantity{Vector{T}}
@test typeof(ones(T, 32) * GenericQuantity(T(1), D, length=1)) <: GenericQuantity{Vector{T}}
@test typeof(ones(T, 32) / GenericQuantity(T(1), D, length=1)) <: GenericQuantity{Vector{T}}
@test ones(T, 32) / GenericQuantity(T(1), length=1) == GenericQuantity(ones(T, 32), length=-1)
@test typeof(ones(T, 32) * GenericQuantity(T(1), D, length=1)) <: T_QA_GenericQuantity(T, 1)
@test typeof(ones(T, 32) / GenericQuantity(T(1), D, length=1)) <: T_QA_GenericQuantity(T, 1)
@test ones(T, 32) / GenericQuantity(T(1), length=1) == QuantityArray(ones(T, 32), GenericQuantity(T(1), length=-1))
end

@testset "isapprox" begin
Expand Down Expand Up @@ -351,6 +352,14 @@ end
@test norm(GenericQuantity(ustrip.(x), length=1, time=-1), 2) ≈ norm(ustrip.(x), 2) * u"m/s"

@test ustrip(x') == ustrip(x)'

# With BitArray and RealQuantity:
T_QA_AbstractArray = QuantityArray{T, 2, D, Q, V} where {T, D<:Dimensions, Q<:UnionAbstractQuantity, V<:AbstractArray}
T_QA_s_AbstractArray = QuantityArray{T, 2, D, Q, V} where {T, D<:SymbolicDimensions, Q<:UnionAbstractQuantity, V<:AbstractArray}

@test BitArray([1 0 1 0]) / u"m" isa T_QA_AbstractArray
@test BitArray([1 0 1 0]) / us"m" isa T_QA_s_AbstractArray
@test BitArray([1 0 1 0]) / RealQuantity(u"m") isa T_QA_AbstractArray
end

@testset "Ranges" begin
Expand Down Expand Up @@ -395,17 +404,19 @@ end
end

@testset "Multiplying ranges with units" begin
T_QA_StepRangeLen = QuantityArray{T, 1, D, Q, V} where {T, D<:Dimensions, Q<:UnionAbstractQuantity, V<:StepRangeLen}
T_QA_s_StepRangeLen = QuantityArray{T, 1, D, Q, V} where {T, D<:SymbolicDimensions, Q<:UnionAbstractQuantity, V<:StepRangeLen}
# Test multiplying ranges with units
x = (1:0.25:4)u"inch"
@test x isa StepRangeLen
@test x isa T_QA_StepRangeLen
@test first(x) == 1u"inch"
@test x[2] == 1.25u"inch"
@test last(x) == 4u"inch"
@test length(x) == 13

# Integer range (but real-valued unit)
x = (1:4)u"inch"
@test x isa StepRangeLen
@test x isa T_QA_StepRangeLen
@test first(x) == 1u"inch"
@test x[2] == 2u"inch"
@test last(x) == 4u"inch"
Expand All @@ -414,7 +425,7 @@ end

# Test with floating point range
x = (1.0:0.5:3.0)u"m"
@test x isa StepRangeLen
@test x isa T_QA_StepRangeLen
@test first(x) == 1.0u"m"
@test x[2] == 1.5u"m"
@test last(x) == 3.0u"m"
Expand All @@ -427,23 +438,23 @@ end

# Test with symbolic units
x = (1:0.25:4)us"inch"
@test x isa StepRangeLen{<:Quantity{Float64,<:SymbolicDimensions}}
@test x isa T_QA_s_StepRangeLen
@test first(x) == us"inch"
@test x[2] == 1.25us"inch"
@test last(x) == 4us"inch"
@test length(x) == 13

# Test that symbolic units preserve their symbolic nature
x = (0:0.1:1)us"km/h"
@test x isa AbstractRange
@test x isa QuantityArray
@test first(x) == 0us"km/h"
@test x[2] == 0.1us"km/h"
@test last(x) == 1us"km/h"
@test length(x) == 11

# Similarly, integers should stay integers:
x = (1:4)us"inch"
@test_skip x isa StepRangeLen{<:Quantity{Int64,<:SymbolicDimensions}}
@test_skip x isa T_QA_s_StepRangeLen
@test first(x) == us"inch"
@test x[2] == 2us"inch"
@test last(x) == 4us"inch"
Expand All @@ -453,6 +464,13 @@ end
@test_skip (1.0:4.0) * RealQuantity(u"inch") isa StepRangeLen{<:RealQuantity{Float64,<:SymbolicDimensions}}
# TODO: This is not available as TwicePrecision interacts with Real in a way
# that demands many other functions to be defined.
@test (1.0:4.0) / RealQuantity(u"inch") isa T_QA_StepRangeLen
@test (1.0:4.0) * RealQuantity(u"inch") isa T_QA_StepRangeLen
@test RealQuantity(u"inch") * (1.0:4.0) isa T_QA_StepRangeLen

@test (1.0:4.0) / RealQuantity(us"inch") isa T_QA_s_StepRangeLen
@test (1.0:4.0) * RealQuantity(us"inch") isa T_QA_s_StepRangeLen
@test RealQuantity(us"inch") * (1.0:4.0) isa T_QA_s_StepRangeLen
end
end

Expand Down