Skip to content

Commit 16defb0

Browse files
committed
fix: promotion of two-arg ustrip in arrays
1 parent 3064294 commit 16defb0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/arrays.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ end
108108
)
109109
end
110110

111+
@inline function promote_except_value(q1::Q1, q2::QA2) where {
112+
T1,D1,T2,D2,Q1<:UnionAbstractQuantity{T1,D1},Q2,
113+
QA2<:QuantityArray{T2,N2,D2,Q2} where N2,
114+
}
115+
if D1 == D2 && constructorof(Q1) == constructorof(Q2)
116+
return (q1, q2)
117+
end
118+
Q = promote_type(Q1, Q2)
119+
D = promote_type(D1, D2)
120+
121+
# Create quantity with the properly promoted dimension type
122+
q1_converted = convert(with_type_parameters(Q, T1, D), q1)
123+
# Create a promoted QuantityArray
124+
d2 = convert(with_type_parameters(Q, T2, D), new_quantity(Q, one(T2), dimension(q2)))
125+
q2_converted = QuantityArray(ustrip(q2), d2)
126+
127+
return (q1_converted, q2_converted)
128+
end
129+
130+
@inline function promote_except_value(q1::QA1, q2::Q2) where {
131+
T1,D1,T2,D2,Q1,Q2<:UnionAbstractQuantity{T2,D2},
132+
QA1<:QuantityArray{T1,N1,D1,Q1} where N1,
133+
}
134+
q2_converted, q1_converted = promote_except_value(q2, q1)
135+
return (q1_converted, q2_converted)
136+
end
137+
111138
function Base.convert(::Type{QA}, A::QA) where {QA<:QuantityArray}
112139
return A
113140
end
@@ -126,6 +153,7 @@ end
126153
@inline ustrip(A::QuantityArray) = A.value
127154
@inline ustrip(A::AbstractArray{<:UnionAbstractQuantity}) = ustrip.(A)
128155
@inline function ustrip(unit::UnionAbstractQuantity, q::QuantityArray)
156+
unit, q = promote_except_value(unit, q)
129157
dimension(unit) == dimension(q) || throw(DimensionError(unit, q))
130158
conversion_factor = ustrip(dimension(q) / unit)
131159
return ustrip(q) .* conversion_factor

0 commit comments

Comments
 (0)