Skip to content

Commit 6d15abe

Browse files
authored
fix #705: made LArray.divnot0 faster when the divisor array has many axes and many zeros (#708)
fix #705: made LArray.divnot0 faster when the divisor array has many axes and many zeros
1 parent e7b93e7 commit 6d15abe

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ Miscellaneous improvements
157157
Fixes
158158
-----
159159

160-
* fixed something (closes :issue:`1`).
160+
* fixed LArray.divnot0 being slow when the divisor has many axes and many zeros (closes :issue:`705`).

larray/core/array.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5420,10 +5420,16 @@ def divnot0(self, other):
54205420
else:
54215421
return self / other
54225422
else:
5423-
with np.errstate(divide='ignore', invalid='ignore'):
5424-
res = self / other
5425-
res[other == 0] = 0
5426-
return res
5423+
(self, other), res_axes = make_numpy_broadcastable((self, other))
5424+
otherdata = other.data
5425+
other_eq0 = otherdata == 0
5426+
# numpy array division gets slower the more zeros you have in other, so we change it before the division
5427+
# happens. This is obviously slower than doing nothing if we have very few zeros but I think it's a win
5428+
# on average given that other is likely to contain zeros when using divnot0.
5429+
otherdata = np.where(other_eq0, 1, otherdata)
5430+
res_data = self.data / otherdata
5431+
res_data[np.broadcast_to(other_eq0, res_data.shape)] = 0.0
5432+
return LArray(res_data, res_axes)
54275433

54285434
# XXX: rename/change to "add_axes" ?
54295435
# TODO: add a flag copy=True to force a new array.

0 commit comments

Comments
 (0)