Skip to content

Commit

Permalink
Merge pull request #742 from pyiron/sparse
Browse files Browse the repository at this point in the history
Avoid excessive memory allocation when repeating SparseList
  • Loading branch information
pmrv authored Sep 3, 2022
2 parents 1f0a812 + 7285950 commit 9b6b2a1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pyiron_atomistics/atomistics/structure/sparse_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,13 @@ def __add__(self, other):
def __mul__(self, other):
if not isinstance(other, Integral):
raise ValueError("Multiplication defined only for SparseArray*integers")
overall_list = other * np.arange(len(self)).tolist()
new_dic = dict()
for k in self.keys():
for val in np.argwhere(np.array(overall_list) == k).flatten():
new_dic[val] = self[k]
return self.__class__(new_dic, default=self._default, length=other * len(self))
new_dict = {}
length = len(self)
for rep in range(other):
offset = rep * length
for k in self.keys():
new_dict[k + offset] = self[k]
return self.__class__(new_dict, default=self._default, length=other * length)

def __rmul__(self, other):
if isinstance(other, Integral):
Expand Down

0 comments on commit 9b6b2a1

Please sign in to comment.