-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrixVectorizer.py
More file actions
97 lines (79 loc) · 4.07 KB
/
MatrixVectorizer.py
File metadata and controls
97 lines (79 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
class MatrixVectorizer:
"""
A class for transforming between matrices and vector representations.
This class provides methods to convert a symmetric matrix into a vector (vectorize)
and to reconstruct the matrix from its vector form (anti_vectorize), focusing on
vertical (column-based) traversal and handling of elements.
"""
def __init__(self):
"""
Initializes the MatrixVectorizer instance.
The constructor currently does not perform any actions but is included for
potential future extensions where initialization parameters might be required.
"""
pass
@staticmethod
def vectorize(matrix, include_diagonal=False):
"""
Converts a matrix into a vector by vertically extracting elements.
This method traverses the matrix column by column, collecting elements from the
upper triangle, and optionally includes the diagonal elements immediately below
the main diagonal based on the include_diagonal flag.
Parameters:
- matrix (numpy.ndarray): The matrix to be vectorized.
- include_diagonal (bool, optional): Flag to include diagonal elements in the vectorization.
Defaults to False.
Returns:
- numpy.ndarray: The vectorized form of the matrix.
"""
# Determine the size of the matrix based on its first dimension
matrix_size = matrix.shape[0]
# Initialize an empty list to accumulate vector elements
vector_elements = []
# Iterate over columns and then rows to collect the relevant elements
for col in range(matrix_size):
for row in range(matrix_size):
# Skip diagonal elements if not including them
if row != col:
if row < col:
# Collect upper triangle elements
vector_elements.append(matrix[row, col])
elif include_diagonal and row == col + 1:
# Optionally include the diagonal elements immediately below the diagonal
vector_elements.append(matrix[row, col])
return np.array(vector_elements)
@staticmethod
def anti_vectorize(vector, matrix_size, include_diagonal=False):
"""
Reconstructs a matrix from its vector form, filling it vertically.
The method fills the matrix by reflecting vector elements into the upper triangle
and optionally including the diagonal elements based on the include_diagonal flag.
Parameters:
- vector (numpy.ndarray): The vector to be transformed into a matrix.
- matrix_size (int): The size of the square matrix to be reconstructed.
- include_diagonal (bool, optional): Flag to include diagonal elements in the reconstruction.
Defaults to False.
Returns:
- numpy.ndarray: The reconstructed square matrix.
"""
# Initialize a square matrix of zeros with the specified size
matrix = np.zeros((matrix_size, matrix_size))
# Index to keep track of the current position in the vector
vector_idx = 0
# Fill the matrix by iterating over columns and then rows
for col in range(matrix_size):
for row in range(matrix_size):
# Skip diagonal elements if not including them
if row != col:
if row < col:
# Reflect vector elements into the upper triangle and its mirror in the lower triangle
matrix[row, col] = vector[vector_idx]
matrix[col, row] = vector[vector_idx]
vector_idx += 1
elif include_diagonal and row == col + 1:
# Optionally fill the diagonal elements after completing each column
matrix[row, col] = vector[vector_idx]
matrix[col, row] = vector[vector_idx]
vector_idx += 1
return matrix