You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Converting between angular frequency and frequency (e.g., rad/s to Hz) #6032
I just tried a couple of ways of converting from 'rad / s' to 'Hz' that gave me different behavior than I was expecting. Doing this conversion requires dividing by 2π, but the dimensionless_angles equivalency uses a factor of 1 instead of 2π. Here are some examples that confused me when I first tried them. The second example gets pretty close to the behavior I was expecting, except that it seems non-intuitive that 'cycle / s' cannot be converted to 'Hz'.
>>>fromastropyimportunitsasu>>>angular_frequency=1.0*u.rad/u.s>>>angular_frequency.to(u.Hz)
UnitConversionError: 'rad / s' (angularspeed) and'1 / s' (frequency) arenotconvertible>>>frequency=angular_frequency.to(u.cycle/u.s) # a cycle is 2π radians<Quantity0.15915494309189535cycle/s>>>>frequency.to(u.Hz)
UnitConversionError: 'cycle / s' (angularspeed) and'Hz' (frequency) arenotconvertible>>>u.set_enabled_equivalencies(u.dimensionless_angles())
>>>angular_frequency=1.0*u.rad/u.s>>>angular_frequency.to(u.Hz)
<Quantity1.0Hz>
Should units allow conversion between angular frequency and frequency by default? Would it be better to add this as another equivalency along with dimensionless_angles? Or is the status quo preferred? Allowing this conversion by default would result in more readable code for me and I think this would be more intuitive, but I don't know if there are any adverse consequences.
Also, here's my code for a custom equivalency that gave me the expected behavior:
@namurphy - thanks for the report. This actually a tricky one. As you will have seen from the long discussion in #1161, dimensionless_angles is meant in the physical sense, where radius * angle has most usefully units of length, not of length * angle. With dimensionless_angles one basically tells Quantity that Hz should be treated as equivalent to rad/s -- which is not what you want!
What you seem to want instead is to treat Hz as cycle/s, for which your later equivalency works, or more simply:
Now whether this should be a possible equivalency in astropy or not depends a bit on where exactly you are using this and whether this is likely a common usage. Is it in the context of spectroscopy? If so, maybe it could be added to u.spectral() -- but I have heard of wavenumber in linear and angular, but not of angular frequencies in that context.
Yes, dealing with dimensionless dimensions is definitely a tricky issue!
This came up while I was calculating basic plasma parameters such as an ion or electron gyrofrequency. These are usually calculated as angular frequency in units of radians per second, with the corresponding frequency in inverse seconds being given as f = ω/2π. In wave physics applications the standard convention is to have angular frequency in radians per second be convertible to Hz, so I do expect this to be common usage.
I don't know if u.spectral() is the best place for this, however, as that enables several other equivalencies that assume we're dealing with light. The f = ω/2π relation is much more general than that since it applies to any sinusoidal wave, which is part of the reason why I'm mostly thinking this should be the default behavior within units.
I am also thinking about how units treats becquerels (where 1 Bq = 1 decay event per second). u.Bq.to(u.Hz) does give 1.0.
Edited to add: In pint there is an issue called Add angular frequency which has a comment that says:
But the problem is that hertz is the inverse of second. It is not a good idea to redefine it in the canonical list included with pint as it will break user expectations. Converting from frequency to angular frequency is not really a unit conversion problem. It is a relation between physical quantities. Both have the same units: 1/second.
That's is fairly convincing, so now I'm thinking that the equivalency approach is better and that perhaps using dimensionless_angles is the way for me to go while manually keeping track of all of the factors of 2π. Hm....
But there's more... In plasma physics there are characteristic length scales that are given by a speed divided by an angular frequency (e.g., an ion inertial length is c/ωpi or the speed of light divided by the ion plasma frequency).
For this, dimensionless_angles() would give the desired behavior and there should be no multiplication or division by 2π. Sheesh! I'm tempted to submit a pull request to set 2π=1...
Sorry to keep circling back to this with such frequency but I'm grateful that you understand my degree of confusion!
Part of my confusion came from not fully understanding the implications of dimensionless_angles(). I suggest that the first thing that we do is expand the documentation on dimensionless_angles() to include information on the potential pitfalls that I encountered, and more broadly on what it means for radians to be dimensionless. That should help allay confusion should anyone else try to use this in the same way that I did. Does that sound like a worthwhile thing to do? We could then postpone the discussion of whether or not another built-in equivalency would be worth it until after we add this additional documentation. I can probably work on this.
One more potential pitfall:
>>> (1*u.cycle/u.s).to(u.Hz, equivalencies=u.dimensionless_angles()) # expected to be 1 Hz<Quantity6.283185307179586Hz>
@namurphy - It may be an idea to add your specific example of cy/s to Hz to the documentation [1], as it is logical but not necessarily expected, and perhaps also give the way to solve it):
@mhvk - Sure! I'll aim to get to that in the next couple of days, while simultaneously trying to get myself unconfused about this. You solution is very clear and explicity, so I like it and will use that approach going forward. I'll also include it in the pull request.
I'm presently a net negative contributor (minus three characters!) so I'm happy to make a positive contribution!
Activity
pllim commentedon May 10, 2017
Maybe this was an oversight because
u.spectral()is able to handleu.rad / u.m, so it only makes sense that it should also handleu.rad / u.s; @mhvk ?mhvk commentedon May 10, 2017
@namurphy - thanks for the report. This actually a tricky one. As you will have seen from the long discussion in #1161,
dimensionless_anglesis meant in the physical sense, whereradius * anglehas most usefully units of length, not oflength * angle. Withdimensionless_anglesone basically tellsQuantitythatHzshould be treated as equivalent torad/s-- which is not what you want!What you seem to want instead is to treat
Hzascycle/s, for which your later equivalency works, or more simply:Now whether this should be a possible equivalency in astropy or not depends a bit on where exactly you are using this and whether this is likely a common usage. Is it in the context of spectroscopy? If so, maybe it could be added to
u.spectral()-- but I have heard of wavenumber in linear and angular, but not of angular frequencies in that context.namurphy commentedon May 10, 2017
Yes, dealing with dimensionless dimensions is definitely a tricky issue!
This came up while I was calculating basic plasma parameters such as an ion or electron gyrofrequency. These are usually calculated as angular frequency in units of radians per second, with the corresponding frequency in inverse seconds being given as f = ω/2π. In wave physics applications the standard convention is to have angular frequency in radians per second be convertible to Hz, so I do expect this to be common usage.
I don't know if
u.spectral()is the best place for this, however, as that enables several other equivalencies that assume we're dealing with light. The f = ω/2π relation is much more general than that since it applies to any sinusoidal wave, which is part of the reason why I'm mostly thinking this should be the default behavior within units.I am also thinking about how units treats becquerels (where 1 Bq = 1 decay event per second).
u.Bq.to(u.Hz)does give1.0.Edited to add: In pint there is an issue called Add angular frequency which has a comment that says:
That's is fairly convincing, so now I'm thinking that the equivalency approach is better and that perhaps using
dimensionless_anglesis the way for me to go while manually keeping track of all of the factors of 2π. Hm....namurphy commentedon May 10, 2017
But there's more... In plasma physics there are characteristic length scales that are given by a speed divided by an angular frequency (e.g., an ion inertial length is c/ωpi or the speed of light divided by the ion plasma frequency).
For this,
dimensionless_angles()would give the desired behavior and there should be no multiplication or division by 2π. Sheesh! I'm tempted to submit a pull request to set2π=1...pllim commentedon May 11, 2017
@namurphy , you are welcome to submit a PR!
namurphy commentedon May 11, 2017
Sorry to keep circling back to this with such frequency but I'm grateful that you understand my degree of confusion!
Part of my confusion came from not fully understanding the implications of
dimensionless_angles(). I suggest that the first thing that we do is expand the documentation ondimensionless_angles()to include information on the potential pitfalls that I encountered, and more broadly on what it means for radians to be dimensionless. That should help allay confusion should anyone else try to use this in the same way that I did. Does that sound like a worthwhile thing to do? We could then postpone the discussion of whether or not another built-in equivalency would be worth it until after we add this additional documentation. I can probably work on this.One more potential pitfall:
mhvk commentedon May 11, 2017
@namurphy - It may be an idea to add your specific example of
cy/stoHzto the documentation [1], as it is logical but not necessarily expected, and perhaps also give the way to solve it):Do you want to make a PR?
[1] http://docs.astropy.org/en/latest/units/equivalencies.html#angles-as-dimensionless-units
namurphy commentedon May 11, 2017
@mhvk - Sure! I'll aim to get to that in the next couple of days, while simultaneously trying to get myself unconfused about this. You solution is very clear and explicity, so I like it and will use that approach going forward. I'll also include it in the pull request.
I'm presently a net negative contributor (minus three characters!) so I'm happy to make a positive contribution!
virtualmarioe commentedon Aug 12, 2019
Hey @namurphy, this conversion is exactly what I was looking for in the context of neural population simulations. Thanks for raising the issue.
Add Hz as rps
Add Hz as rps