Skip to content

Commit 2924211

Browse files
committed
Fix array conversion for static arrays
1 parent 151d324 commit 2924211

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/arrays.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ function Base.convert(::Type{QA}, A::QA) where {QA<:QuantityArray}
7070
return A
7171
end
7272
function Base.convert(::Type{QA1}, A::QA2) where {QA1<:QuantityArray,QA2<:QuantityArray}
73-
Q = quantity_type(QA1)
7473
V = array_type(QA1)
75-
N = ndims(QA1)
76-
77-
raw_array = Base.Fix1(convert, Q).(A)
78-
output = QuantityArray(convert(constructorof(V){Q,N}, raw_array))
79-
# TODO: This will mess with static arrays
74+
D = dim_type(QA1)
75+
Q = quantity_type(QA1)
8076

81-
return output::QA1
77+
return QuantityArray(
78+
convert(V, ustrip(A)),
79+
convert(D, dimension(A)),
80+
Q,
81+
)::QA1
8282
end
8383

8484
@inline ustrip(A::QuantityArray) = A.value

test/unittests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,4 +1032,15 @@ end
10321032
# TODO: Currently this converts to a `Vector` of `GenericQuantity`
10331033
@test_skip x .* z isa QuantityArray{Float32,1,<:Dimensions,<:GenericQuantity{Float32}}
10341034
end
1035+
1036+
@testset "Array conversion" begin
1037+
x = SArray{Tuple{3}}(randn(3))
1038+
y = SArray{Tuple{3}}(randn(Float32, 3))
1039+
qx = QuantityArray(x, Dimensions(Rational{Int}, length=1))
1040+
qy = QuantityArray(y; length=1)
1041+
1042+
@test typeof(convert(typeof(qx), qy)) == typeof(qx)
1043+
@test convert(typeof(qx), qy)[1] isa Quantity{Float64}
1044+
@test convert(typeof(qx), qy)[1] == convert(Quantity{Float64}, qy[1])
1045+
end
10351046
end

0 commit comments

Comments
 (0)