-
Notifications
You must be signed in to change notification settings - Fork 3
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: cleaner dispatching onto StaticArrays #51
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
🚀 New features to boost your workflow:
|
Please help review this PR, thank you so much! Edit: Hi, Andrew! There were some merge conflict after latest commit, so I resolved them which led to some commits 🙏🏼 |
Hi, Andrew! My apologies 🙏🏼 There were some after errors after I resolved the merge conflicts so I had to push some additional commits 😅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Feroz! Thanks for this contribution! StaticArrays is such a handy resource for this package, so this is great. I left a few comments for this round of review. More generally, they are the following:
- A cleaner and more encompassing utility function for inferring the input array type(s). This can probability be something like
_infer_types(T1, T2, basis)
_infer_types(T, basis)
- Allowing a user to also just specify a single array type (e.g.
SArray
) if they're lazy and don't want to writeSVector
andSMatrix
.
…f dispatch
…_tensor and _promote_output_matrix
Hi, Andrew! Thank you so much for your wonderful and helpful suggestions! They really helped improved this PR! Please help review this PR, thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Feroz, thanks so much again! A few things:
- I put in one of the comments a potential approach for dealing with the static arrays extension. Let me know if you think that's the way to go.
- We shouldn't have the previous tests for dispatching onto static arrays break. So, e.g., these implementations should all be able to work once this PR is merged:
displace(SVector{2n}, SMatrix{2n,2n}, basis, alpha)
displace(SVector, SMatrix, basis, alpha)
displace(SArray, basis, alpha)
- Could you also make sure these work for random Gaussian objects in
src/randoms.jl
? I could be wrong, but I don't think I saw changes for those methods in this PR.
Co-authored-by: Andrew Kille <[email protected]>
…atch
…as they throw error
… type in Symbolics that we previously encountered
Co-authored-by: Andrew Kille <[email protected]>
Hi, Andrew! Thank you for your suggestions. Kindly please let me know your comments! I have made sure that there are no breaking changes. All three points suggested in #51 (review) have been incorporated. There were some tests that are broken due to the same old symbolic error previously seen in #46 (comment). Maybe plausible change: After taking the tensor product with with static arrays, the It also appears that if the user is lazy and relies on julia> state = coherentstate(SVector, SMatrix, QuadPairBasis(1), 1.0-im)
GaussianState for 1 mode.
symplectic basis: QuadPairBasis
mean: 2-element SVector{2, Any} with indices SOneTo(2):
2.0
-2.0
covariance: 2×2 SMatrix{2, 2, Any, 4} with indices SOneTo(2)×SOneTo(2):
1.0 0.0
0.0 1.0
julia> state = coherentstate(SVector{2, Float32}, SMatrix{2,2, Float32}, QuadPairBasis(1), 1.0-im)
GaussianState for 1 mode.
symplectic basis: QuadPairBasis
mean: 2-element SVector{2, Float32} with indices SOneTo(2):
2.0
-2.0
covariance: 2×2 SMatrix{2, 2, Float32, 4} with indices SOneTo(2)×SOneTo(2):
1.0 0.0
0.0 1.0 |
Hi @Fe-r-oz, sorry for the late response. I've been thinking about this implementation and a cleaner way of doing it. What we could do, rather than inserting the
which simply defines a more narrow dispatch for
To me, this seems like a cleaner and more Julian approach, as it's just carefully implementing multiple dispatch and not adding additional utility functions. Let me know your thoughts! |
Hi, Andrew! Thank you for your insights. The latest approach that you suggested looks much cleaner and simpler than previous over-cooked approaches. I am excited to follow this approach! |
This PR aims to resolve #39 to provide cleaner dispatching onto StaticArrays. I hope that I have correctly followed this wonderful comment: #39 (comment)!
Edit: Dear Andrew, Thank you for your helpful and wonderful feedback 🙂 I really appreciate your insights. I wasn’t entirely sure whether you would prefer trait-based dispatch over type-based dispatch, but I found that traits provided a simpler and flexible solution. I would to be happy to hear your thoughts and criticism :) As mentioned here, type-based dispatch was causing complications such as method overwriting, code repetition, and unexpected errors for me! 😅 Dispatch on Traits, on the other hand, allowed for a neater approach by defining properties that types can have and dispatching accordingly. Additionally, we can easily extend this approach to PDMats as mentioned here. 😃
Outputs: 😃
It was a wonderful learning experience for me. It helped me realize that dispatch on traits avoid code duplication as shown here and cleanly handle orthogonal properties (being a
StaticArray
,Array
, orPDMat
etc.), while dispatch on types requires adding new methods for each types that we add in future as well. I will ponder on why dispatch on types causes mistakes on my end 😅 Silly me...I truly appreciate your guidance. I look forward to hearing your thoughts and criticism on which approach you feel would be best for the future! I may have overlooked some aspects or not fully considered the long-term implications of each approach 😅. That said, I truly enjoyed working on this! My apologies if I made any silly mistakes! Thank you! 🙂