Skip to content

Commit

Permalink
Ensure index of sparse matrices is always stored as NumPy arrays.
Browse files Browse the repository at this point in the history
Previously, they could also be stored as plain lists, which would happen
whenever the COOMatrix objects was initialised with lists. Most operations
still worked ok then, but equality check means something completely
different for NumPy arrays, and ProductSpaceOperator's __getitem__
method relies on this behaviour to look up the desired linear index for
a requested element in the matrix of operators. If the index is a list,
this would give wrong results.
  • Loading branch information
leftaroundabout committed Aug 12, 2024
1 parent 29a64d5 commit 3b4a7db
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions odl/util/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Sparse matrix representation for creating product space operators.
"""

import numpy as np

__all__ = ('COOMatrix',)


Expand All @@ -27,18 +29,19 @@ def __init__(self, data, index, shape):
raise ValueError('data and index must have the same length')

self.__data = data
self.__index = index
self.__row_index = np.asarray(index[0])
self.__col_index = np.asarray(index[1])
self.__shape = shape

@property
def row(self):
"""Return the row indices of the matrix."""
return self.__index[0]
return self.__row_index

@property
def col(self):
"""Return the column indices of the matrix."""
return self.__index[1]
return self.__col_index

@property
def shape(self):
Expand All @@ -51,4 +54,5 @@ def data(self):
return self.__data

def __repr__(self):
return f"COO matrix({self.data}, {self.__index}, {self.shape})"
return ( f"COO matrix({self.data},"
+ f"({self.__row_index}, {self.__col_index}), {self.shape})" )

0 comments on commit 3b4a7db

Please sign in to comment.