Skip to content

Commit

Permalink
feat: Support hist/hist division if view supports it
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jul 6, 2020
1 parent 7352ab0 commit f55a6a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,35 @@ def __imul__(self, other):
return self

def __truediv__(self, other):
return self.__class__(self._hist.__truediv__(_hist_or_val(other)))
if isinstance(other, BaseHistogram):
result = self.copy()
result.__itruediv__(other)
return result
else:
return self.__class__(self._hist.__truediv__(_hist_or_val(other)))

def __div__(self, other):
return self.__class__(self._hist.__div__(_hist_or_val(other)))
if isinstance(other, BaseHistogram):
result = self.copy()
result.__idiv__(other)
return result
else:
return self.__class__(self._hist.__div__(_hist_or_val(other)))

def __itruediv__(self, other):
self._hist.__itruediv__(_hist_or_val(other))
if isinstance(other, BaseHistogram):
view = self.view(flow=True)
view.__itruediv__(other.view(flow=True))
else:
self._hist.__itruediv__(_hist_or_val(other))
return self

def __idiv__(self, other):
self._hist.__idiv__(_hist_or_val(other))
if isinstance(other, BaseHistogram):
view = self.view(flow=True)
view.__idiv__(other.view(flow=True))
else:
self._hist.__idiv__(_hist_or_val(other))
return self

def __copy__(self):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ def test_operators():
h + h2


def test_hist_hist_div():
h1 = bh.Histogram(bh.axis.Boolean())
h2 = bh.Histogram(bh.axis.Boolean())

h1[:] = (8, 6)
h2[:] = (2, 3)

h3 = h1 / h2

assert h3[False] == 4
assert h3[True] == 2

h1 /= h2

assert h1[False] == 4
assert h1[True] == 2


def test_project():
h = bh.Histogram(bh.axis.Integer(0, 2), bh.axis.Integer(1, 4))
h.fill(0, 1)
Expand Down

0 comments on commit f55a6a9

Please sign in to comment.