|
98 | 98 |
|
99 | 99 | Expand the symbolic units in a quantity to their base SI form. |
100 | 100 | In other words, this converts a `Quantity` with `SymbolicDimensions` |
101 | | -to one with `Dimensions`. |
| 101 | +to one with `Dimensions`. The opposite of this function is `uconvert`, |
| 102 | +for converting to specific symbolic units, or `convert(Quantity{<:Any,<:SymbolicDimensions}, q)`, |
| 103 | +for assuming SI units as the output symbols. |
102 | 104 | """ |
103 | 105 | function expand_units(q::Q) where {T,R,D<:SymbolicDimensions{R},Q<:AbstractQuantity{T,D}} |
104 | 106 | return convert(constructor_of(Q){T,Dimensions{R}}, q) |
105 | 107 | end |
106 | 108 | expand_units(q::QuantityArray) = expand_units.(q) |
107 | 109 |
|
| 110 | +""" |
| 111 | + uconvert(qout::AbstractQuantity{<:Any, <:SymbolicDimensions}, q::AbstractQuantity{<:Any, <:Dimensions}) |
| 112 | +
|
| 113 | +Convert a quantity `q` with base SI units to the symbolic units of `qout`, for `q` and `qout` with compatible units. |
| 114 | +Mathematically, the result has value `q / expand_units(qout)` and units `dimension(qout)`. |
| 115 | +""" |
| 116 | +function uconvert(qout::AbstractQuantity{<:Any, <:SymbolicDimensions}, q::AbstractQuantity{<:Any, <:Dimensions}) |
| 117 | + @assert isone(ustrip(qout)) "You passed a quantity with a non-unit value to uconvert." |
| 118 | + qout_expanded = expand_units(qout) |
| 119 | + dimension(q) == dimension(qout_expanded) || throw(DimensionError(q, qout_expanded)) |
| 120 | + new_val = ustrip(q) / ustrip(qout_expanded) |
| 121 | + new_dim = dimension(qout) |
| 122 | + return new_quantity(typeof(q), new_val, new_dim) |
| 123 | +end |
| 124 | + |
| 125 | +""" |
| 126 | + uconvert(qout::AbstractQuantity{<:Any, <:SymbolicDimensions}) |
| 127 | +
|
| 128 | +Create a function that converts an input quantity `q` with base SI units to the symbolic units of `qout`, i.e |
| 129 | +a function equivalent to `q -> uconvert(qout, q)`. |
| 130 | +""" |
| 131 | +uconvert(qout::AbstractQuantity{<:Any, <:SymbolicDimensions}) = Base.Fix1(uconvert, qout) |
108 | 132 |
|
109 | 133 | Base.copy(d::SymbolicDimensions) = SymbolicDimensions(copy(getfield(d, :nzdims)), copy(getfield(d, :nzvals))) |
110 | 134 | function Base.:(==)(l::SymbolicDimensions, r::SymbolicDimensions) |
|
0 commit comments