Unitful.jl
Unitful.jl copied to clipboard
Counts?
Does it make sense to define "Counts" ?
For example, in intensity can be described as counts per second. Similarly, for an inductor or helmholtz coil we might have "turns" / meter. Now turns is just syntactic sugar really and could be user defined, it's a counting event.
Or in chemistry we have a notion of a "mole" which is ~6.023x10^23 "atoms/molecules/moeities/etc". For analysis sometimes we care about this type of unit IE: Parts Per Million (ppm).
I understand these aren't SI or MKT units but they are fundamental :). Would really appreciate this consideration. There is a chance this is in here somewhere but I can't figure out how to do it without some pretty hacky stuff.
"turn" is defined in UnitfulAngles.jl
In this case a "turn" is the number of times a wire is wrapped around an object or an air core. So it could be thought of as just a scalar count. Although, it could be fractional I guess - most people wouldn't bother with that case unless they were doing something more advanced though.
I just ran into this as well. For example, it would be nice to have unique counts that you can operate on. For example, IFU
(infectious units of virus) and cells
as units.
julia> using Unitful
julia> @unit cells "cells" Cells 1 false
cells
julia> @unit IFU "IFU" IFU 1 false
IFU
julia> 100_000cells/IFU
100000 cells IFU^-1
julia> upreferred(ans)
100000
Everything works except the last step because cells and IFU should not cancel out. I know this is happening due to my setting of equals
for each to be 1, but it would be nice to make that optional.
related: https://github.com/PainterQubits/Unitful.jl/issues/38
I also just had a use case for a ppm
unit for frequency corrections on an SDR.
We could add counts
and ppm
. After all, we already have percent
, permille
and pertenthousand
.
The only issue I see with adding counts
is that the following might seem inconsistent (I don’t think it is too much of a problem):
julia> 1u"rad" == 1u"counts"
true
julia> u"rad" == u"counts"
false
Everything works except the last step because cells and IFU should not cancel out.
Why is the last step necessary? If you want them to not cancel out, you can create a new dimension for one (or both) of them.
Sorry, I opened another issue for essentially this. The text of that is:
I often want to do unit-safe arithmetic on things that aren't si units. For example, I want a unit for books and for bookshelfs. Then I can express the capacity of a bookshelf with the units books/bookshelf. However, I can't see an easy way to do this kind of ad-hockery. The @uinit macro doesn't seem appropriate as it wants a conversion factor to something else. The @dimension macro also doesn't seem to really fit, since I'm not sure how I should express books or bookshelfs as dimensions.
Any help would be greatly appreciated. I would love to make my arithmetic unitful as a matter of course, for the domain-specific quantities I have that day.
Perhaps we need a @countable macro, that symultaneously sets up a dimension for your item type and a unit that's the preferred and only unit for that dimension? Because I want to be able to get compound units like 'books/bookshelf' but it would be nice if I could achieve that with a one-liner to introduce the books units and another 1-liner for the bookshelf units.
A @countable
macro for books/bookshelf
https://discourse.julialang.org/t/wrapping-unitful-macros/95171/3 with thanks to @bertschi
using Unitful
macro countable(name)
namestr = uppercasefirst(string(name))
dimsym = Symbol(namestr, "Dim")
dimstr = namestr * "Dimension"
esc(quote
Unitful.@dimension $(Symbol(namestr, "Dimension")) $dimstr $dimsym
Unitful.@refunit $name $(string(name)) $(Symbol(namestr)) $(Symbol(dimstr)) false
end)
end
@countable book
@countable shelf
@countable case
@assert (20book / shelf) * (8shelf / case) * (10case) == 1600book
@countable book
expands to
Unitful.@dimension BookDimension "BookDimension" BookDim
Unitful.@refunit book "book" Book BookDimension false