Skip to content

Fill NxM matrices #35

@ulf1

Description

@ulf1

Example

import sparsity_pattern
import torch

# Erzeuge 
n_row_segments, n_col_segments = 5, 3#9

# circular pattern
block_idx = sparsity_pattern.get(
    'circle', 
    min(n_row_segments, n_col_segments), 
    offsets=[1, 2])

# fill the 
i_start = max(0, 1 + n_row_segments - n_col_segments)
j_start = max(0, 1 + n_col_segments - n_row_segments)
print(i_start, j_start)

num_blocks = max((n_row_segments - i_start), (n_col_segments - j_start))
result = []
i, j = i_start, j_start
while len(result) < num_blocks:
    result.append((i, j))
    i = i + 1
    if i >= n_row_segments:
        i = i_start
    j = j + 1
    if j >= n_col_segments:
        j = j_start

print(result)

block_idx = block_idx + result

mat = torch.sparse_coo_tensor(
    indices=torch.tensor(block_idx).transpose(0, 1), 
    values=range(1, len(block_idx)+1),
    size=[n_row_segments, n_col_segments])

print(mat.to_dense())

block_idx
``

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions