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

Automatic conversion of Constant to Quantity #2

Closed
giordano opened this issue Sep 9, 2018 · 6 comments
Closed

Automatic conversion of Constant to Quantity #2

giordano opened this issue Sep 9, 2018 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@giordano
Copy link
Member

giordano commented Sep 9, 2018

In order to use a Constant in mathematical operations one has to explicitly convert it to a Quantity with float(). I think that it's good that the user has to explicitly require the uncertainty with measurement(x), this is something that may not be always needed.

My desire is to make the conversion to Quantity, without uncertainty, automatic. I initially thought that promotion rules would have helped, but I found out that they're quite useless for this (you almost never want to promote a Constant and another number to a common type).

I see the following options:

  • manually overload all relevant mathematical operations here in order to support Constant objects
  • lobby Unitful.jl maintainer to define an AbstractQuantity type that can be subclassed. Constant could then be a subtype of AbstractQuantity and automatically benefits from all the mathematical operations defined there, without duplicating the work here.

Please share your thoughts on this.

@giordano giordano added the help wanted Extra attention is needed label Sep 9, 2018
@Datseris
Copy link

I totally agree that the objects exported by this Module should be immediatelly useable in mathematical operations. I do not think that an explicit conversion should first be done, as this typically means that it is faster to copy paste the wikipedia value :D

I won't comment on how this should be done internally however. I don't have enough experience with coding this package or Uniful....

@BeastyBlacksmith
Copy link

First of all, I'm glad someone takes another hit on this topic. There was a Package with the same name in the past: https://github.com/DataWookie/PhysicalConstants.jl and another that extracts the Data directly from CODATA: https://github.com/kofron/CODATA.jl, but both are unmaintained today.
I forked the old PhysicalConstants and gave it a global switch, so you can change from Value with Dimension to Values without dimension by calling something like switch_units(), don't know if it is of any use for your implementation thogh.
Regarding the abstract type in Unitful.jl you could play with it on your own fork and make a PR if it works out.

@giordano
Copy link
Member Author

@Datseris regarding how this package works internally, it defines a new Constant type, which is very similar to Base.Irrational in spirit (and also in code): each constant is a singleton (empty struct) for which various methods are defined.

The most useful feature of this approach is that you get arbitrary precision values for the physical constants (just like the Base.Irrational mathematical constants), even if you change the precision of BigFloat.

You know, apart from some constants that are defined to be exact (without uncertainty), all physical constants are currently measured with precision much lower than that of double precision floating-point numbers. Thus, it may look an overkill to use arbitrary precision for calculations involving physical constants, but I think that Julia should be ready for the era of high-precision measurements 😄

@BeastyBlacksmith thanks for pointing those packages out. Probably I came across them long time ago and didn't like how they worked. Indeed, I've been thinking about this package for a while now. I don't think I like the idea of having a constant without units, the choice of the reference units would be then arbitrary. However, the user is free to strip the units with ustrip(float(c)) (or, even better, with ustrip(c) if I define an appropriate method for ustrip)

@carstenbauer
Copy link
Member

+1 for AbstractQuantity

@giordano
Copy link
Member Author

giordano commented Nov 5, 2018

I've opened an issue at Unitful.jl: PainterQubits/Unitful.jl#186

@giordano
Copy link
Member Author

giordano commented Jan 27, 2019

I've opened a PR for Unitful.jl defining AbstractQuantity: PainterQubits/Unitful.jl#204 If it gets merged, I've already a branch for this package to achieve these cool features:

julia> using PhysicalConstants.CODATA2014

julia> 2c
5.99584916e8 m s^-1

julia> sqrt((h/2pi) * G / c^3) # Planck length, the units are just metres
1.6162283730808862e-35 J^1/2 s kg^-1/2

julia> α
Fine-structure constant (α)
Value                         = 0.0072973525664
Standard uncertainty          = 1.7e-12
Relative standard uncertainty = 2.3e-10
Reference                     = CODATA 2014

julia> α/2
0.0036486762832

julia> typeof/2)
Float64

😃

However this needs improvement as it currently works only with Float64 values of the constants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants