Replies: 1 comment 3 replies
-
import functools
import operator
import numpy as np
from dataclasses import dataclass
m = [[0, 1, 2], [0, 1, 2, 3, 4], [4, 5, 6], [0, 4, 5, 6, 7, 8]]
@dataclass
class PolyList(list):
array: list
def get_max_dims(self):
""" return num rows and length of longest row """
return len(self.array), max(map(len, self.array))
@property
def loops(self):
flat = np.array(functools.reduce(operator.iconcat, self.array, []))
start_indices, end_indices = [], []
add_start, add_end = start_indices.append, end_indices.append
sum_state = 0
for row in self.array:
add_start(sum_state)
sum_state = sum_state + len(row)
add_end(sum_state)
return flat, np.vstack((start_indices, end_indices)).T
@property
def sparse_matrix(self):
f = self.get_max_dims()
M = np.full(f, np.nan)
for idx, x in enumerate(M):
rsize = len(m[idx])
M[idx][:rsize] = m[idx]
return M
mpl = PolyList(m)
prin(mpl.sparse_matrix)
flatlist, starts_ends = mpl.loops
prin(starts_ends)
for idx_range in np.nditer(starts_ends.T, flags=['external_loop'], order='F'):
prin(flatlist[idx_range[0]:idx_range[1]]) shows [[ 0. 1. 2. nan nan nan]
[ 0. 1. 2. 3. 4. nan]
[ 4. 5. 6. nan nan nan]
[ 0. 4. 5. 6. 7. 8.]]
[[ 0 3]
[ 3 8]
[ 8 11]
[11 17]]
[0 1 2]
[0 1 2 3 4]
[4 5 6]
[0 4 5 6 7 8] the nan-padded matrix allows running |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
faux sparse might work for numba, not tried.
prints
Beta Was this translation helpful? Give feedback.
All reactions