Skip to content

Commit e259291

Browse files
committed
Add constants with namespace collisions equal to expanded
1 parent 1a6ff33 commit e259291

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/symbolic_dimensions.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const ALL_MAPPING = NamedTuple([s => i for (i, s) in enumerate(ALL_SYMBOLS)])
2424
"""
2525
SymbolicDimensions{R} <: AbstractDimensions{R}
2626
27-
An `AbstractDimensions` with one dimension for every unit symbol.
27+
An `AbstractDimensions` with one dimension for every unit and constant symbol.
2828
This is to allow for lazily reducing to SI base units, whereas
2929
`Dimensions` is always in SI base units. Furthermore, `SymbolicDimensions`
3030
stores dimensions using a sparse vector for efficiency (since there
@@ -58,6 +58,21 @@ SymbolicDimensions{R}(d::SymbolicDimensions) where {R} = SymbolicDimensions{R}(d
5858
return constructor(data)
5959
end
6060

61+
function Base.convert(::Type{Qout}, q::Quantity{<:Any,<:Dimensions}) where {T,D<:SymbolicDimensions,Qout<:Quantity{T,D}}
62+
output = Qout(
63+
convert(T, ustrip(q)),
64+
D;
65+
m=ulength(q),
66+
kg=umass(q),
67+
s=utime(q),
68+
A=ucurrent(q),
69+
K=utemperature(q),
70+
cd=uluminosity(q),
71+
mol=uamount(q),
72+
)
73+
SA.dropzeros!(data(dimension(output)))
74+
return output
75+
end
6176
function Base.convert(::Type{Q}, q::Quantity{<:Any,<:SymbolicDimensions}) where {T,D<:Dimensions,Q<:Quantity{T,D}}
6277
result = one(Q) * ustrip(q)
6378
d = dimension(q)
@@ -115,6 +130,8 @@ module SymbolicUnitsParse
115130
import ..DEFAULT_VALUE_TYPE
116131
import ..DEFAULT_DIM_BASE_TYPE
117132

133+
import ...Constants as EagerConstants
134+
118135
const CONSTANT_SYMBOLS_EXIST = Ref{Bool}(false)
119136
const CONSTANT_SYMBOLS_LOCK = Threads.SpinLock()
120137
function _generate_unit_symbols()
@@ -123,6 +140,10 @@ module SymbolicUnitsParse
123140
for unit in setdiff(CONSTANT_SYMBOLS, SYMBOL_CONFLICTS)
124141
@eval const $unit = Quantity(DEFAULT_VALUE_TYPE(1.0), SymbolicDimensions{DEFAULT_DIM_BASE_TYPE}; $(unit)=1)
125142
end
143+
# Evaluate conflicting symbols to non-symbolic form:
144+
for unit in SYMBOL_CONFLICTS
145+
@eval const $unit = convert(Quantity{DEFAULT_VALUE_TYPE,SymbolicDimensions}, EagerConstants.$unit)
146+
end
126147
CONSTANT_SYMBOLS_EXIST[] = true
127148
end
128149
return nothing

test/unittests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,21 @@ end
431431
@test dimension(expand_units(inv(us"s") * us"m")) == dimension(expand_units(us"km/s"))
432432

433433
@test_throws ErrorException sym_uparse("'c'")
434+
435+
# For constants which have a namespace collision, the numerical expansion is used:
436+
@test dimension(us"Constants.au")[:au] == 1
437+
@test dimension(us"Constants.h")[:h] == 0
438+
@test dimension(us"h")[:h] == 1
439+
440+
@test us"Constants.h" != us"h"
441+
@test expand_units(us"Constants.h") == u"Constants.h"
442+
443+
# Actually expands to:
444+
@test dimension(us"Constants.h")[:m] == 2
445+
@test dimension(us"Constants.h")[:s] == -1
446+
@test dimension(us"Constants.h")[:kg] == 1
447+
448+
# So the numerical value is different from other constants:
449+
@test ustrip(us"Constants.h") == ustrip(u"Constants.h")
450+
@test ustrip(us"Constants.au") != ustrip(u"Constants.au")
434451
end

0 commit comments

Comments
 (0)