forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse.py
More file actions
275 lines (206 loc) · 6.76 KB
/
sparse.py
File metadata and controls
275 lines (206 loc) · 6.76 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Functions to create and manipulate sparse matrices.
"""
from .library import *
from .array import *
import numbers
from .interop import to_array
__to_sparse_enum = [STORAGE.DENSE,
STORAGE.CSR,
STORAGE.CSC,
STORAGE.COO]
def create_sparse(values, row_idx, col_idx, nrows, ncols, storage = STORAGE.CSR):
"""
Create a sparse matrix from it's constituent parts.
Parameters
----------
values : af.Array.
- Contains the non zero elements of the sparse array.
row_idx : af.Array.
- Contains row indices of the sparse array.
col_idx : af.Array.
- Contains column indices of the sparse array.
nrows : int.
- specifies the number of rows in sparse matrix.
ncols : int.
- specifies the number of columns in sparse matrix.
storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
- Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.
Returns
-------
A sparse matrix.
"""
assert(isinstance(values, Array))
assert(isinstance(row_idx, Array))
assert(isinstance(col_idx, Array))
out = Array()
safe_call(backend.get().af_create_sparse_array(c_pointer(out.arr), c_dim_t(nrows), c_dim_t(ncols),
values.arr, row_idx.arr, col_idx.arr, storage.value))
return out
def create_sparse_from_host(values, row_idx, col_idx, nrows, ncols, storage = STORAGE.CSR):
"""
Create a sparse matrix from it's constituent parts.
Parameters
----------
values : Any datatype that can be converted to array.
- Contains the non zero elements of the sparse array.
row_idx : Any datatype that can be converted to array.
- Contains row indices of the sparse array.
col_idx : Any datatype that can be converted to array.
- Contains column indices of the sparse array.
nrows : int.
- specifies the number of rows in sparse matrix.
ncols : int.
- specifies the number of columns in sparse matrix.
storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
- Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.
Returns
-------
A sparse matrix.
"""
return create_sparse(to_array(values), to_array(row_idx), to_array(col_idx), nrows, ncols, storage)
def create_sparse_from_dense(dense, storage = STORAGE.CSR):
"""
Create a sparse matrix from a dense matrix.
Parameters
----------
dense : af.Array.
- A dense matrix.
storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
- Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.
Returns
-------
A sparse matrix.
"""
assert(isinstance(dense, Array))
out = Array()
safe_call(backend.get().af_create_sparse_array_from_dense(c_pointer(out.arr), dense.arr, storage.value))
return out
def convert_sparse_to_dense(sparse):
"""
Create a dense matrix from a sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
-------
A dense matrix.
"""
out = Array()
safe_call(backend.get().af_sparse_to_dense(c_pointer(out.arr), sparse.arr))
return out
def sparse_get_info(sparse):
"""
Get the constituent arrays and storage info from a sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
--------
(values, row_idx, col_idx, storage) where
values : arrayfire.Array containing non zero elements from sparse matrix
row_idx : arrayfire.Array containing the row indices
col_idx : arrayfire.Array containing the column indices
storage : sparse storage
"""
values = Array()
row_idx = Array()
col_idx = Array()
stype = c_int_t(0)
safe_call(backend.get().af_sparse_get_info(c_pointer(values.arr), c_pointer(row_idx.arr),
c_pointer(col_idx.arr), c_pointer(stype),
sparse.arr))
return (values, row_idx, col_idx, __to_sparse_enum[stype.value])
def sparse_get_values(sparse):
"""
Get the non zero values from sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
--------
arrayfire array containing the non zero elements.
"""
values = Array()
safe_call(backend.get().af_sparse_get_values(c_pointer(values.arr), sparse.arr))
return values
def sparse_get_row_idx(sparse):
"""
Get the row indices from sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
--------
arrayfire array containing the non zero elements.
"""
row_idx = Array()
safe_call(backend.get().af_sparse_get_row_idx(c_pointer(row_idx.arr), sparse.arr))
return row_idx
def sparse_get_col_idx(sparse):
"""
Get the column indices from sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
--------
arrayfire array containing the non zero elements.
"""
col_idx = Array()
safe_call(backend.get().af_sparse_get_col_idx(c_pointer(col_idx.arr), sparse.arr))
return col_idx
def sparse_get_nnz(sparse):
"""
Get the column indices from sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
--------
Number of non zero elements in the sparse matrix.
"""
nnz = c_dim_t(0)
safe_call(backend.get().af_sparse_get_nnz(c_pointer(nnz), sparse.arr))
return nnz.value
def sparse_get_storage(sparse):
"""
Get the column indices from sparse matrix.
Parameters
----------
sparse : af.Array.
- A sparse matrix.
Returns
--------
Number of non zero elements in the sparse matrix.
"""
storage = c_int_t(0)
safe_call(backend.get().af_sparse_get_storage(c_pointer(storage), sparse.arr))
return __to_sparse_enum[storage.value]
def convert_sparse(sparse, storage):
"""
Convert sparse matrix from one format to another.
Parameters
----------
storage : arrayfire.STORAGE.
Returns
-------
Sparse matrix converted to the appropriate type.
"""
out = Array()
safe_call(backend.get().af_sparse_convert_to(c_pointer(out.arr), sparse.arr, storage.value))
return out