Unitful.jl icon indicating copy to clipboard operation
Unitful.jl copied to clipboard

Counts?

Open caseykneale opened this issue 4 years ago • 10 comments

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.

caseykneale avatar Feb 09 '20 01:02 caseykneale

"turn" is defined in UnitfulAngles.jl

giordano avatar Feb 09 '20 01:02 giordano

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.

caseykneale avatar Feb 09 '20 01:02 caseykneale

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.

tlnagy avatar Dec 23 '20 01:12 tlnagy

related: https://github.com/PainterQubits/Unitful.jl/issues/38

goretkin avatar Dec 28 '20 04:12 goretkin

I also just had a use case for a ppm unit for frequency corrections on an SDR.

Keno avatar Jul 11 '21 00:07 Keno

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

sostock avatar Jul 11 '21 16:07 sostock

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.

sostock avatar Jul 11 '21 16:07 sostock

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.

drdozer avatar Aug 04 '21 13:08 drdozer

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.

drdozer avatar Aug 04 '21 13:08 drdozer

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

jariji avatar Mar 18 '23 21:03 jariji