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

__add__ and __iadd__ #102

Merged
merged 1 commit into from
Jul 13, 2023
Merged

__add__ and __iadd__ #102

merged 1 commit into from
Jul 13, 2023

Conversation

robertodr
Copy link
Contributor

Another possible fix for #101.

Context

In principle __add__ and __iadd__ should do the exact same thing, so in C++ one would overload operator+ and then overload operator+= using operator+. Things should be done similarly in practice. Up to now the two functions where using different implementations.

Possible solutions

1: Fix the bug in the implementation of __iadd__

Even though we can make the two different implementations do the exact same thing, I'd still argue that the strategy is un-intuitive and possibly bug-prone.

2: Implement __add__ in the bindings and attach __iadd__ dynamically to the class in Python

The strategy is:

  • Remove __iadd__ from trees.h.
  • Add a trees.py file in src/vampyr containing:
from ._vampyr.vampyr3d import FunctionTree

def _impl__iadd__(...):
    ....

FunctionTree.__iadd__ = _impl__iadd__
  • Modify __init__.py like so:
from .trees import FunctionTree

I tried this, but it actually adds more code complexity Python-side! Part of the reason is that Python does not have a notion of templates, so attaching a method to FunctionTree has to be done once per value of D (3 times as things stand now) And actually instead of a single trees.py this needs to be done in 3 separate Python files plus the re-export in __init__.py loses all notions of namespacing by dimension (vampyr1d, vampyr2d, vampyr3d) I think this is hard to get right.

3: Implement __add__ as a free function instead of a lambda and re-use it for __iadd__

This is what I ended up doing in this PR.

@robertodr
Copy link
Contributor Author

The diff is a bit too large between this branch and master, even after rebasing 🤷🏻

@codecov
Copy link

codecov bot commented Jul 13, 2023

Codecov Report

Patch coverage has no change and project coverage change: +8.49 🎉

Comparison is base (bbe2319) 82.41% compared to head (8d44a79) 90.90%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #102      +/-   ##
==========================================
+ Coverage   82.41%   90.90%   +8.49%     
==========================================
  Files          22        3      -19     
  Lines         910      187     -723     
==========================================
- Hits          750      170     -580     
+ Misses        160       17     -143     

see 19 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@bjorgve bjorgve left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks, Roberto!

@bjorgve bjorgve merged commit 9e2ac3b into MRChemSoft:master Jul 13, 2023
15 checks passed
@robertodr robertodr deleted the fix-101 branch July 14, 2023 06:59
@robertodr
Copy link
Contributor Author

Note that all other overloaded operators should be double-checked (and in case re-implemented)

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.

2 participants