Skip to content

Additional QuantityArray constructions #178

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ 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:

```@repl quantity-array
using DynamicQuantities # hide

x = [0.3, 0.4, 0.5]u"km/s"

y = (42:45) * u"kg"
```

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

```@repl quantity-array
z = [0.3, 0.4, 0.5] .* u"km/s"
```

### Unitful

DynamicQuantities allows you to convert back and forth from Unitful.jl:
Expand Down
5 changes: 5 additions & 0 deletions src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
function Base.:/(l::AbstractDimensions, r::$type)
new_quantity(typeof(r), inv(ustrip(r)), l / dimension(r))
end

# Defining here instead of outside loop with UnionAbstractQuantity to avoid method ambiguities
Base.:*(A::AbstractArray, q::$type) = QuantityArray(A, q)
Base.:*(q::$type, A::AbstractArray) = A * q
Base.:/(A::AbstractArray, q::$type) = A * inv(q)
end
end

Expand Down
23 changes: 13 additions & 10 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 @@ -395,17 +396,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 +417,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,30 +430,30 @@ 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"
@test length(x) == 4

# With RealQuantity:
@test_skip (1.0:4.0) * RealQuantity(u"inch") isa StepRangeLen{<:RealQuantity{Float64,<:SymbolicDimensions}}
@test_skip (1.0:4.0) * RealQuantity(u"inch") isa T_QA_s_StepRangeLen
# TODO: This is not available as TwicePrecision interacts with Real in a way
# that demands many other functions to be defined.
end
Expand Down