/
matrix_funcs.py
212 lines (163 loc) · 6.59 KB
/
matrix_funcs.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import numpy as np
from scipy import sparse
from top_n_funcs import convert_matrix_to_sparse_with_top_n
def create_matrix(rows, cols, percent_zeros=0.99):
"""
Creates a random matrix with a defined percentage of sparsity.
The matrix contains only zeros and ones.
Parameters
----------
rows: number of rows in the matrix
cols: number of columns in the matrix
percent_zeros: percentage of zeros in the matrix
Returns numpy 2-dim matrix
-------
"""
if percent_zeros < 0 or percent_zeros > 1:
raise ValueError
if rows <= 0 or cols <= 0:
raise ValueError
matrix = np.ones([rows,cols], dtype=np.int)
desired_ones_count = int(rows*cols*percent_zeros)
ones_count = 0
while ones_count < desired_ones_count:
row = np.random.random_integers(0,rows-1)
col = np.random.random_integers(0,cols-1)
if matrix[row][col]==1 :
matrix[row][col] = 0
ones_count+=1
return matrix
def dot_numpy(matrix_1: np.ndarray, matrix_2: np.ndarray):
"""
Calculates the dot product using numpy
Parameters
----------
matrix_1: numpy-array
matrix_2: numpy-array
Returns: a numpy-array which results from the dot product
-------
"""
return np.dot(matrix_1, matrix_2)
def dot_scipy_csc_with_conversion(matrix_1: np.ndarray, matrix_2: np.ndarray):
"""
Calculates the dot product by converting the parameters to compressed Sparse Column matrices
Parameters
----------
matrix_1: numpy-array
matrix_2: numpy-array
Returns: a numpy-array which results from the dot product
-------
"""
sparse_result = sparse.csc_matrix(matrix_1).dot(sparse.csc_matrix(matrix_2))
return np.array(sparse_result.todense())
def dot_scipy_bsr_with_conversion(matrix_1: np.ndarray, matrix_2: np.ndarray):
"""
Calculates the dot product by converting the parameters to Block Sparse Row matrices
Parameters
----------
matrix_1: numpy-array
matrix_2: numpy-array
Returns: a numpy-array which results from the dot product
-------
"""
sparse_result = sparse.bsr_matrix(matrix_1).dot(sparse.bsr_matrix(matrix_2))
return np.array(sparse_result.todense())
def dot_scipy_csr_with_conversion(matrix_1: np.ndarray, matrix_2: np.ndarray):
"""
Calculates the dot product by converting the parameters to Compressed Sparse Row sparse matrices
Parameters
----------
matrix_1: numpy-array
matrix_2: numpy-array
Returns: a numpy-array which results from the dot product
-------
"""
sparse_result = sparse.csr_matrix(matrix_1).dot(sparse.csr_matrix(matrix_2))
return np.array(sparse_result.todense())
def scipy_csc_dot_numpy_with_swap(matrix_dense: np.ndarray, matrix_sparse: np.ndarray):
"""
Calculates the dot product of two numpy arrays. The matrices are converted to CSC format for fast
multiplication.
Parameters
----------
matrix_dense - the first array
matrix_sparse - the second array.
Returns a numpy array, which is the result of the matrix multiplication.
-------
"""
result = sparse.csc_matrix(matrix_sparse.T).dot(matrix_dense.T)
return result.T
def scipy_csr_dot_numpy_with_swap(dense_matrix: np.ndarray, sparse_matrix: np.ndarray):
"""
Calculates the dot product of two numpy arrays. The matrices are converted to CSR format for fast
multiplication.
Parameters
----------
matrix_dense - the first array
matrix_sparse - the second array.
Returns a numpy array, which is the result of the matrix multiplication.
-------
"""
result = sparse.csr_matrix(sparse_matrix.T).dot(dense_matrix.T)
return result.T
def scipy_bsr_dot_numpy_with_swap(dense_matrix: np.ndarray, sparse_matrix: np.ndarray):
"""
Calculates the dot product of two numpy arrays. The matrices are converted to BSR format for fast
multiplication.
Parameters
----------
matrix_dense - the first array
matrix_sparse - the second array.
Returns a numpy array, which is the result of the matrix multiplication.
-------
"""
result = sparse.bsr_matrix(sparse_matrix.T).dot(dense_matrix.T)
return result.T
def scipy_csc_dot_numpy_with_top_n(dense_matrix: np.ndarray, sparse_matrix: np.ndarray, n=20):
"""
Calculates the dot product of two Matrices of type numpy array. The first array is convert to a sparse matrix with
top N items in every row. Afterwards both matrices are converted to Sparse matrices from type CSC for
fast multiplication.
Parameters
----------
dense_matrix - The first matrix, which will be converted to a top-n matrix
sparse_matrix - the second matrix
n = the n value for the top n matrix.
Returns a numpy array, which is the result of the matrix multiplication.
-------
"""
convert_matrix_to_sparse_with_top_n(dense_matrix, n)
result = sparse.csc_matrix(dense_matrix).dot(sparse.csc_matrix(sparse_matrix))
return np.array(result.todense())
def scipy_csr_dot_numpy_with_top_n(dense_matrix: np.ndarray, sparse_matrix: np.ndarray, n=20):
"""
Calculates the dot product of two Matrices of type numpy array. The first array is convert to a sparse matrix with
top N items in every row. Afterwards both matrices are converted to Sparse matrices from type CSR for
fast multiplication.
Parameters
----------
dense_matrix - The first matrix, which will be converted to a top-n matrix
sparse_matrix - the second matrix
n = the n value for the top n matrix.
Returns a numpy array, which is the result of the matrix multiplication.
-------
"""
convert_matrix_to_sparse_with_top_n(dense_matrix, n)
result = sparse.csr_matrix(dense_matrix).dot(sparse.csr_matrix(sparse_matrix))
return np.array(result.todense())
def scipy_bsr_dot_numpy_with_top_n(dense_matrix: np.ndarray, sparse_matrix: np.ndarray, n=20):
"""
Calculates the dot product of two Matrices of type numpy array. The first array is convert to a sparse matrix with
top N items in every row. Afterwards both matrices are converted to Sparse matrices from type BSR for
fast multiplication.
Parameters
----------
dense_matrix - The first matrix, which will be converted to a top-n matrix
sparse_matrix - the second matrix
n = the n value for the top n matrix.
Returns a numpy array, which is the result of the matrix multiplication.
-------
"""
convert_matrix_to_sparse_with_top_n(dense_matrix, n)
result = sparse.bsr_matrix(dense_matrix).dot(sparse.bsr_matrix(sparse_matrix))
return np.array(result.todense())