Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for angles not having a dimension #570

Closed
DanDeepPhase opened this issue Oct 6, 2022 · 7 comments
Closed

Workaround for angles not having a dimension #570

DanDeepPhase opened this issue Oct 6, 2022 · 7 comments

Comments

@DanDeepPhase
Copy link

Preface

Currently angles do not have a dimension, leading to several issues #531 #537 #392 #216 #38 and stems from a conversation in #9 . Personally, I believe that angles should have a dimension, and appreciate the arguments in this paper: https://iopscience.iop.org/article/10.1088/0026-1394/52/1/40. I suspect that the argument "the circumference of a circle should be (2π radians) * (radius) which better have dimensions of length." is incorrect, because its probably shorthand for a limit series of inscribed triangles. I think the correct definition of circumference is "(2π)*(radius)" and if you are taking an arc segment then you are multiplying by (arc-angle radians) / (2π radians).
Note: Similar issues have requested the ability to dispatch on angles, to which the suggestion has been to create a union Quantity type. This gets partway there, but the angles are still dimensionless
Note: The UnitfulAngles package does not address dimensions or union types
Note: SI currently treats angles as dimensionless, so i understand mine is a minority opinion.

My Pain point

I work with radiometric units, where steradians are used to distinguish between quantities. some typical units are:

Spectral Radiance = u"W/(m^2*μm*sr)"
Spectral Irradiance = u"W/(m^2*μm)"

A typical problem is that i might integrate over the wavelength (the μm term here) which sometimes comes in other units, like nanometers. This leads to a clumsy nm W μm^-1 m^-2 sr^-1 which i need to simplify. upreferred turns this into kg/s^3 so i've tried to dispatch on derived dimensions of

@derived_dimension Radiance 𝐌/(𝐋*𝐓^2) false
@derived_dimension Irradiance 𝐌/(𝐋*𝐓^2) false

unfortunately, due to the lack of an angular dimension, i have some errors in my math where angles are inappropriately eliminated or added.

Paths forward

  1. Add a dimension for angles - I understand that I am in the minority opinion here, and this seems like the kind of change that could break code for people comfortable ignoring units in small angle approximations. However, it seems like a problem that other people are running into.
    1B. Add a dimension for solid angles only - this is a hack compromise which would fix my code, and probably not break too many existing code bases.
  2. I create a package that respects dimensionality of angles. however, I'm not sure how to do this since i think i either need to redefine the definitions in pkgdefaults or create some workaround types like myradian mydegree mysteradian... etc...
  3. is there some way to determine if an angle is present in a quantity in order to preserve dimensions when converting units? ie since i cant tell the difference dimensionally, to check the units of the input Quantity, extract the angle units which would be lost, perform the conversion, then reapply the angle units to the result. The type generation macros have prevented me from fully understanding what is going on inside the Quantities or how to test parts of them.

Any willingness for #1? if not, any suggestions for #2/#3?

@sostock
Copy link
Collaborator

sostock commented Oct 30, 2022

  1. Add a dimension for angles - I understand that I am in the minority opinion here, and this seems like the kind of change that could break code for people comfortable ignoring units in small angle approximations. However, it seems like a problem that other people are running into.
    1B. Add a dimension for solid angles only - this is a hack compromise which would fix my code, and probably not break too many existing code bases.

That’s very unlikely to happen.

  1. I create a package that respects dimensionality of angles. however, I'm not sure how to do this since i think i either need to redefine the definitions in pkgdefaults or create some workaround types like myradian mydegree mysteradian... etc...

I would create a package (maybe called DimensionfulAngles.jl) that defines dimensions for angle and solid angle and units of those dimensions. I wouldn’t attempt to redefine the units defined in Unitful.jl, but define the dimensionful units in addition (maybe adding a suffix so they can be distinguished when printed). The package should also contain functions to convert between the standard Unitful.jl (dimensionless) angles and the dimensionful angles.

  1. is there some way to determine if an angle is present in a quantity in order to preserve dimensions when converting units? ie since i cant tell the difference dimensionally, to check the units of the input Quantity, extract the angle units which would be lost, perform the conversion, then reapply the angle units to the result. The type generation macros have prevented me from fully understanding what is going on inside the Quantities or how to test parts of them.

There isn’t a super nice way of doing this, but it is not too hard either: The first type parameter of the Unitful.Units type is a tuple of Unitful.Unit values. For degrees, you would need to look for a value of type Unitful.Unit{:Degree, NoDims} in this tuple:

julia> my_unit = u"s/°^2"
s °^-2

julia> units_tuple(::Unitful.Units{T}) where T = T
units_tuple (generic function with 1 method)

julia> units_tuple(my_unit)
(°^-2, s)

julia> units_tuple(my_unit)[1]
°^-2

julia> units_tuple(my_unit)[1] |> typeof
Unitful.Unit{:Degree, NoDims}

julia> units_tuple(my_unit)[1].power
-2//1

@cmichelenstrofer
Copy link
Contributor

cmichelenstrofer commented Nov 25, 2022

@DanDeepPhase @sostock I am working on option 2 above. It is almost ready for release! I started it a while ago, picking it up again now. I am using a superscript as well as a modified string (both modeled after UnitfulUS) .

@cmichelenstrofer
Copy link
Contributor

Here is a first version. Any feedback or contributions would be appreciated! (I started learning Julia recently and this is my first package).

@cmichelenstrofer
Copy link
Contributor

@sostock I really like your name suggestion "DimensionfulAngles.jl"! Can I use this? If so, I would mention it in the Acknwledgements or (even better) if you have time you can do a PR 🤓.

@sostock
Copy link
Collaborator

sostock commented Dec 5, 2022

@cmichelenstrofer You can use the name, you don’t have to credit me for it.

@cmichelenstrofer
Copy link
Contributor

FYI The package is now registered. It is here: https://github.com/cmichelenstrofer/DimensionfulAngles.jl. Hope you find it useful!

@DanDeepPhase
Copy link
Author

Thanks. It looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants