@@ -29,6 +29,10 @@ This is to allow for lazily reducing to SI base units, whereas
2929`Dimensions` is always in SI base units. Furthermore, `SymbolicDimensions`
3030stores dimensions using a sparse vector for efficiency (since there
3131are so many unit symbols).
32+
33+ You can convert a quantity using `SymbolicDimensions` as its dimensions
34+ to one which uses `Dimensions` as its dimensions (i.e., base SI units)
35+ `expand_units`.
3236"""
3337struct SymbolicDimensions{R} <: AbstractDimensions{R}
3438 _data:: SA.SparseVector{R}
@@ -37,7 +41,10 @@ struct SymbolicDimensions{R} <: AbstractDimensions{R}
3741 SymbolicDimensions {_R} (data:: SA.SparseVector ) where {_R} = new {_R} (data)
3842end
3943
44+ static_fieldnames (:: Type{<:SymbolicDimensions} ) = ALL_SYMBOLS
4045data (d:: SymbolicDimensions ) = getfield (d, :_data )
46+ Base. getproperty (d:: SymbolicDimensions{R} , s:: Symbol ) where {R} = data (d)[ALL_MAPPING[s]]
47+ Base. getindex (d:: SymbolicDimensions{R} , k:: Symbol ) where {R} = getproperty (d, k)
4148constructor_of (:: Type{<:SymbolicDimensions} ) = SymbolicDimensions
4249
4350SymbolicDimensions {R} (d:: SymbolicDimensions ) where {R} = SymbolicDimensions {R} (data (d))
@@ -59,14 +66,19 @@ function Base.convert(::Type{Q}, q::Quantity{<:Any,<:SymbolicDimensions}) where
5966 end
6067 return result
6168end
69+
70+ """
71+ expand_units(q::Quantity{<:Any,<:SymbolicDimensions})
72+
73+ Expand the symbolic units in a quantity to their base SI form.
74+ In other words, this converts a `Quantity` with `SymbolicDimensions`
75+ to one with `Dimensions`.
76+ """
6277function expand_units (q:: Q ) where {T,R,D<: SymbolicDimensions{R} ,Q<: Quantity{T,D} }
6378 return convert (Quantity{T,Dimensions{R}}, q)
6479end
6580
6681
67- static_fieldnames (:: Type{<:SymbolicDimensions} ) = ALL_SYMBOLS
68- Base. getproperty (d:: SymbolicDimensions{R} , s:: Symbol ) where {R} = data (d)[ALL_MAPPING[s]]
69- Base. getindex (d:: SymbolicDimensions{R} , k:: Symbol ) where {R} = getproperty (d, k)
7082Base. copy (d:: SymbolicDimensions ) = SymbolicDimensions (copy (data (d)))
7183Base.:(== )(l:: SymbolicDimensions , r:: SymbolicDimensions ) = data (l) == data (r)
7284Base. iszero (d:: SymbolicDimensions ) = iszero (data (d))
@@ -131,6 +143,22 @@ module SymbolicUnitsParse
131143 return nothing
132144 end
133145
146+ """
147+ sym_uparse(raw_string::AbstractString)
148+
149+ Parse a string containing an expression of units and return the
150+ corresponding `Quantity` object with `Float64` value.
151+ However, that unlike the regular `u"..."` macro, this macro uses
152+ `SymbolicDimensions` for the dimension type, which means that all units and
153+ constants are stored symbolically and will not automatically expand to SI
154+ units. For example, `sym_uparse("km/s^2")` would be parsed to
155+ `Quantity(1.0, SymbolicDimensions, km=1, s=-2)`.
156+
157+ Note that inside this expression, you also have access to the `Constants`
158+ module. So, for example, `sym_uparse("Constants.c^2 * Hz^2")` would evaluate to
159+ `Quantity(1.0, SymbolicDimensions, c=2, Hz=2)`. However, note that due to
160+ namespace collisions, a few physical constants are not available.
161+ """
134162 function sym_uparse (raw_string:: AbstractString )
135163 _generate_unit_symbols ()
136164 Constants. _generate_unit_symbols ()
145173
146174import . SymbolicUnitsParse: sym_uparse
147175
176+ """
177+ us"[unit expression]"
178+
179+ Parse a string containing an expression of units and return the
180+ corresponding `Quantity` object with `Float64` value. However,
181+ unlike the regular `u"..."` macro, this macro uses `SymbolicDimensions`
182+ for the dimension type, which means that all units and constants
183+ are stored symbolically and will not automatically expand to SI units.
184+ For example, `us"km/s^2"` would be parsed to `Quantity(1.0, SymbolicDimensions, km=1, s=-2)`.
185+
186+ Note that inside this expression, you also have access to the `Constants`
187+ module. So, for example, `us"Constants.c^2 * Hz^2"` would evaluate to
188+ `Quantity(1.0, SymbolicDimensions, c=2, Hz=2)`. However, note that due to
189+ namespace collisions, a few physical constants are not available.
190+ """
148191macro us_str (s)
149192 return esc (SymbolicUnitsParse. sym_uparse (s))
150193end
0 commit comments