Skip to content

Commit 996ff5c

Browse files
committed
Simplify docs and test string input to as_u
1 parent e099b1d commit 996ff5c

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/symbolic_dimensions.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,29 @@ expand_units(q::QuantityArray) = expand_units.(q)
9696

9797
"""
9898
as_u(q::AbstractQuantity{<:Any, <:Dimensions}, qout::AbstractQuantity{<:Any, <:SymbolicDimensions})
99+
as_u(q::AbstractQuantity{<:Any, <:Dimensions}, ustr::String)
99100
100101
Convert a quantity `q` with base SI units to the symbolic units of `qout`, for `q` and `qout` with compatible units.
101102
Mathematically, the result has value `q / expand_units(qout)` and units `dimension(qout)`.
103+
For string input, `qout` is created by parsing `ustr` as a symbolic unit, i.e. `qout = sym_uparse(ustr)`.
102104
"""
103105
function as_u(q::AbstractQuantity{<:Any, <:Dimensions}, qout::AbstractQuantity{<:Any, <:SymbolicDimensions})
104106
qout_expanded = expand_units(qout)
105107
dimension(q) == dimension(qout_expanded) || throw(DimensionError(q, qout_expanded))
106108
return new_quantity(typeof(qout), ustrip(q) / ustrip(qout_expanded), dimension(qout))
107109
end
110+
as_u(q::AbstractQuantity{<:Any, <:Dimensions}, ustr::String) = as_u(q, sym_uparse(ustr))
108111

109112
"""
110113
as_u(qout::AbstractQuantity{<:Any, <:SymbolicDimensions})
114+
as_u(ustr::String)
111115
112116
Create a function that converts an input quantity `q` with base SI units to the symbolic units of `qout`, i.e
113117
a function equivalent to `q -> as_u(q, qout)`.
118+
For string input, `qout` is created by parsing `ustr` as a symbolic unit, i.e. `qout = sym_uparse(ustr)`.
114119
"""
115120
as_u(qout::AbstractQuantity{<:Any, <:SymbolicDimensions}) = Base.Fix2(as_u, qout)
116-
117-
"""
118-
as_u(q::AbstractQuantity{<:Any, <:Dimensions}, qout::String)
119-
120-
Convert a quantity `q` with base SI units to a set of compatible units specified of `qout`.
121-
Internally, this works by parsing `qout` as a symbolic unit, so this is equivalent to `as_u(q, sym_uparse(qout))`.
122-
"""
123-
as_u(q::AbstractQuantity{<:Any, <:Dimensions}, qout::String) = as_u(q, sym_uparse(qout))
121+
as_u(ustr::String) = Base.Fix2(as_u, ustr)
124122

125123
Base.copy(d::SymbolicDimensions) = SymbolicDimensions(copy(data(d)))
126124
Base.:(==)(l::SymbolicDimensions, r::SymbolicDimensions) = data(l) == data(r)

test/unittests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ end
526526
@test us"Constants.h" != us"h"
527527
@test expand_units(us"Constants.h") == u"Constants.h"
528528

529-
@test as_u(5e-9u"m", us"nm") (5e-9u"m" |> as_u(us"nm")) 5us"nm"
529+
@test as_u(5e-9u"m", us"nm") as_u(5e-9u"m", "nm") (5e-9u"m" |> as_u(us"nm")) (5e-9u"m" |> as_u("nm")) 5us"nm"
530530
@test_throws DimensionError as_u(5e-9u"m", us"nm * J")
531531

532532
# Actually expands to:

0 commit comments

Comments
 (0)