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

fix multiplication of particles and quantity #159

Merged
merged 1 commit into from
Jan 8, 2025
Merged

Conversation

baggepinnen
Copy link
Owner

closes #152

@baggepinnen baggepinnen mentioned this pull request Jan 7, 2025
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.33%. Comparing base (f4a9bd6) to head (26a6d10).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #159      +/-   ##
==========================================
- Coverage   89.59%   89.33%   -0.27%     
==========================================
  Files          20       20              
  Lines        1153     1153              
==========================================
- Hits         1033     1030       -3     
- Misses        120      123       +3     
Flag Coverage Δ
unittests 89.33% <100.00%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aplavin
Copy link
Contributor

aplavin commented Jan 7, 2025

Seems to work for me, thanks!
Shouldn't introduce new ambiguities either...

QT = Base.promote_op($op, T, typeof(y))
$PT{QT,N}($(op).(p.particles, y))
end

# Below is just the reverse signature of above
function Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T <: Quantity, N}
function Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T <: Number, N}
QT = Base.promote_op($op, typeof(y), T)
Copy link
Contributor

@aplavin aplavin Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why these promote_op calls are needed at all?
Seems like

Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T <: Number, N} = $PT($(op).(y, p.particles))

would work exactly the same.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, inference fails for this implementation, compare the two inference results below, where the first is with the implementation in this PR and the second is the one you propose
image

Copy link
Contributor

@aplavin aplavin Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, indeed – it cannot infer the 2nd argument, number of particles.
Still, it's generally recommended to avoid explicitly calling inference (promote_op) whenever possible, and the approach used elsewhere in this package is cleaner, eg

function ($m.$f)(p::$PT{T,N},a::Real...) where {T,N}
res = ($m.$f).(p.particles, MonteCarloMeasurements.maybe_particles.(a)...) # maybe_particles introduced to handle >2 arg operators
return $PT{eltype(res),N}(res)
:

function Base.$f(y::Quantity, p::$PT{T,N}) where {T, N}
    res = $(op).(y, p.particles)
    $PT{eltype(res), N}(res)
end

@baggepinnen baggepinnen merged commit 814ca57 into master Jan 8, 2025
4 of 5 checks passed
@baggepinnen baggepinnen deleted the unitfulscalars branch January 8, 2025 07:21
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

Successfully merging this pull request may close these issues.

Particles & Unitful
2 participants