@@ -29,6 +29,10 @@ This is to allow for lazily reducing to SI base units, whereas
29
29
`Dimensions` is always in SI base units. Furthermore, `SymbolicDimensions`
30
30
stores dimensions using a sparse vector for efficiency (since there
31
31
are 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`.
32
36
"""
33
37
struct SymbolicDimensions{R} <: AbstractDimensions{R}
34
38
_data:: SA.SparseVector{R}
@@ -37,7 +41,10 @@ struct SymbolicDimensions{R} <: AbstractDimensions{R}
37
41
SymbolicDimensions {_R} (data:: SA.SparseVector ) where {_R} = new {_R} (data)
38
42
end
39
43
44
+ static_fieldnames (:: Type{<:SymbolicDimensions} ) = ALL_SYMBOLS
40
45
data (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)
41
48
constructor_of (:: Type{<:SymbolicDimensions} ) = SymbolicDimensions
42
49
43
50
SymbolicDimensions {R} (d:: SymbolicDimensions ) where {R} = SymbolicDimensions {R} (data (d))
@@ -59,14 +66,19 @@ function Base.convert(::Type{Q}, q::Quantity{<:Any,<:SymbolicDimensions}) where
59
66
end
60
67
return result
61
68
end
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
+ """
62
77
function expand_units (q:: Q ) where {T,R,D<: SymbolicDimensions{R} ,Q<: Quantity{T,D} }
63
78
return convert (Quantity{T,Dimensions{R}}, q)
64
79
end
65
80
66
81
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)
70
82
Base. copy (d:: SymbolicDimensions ) = SymbolicDimensions (copy (data (d)))
71
83
Base.:(== )(l:: SymbolicDimensions , r:: SymbolicDimensions ) = data (l) == data (r)
72
84
Base. iszero (d:: SymbolicDimensions ) = iszero (data (d))
@@ -131,6 +143,22 @@ module SymbolicUnitsParse
131
143
return nothing
132
144
end
133
145
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
+ """
134
162
function sym_uparse (raw_string:: AbstractString )
135
163
_generate_unit_symbols ()
136
164
Constants. _generate_unit_symbols ()
145
173
146
174
import . SymbolicUnitsParse: sym_uparse
147
175
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
+ """
148
191
macro us_str (s)
149
192
return esc (SymbolicUnitsParse. sym_uparse (s))
150
193
end
0 commit comments