SymbolicML / DynamicQuantities.jl

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

Fix promotion rules in `+, -, *, /` #83

Closed MilesCranmer closed 10 months ago

MilesCranmer commented 11 months ago

Right now the promotion rules for these try to promote the value type itself. This means that Vector{Float64} plus a Int64 would try to promote to Any..... So this PR changes it so that it only promotes the quantity and dimension type, but leaves the value type to be promoted by the operation itself.

With this PR, we can now do

0.3us"km" + 0.3u"m"

and it will automatically convert the first term to a regular Dimensions before the calculation.

github-actions[bot] commented 10 months ago

Benchmark Results

main c291f5f5155e42... t[main]/t[c291f5f5155e42...]
Quantity/creation/Quantity(x) 3.1 ± 0.01 ns 3.1 ± 0.01 ns 1
Quantity/creation/Quantity(x, length=y) 3.11 ± 0.01 ns 3.11 ± 0.01 ns 1
Quantity/with_numbers/*real 3.1 ± 0.01 ns 3.1 ± 0.01 ns 1
Quantity/with_numbers/^int 8.05 ± 1.9 ns 8.05 ± 1.9 ns 1
Quantity/with_numbers/^int * real 8.05 ± 1.9 ns 8.05 ± 1.9 ns 1
Quantity/with_quantity/+y 5.27 ± 0.01 ns 5.27 ± 0.01 ns 1
Quantity/with_quantity//y 3.41 ± 0.01 ns 3.41 ± 0.01 ns 1
Quantity/with_self/dimension 1.55 ± 0.01 ns 1.55 ± 0.01 ns 1
Quantity/with_self/inv 3.11 ± 0.01 ns 3.11 ± 0.01 ns 1
Quantity/with_self/ustrip 1.55 ± 0.01 ns 1.55 ± 0.01 ns 1
QuantityArray/broadcasting/multi_array_of_quantities 0.147 ± 0.14 ms 0.148 ± 0.14 ms 0.991
QuantityArray/broadcasting/multi_normal_array 0.0471 ± 0.0034 ms 0.0471 ± 0.00026 ms 1
QuantityArray/broadcasting/multi_quantity_array 0.158 ± 0.00047 ms 0.158 ± 0.00071 ms 1
QuantityArray/broadcasting/x^2_array_of_quantities 24.8 ± 1.7 μs 25.3 ± 1.8 μs 0.98
QuantityArray/broadcasting/x^2_normal_array 4.52 ± 1.2 μs 4.69 ± 0.92 μs 0.964
QuantityArray/broadcasting/x^2_quantity_array 5.96 ± 0.34 μs 6 ± 0.3 μs 0.994
QuantityArray/broadcasting/x^4_array_of_quantities 0.0816 ± 0.00045 ms 0.0816 ± 0.00044 ms 0.999
QuantityArray/broadcasting/x^4_normal_array 0.0528 ± 0.0092 ms 0.0436 ± 0.00018 ms 1.21
QuantityArray/broadcasting/x^4_quantity_array 0.0561 ± 0.00016 ms 0.0561 ± 0.00021 ms 1
time_to_load 0.163 ± 0.0012 s 0.164 ± 0.0021 s 0.995

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).