Skip to content

Commit

Permalink
feat: Allow addition of exactly 0
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jul 10, 2020
1 parent 7427940 commit ceda8f3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ compile_commands.json
# Testing
/.benchmarks/*

# IDEs
/.idea
# PyCharm
.idea
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ message(STATUS "boost-histogram ${BOOST_HISTOGRAM_VERSION}")
if(BUILD_TESTING)
python_import(numpy pytest pytest-benchmark six typing)
python_import(numpy pytest pytest-benchmark typing)
# Support for running from build directory
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
Expand Down
21 changes: 17 additions & 4 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,25 @@ def __array__(self):
return self.view(False)

def __add__(self, other):
return self.__class__(self._hist.__add__(other._hist))
if hasattr(other, "_hist"):
return self.__class__(self._hist.__add__(other._hist))
else:
retval = self.copy()
retval += other
return retval

def __iadd__(self, other):
self._hist.__iadd__(other._hist)
self.axes = self._generate_axes_()
return self
if isinstance(other, (int, float)) and other == 0:
return self
if hasattr(other, "_hist"):
self._hist.__iadd__(other._hist)
# Addition may change category axes
self.axes = self._generate_axes_()
return self
return NotImplemented

def __radd__(self, other):
return self + other

def __eq__(self, other):
return self._hist == other._hist
Expand Down
41 changes: 41 additions & 0 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,47 @@ def test_fill_2d(flow):


def test_add_2d(flow):
h0 = bh.Histogram(
bh.axis.Integer(-1, 2, underflow=flow, overflow=flow),
bh.axis.Regular(4, -2, 2, underflow=flow, overflow=flow),
)
assert isinstance(h0, bh.Histogram)

h0.fill(-1, -2)
h0.fill(-1, -1)
h0.fill(0, 0)
h0.fill(0, 1)
h0.fill(1, 0)
h0.fill(3, -1)
h0.fill(0, -3)

m = [
[1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
]

h = h0.copy()
h += h
for i in range(-flow, h.axes[0].size + flow):
for j in range(-flow, h.axes[1].size + flow):
assert h[bh.tag.at(i), bh.tag.at(j)] == 2 * m[i][j]

h = sum([h0, h0])
for i in range(-flow, h.axes[0].size + flow):
for j in range(-flow, h.axes[1].size + flow):
assert h[bh.tag.at(i), bh.tag.at(j)] == 2 * m[i][j]

h = 0 + h0 + h0
for i in range(-flow, h.axes[0].size + flow):
for j in range(-flow, h.axes[1].size + flow):
assert h[bh.tag.at(i), bh.tag.at(j)] == 2 * m[i][j]


@pytest.mark.parametrize("flow", [True, False])
def test_add_2d_fancy(flow):
h = bh.Histogram(
bh.axis.Integer(-1, 2, underflow=flow, overflow=flow),
bh.axis.Regular(4, -2, 2, underflow=flow, overflow=flow),
Expand Down

0 comments on commit ceda8f3

Please sign in to comment.