SymbolicML / DynamicQuantities.jl

Efficient and type-stable physical quantities in Julia
https://symbolicml.org/DynamicQuantities.jl/dev/
Apache License 2.0
120 stars 15 forks source link

Add angular units #117

Closed MilesCranmer closed 4 months ago

MilesCranmer commented 4 months ago

Fixes #28

@mikeingold since you have interest in using this, what do you think?

Here I am using the SI definition that 1 rad = 1. So to have "rad" actually show up in quantities you would need to use symbolic units, like the following:

julia> θ = 10.2us"deg"
10.2 deg

julia> r = 5us"km"
5.0 km

julia> θ * r |> uexpand
890.117918517108 m

which I think seems okay?

github-actions[bot] commented 4 months ago

Benchmark Results

main 1424a94fe06ce9... main/1424a94fe06ce9...
Quantity/creation/Quantity(x) 3.41 ± 0.01 ns 3.71 ± 0.011 ns 0.919
Quantity/creation/Quantity(x, length=y) 3.11 ± 0.01 ns 3.42 ± 0.01 ns 0.909
Quantity/with_numbers/*real 3.11 ± 0.01 ns 3.1 ± 0.01 ns 1
Quantity/with_numbers/^int 8.05 ± 2.2 ns 8.37 ± 2.2 ns 0.963
Quantity/with_numbers/^int * real 8.68 ± 2.2 ns 8.05 ± 1.8 ns 1.08
Quantity/with_quantity/+y 4.04 ± 0.001 ns 4.04 ± 0.001 ns 1
Quantity/with_quantity//y 3.42 ± 0.01 ns 3.11 ± 0.001 ns 1.1
Quantity/with_self/dimension 3.1 ± 0.01 ns 2.79 ± 0.009 ns 1.11
Quantity/with_self/inv 3.11 ± 0.001 ns 3.11 ± 0.001 ns 1
Quantity/with_self/ustrip 2.79 ± 0.01 ns 2.79 ± 0.01 ns 1
QuantityArray/broadcasting/multi_array_of_quantities 0.145 ± 0.00075 ms 0.15 ± 0.0008 ms 0.967
QuantityArray/broadcasting/multi_normal_array 0.0529 ± 0.00028 ms 0.0498 ± 0.00029 ms 1.06
QuantityArray/broadcasting/multi_quantity_array 0.155 ± 0.00066 ms 0.155 ± 0.00059 ms 1
QuantityArray/broadcasting/x^2_array_of_quantities 25 ± 3 μs 24.6 ± 2.8 μs 1.02
QuantityArray/broadcasting/x^2_normal_array 4.52 ± 1.1 μs 4.24 ± 1.1 μs 1.07
QuantityArray/broadcasting/x^2_quantity_array 6.98 ± 0.28 μs 6.9 ± 0.28 μs 1.01
QuantityArray/broadcasting/x^4_array_of_quantities 0.0787 ± 0.00067 ms 0.0787 ± 0.00057 ms 1
QuantityArray/broadcasting/x^4_normal_array 0.0467 ± 0.00019 ms 0.0497 ± 0.00014 ms 0.94
QuantityArray/broadcasting/x^4_quantity_array 0.05 ± 0.003 ms 0.0499 ± 0.00016 ms 1
time_to_load 0.127 ± 0.00052 s 0.127 ± 0.00034 s 1

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR. Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

mikeingold commented 4 months ago

I just added this branch directly into Julia and (at a glance) it seems to behave like I'd expect ,e.g.:

Leλ_units_preferred = us"mW/(m^2*sr*μm)"

val = 1.0us"W/(cm^2*sr*nm)"

2val |> uconvert(Leλ_units_preferred)
# 1.9999999999999996e10 m⁻² μm⁻¹ mW sr⁻¹
MilesCranmer commented 4 months ago

Cool. Thanks! And yeah it looks like Unitful does this the same way so all is good.