julia> function hz(x::typeof(1.0u"Hz"))
2x
end
hz (generic function with 1 method)
and this does not work:
julia> typeof(1.0u"Hz")
Quantity{Float64,π^-1,Unitful.FreeUnits{(Hz,),π^-1,nothing}}
julia> function hz2(x::Quantity{Float64,π^-1,Unitful.FreeUnits{(Hz,),π^-1,nothing}})
4x
end
ERROR: UndefVarError: π not defined
Stacktrace:
[1] top-level scope at REPL[37]:1
julia> function hz(x::typeof(1.0Hz))
2x
end
hz (generic function with 1 method)
julia> hz(2.0Hz)
4.0 Hz
julia> hz(1/0.5s)
ERROR: MethodError: no method matching hz(::Quantity{Float64,π^-1,Unitful.FreeUnits{(s^-1,),π^-1,nothing}})
Closest candidates are:
hz(::Quantity{Float64,π^-1,Unitful.FreeUnits{(Hz,),π^-1,nothing}}) at REPL[68]:1
Stacktrace:
[1] top-level scope at REPL[70]:1
However
julia> 2.0Hz + 1/0.5s
4.0 s^-1
works?? Why?
How to define a type for dispatch that will accept either Hz or 1/s, Union?
The real question is why do you have to do this? Its better if we can help you find a solution to your problem generally than specifically help on this one detail.
Sorry, I thought it is obvious. I want to write a function that (in this case) accepts frequency as an argument and dispatch using it. Frequency can be defined as Hertz or 1/s (an inverse of period). Computationally these units are identical. How to accomplish this? Maybe I have misunderstood the purpose of Unitful.
I havenβt used Unitful, but Iβm guessing that Hz and typeof(1/0.5s) are different. In that case you have to define your function to accept a Union or define a wrapper to convert from inverse seconds to Hz. But because arithmetic operations are common, Unitful has evidently defined promotion rules to automatically convert types as needed in arithmetic operations.
Just donβt specify the units you want, instead specify the dimensions. E.g. in this case, you want any Quantity which has dimensions of π^-1, so you just do
julia> using Unitful; using Unitful: Hz, s, π
julia> f(x::Quantity{<:Any, π^-1}) = 2x
f (generic function with 1 method)
julia> f(2.0Hz)
4.0 Hz
julia> f(1/0.5s)
4.0 s^-1
I am writing an averaging function for interferometric data. It is possible to average over time or frequency and the algorithm is different for these β dispatch. BTW, I am using DimensionalData.jl for dealing with multidimensional data (in this case polarization, IF band, frequency channel and time), makes everything a lot easier, thanks!
There are already build-in names for physical dimensions, you can find them here
Use Frequency, it looks nicer and is more explicit
julia> using Unitful; using Unitful: s, Hz, Frequency
julia> f(x::Frequency) = 2x
f (generic function with 1 method)
julia> f(2Hz)
4 Hz
julia> f(1/2s)
1.0 s^-1
Ok dispatch like that absolutely makes sense then! So yes, as Mason pointed out is the way. I couldnβt tell if you were a newb trying to do something weird with units you donβt actually need to do