Skip to content

Commit 61813ca

Browse files
committed
Complete element setter/getter functional API for all GraphBLAS types
Previously matrix.py, vector.py, and scalar.py only exposed `set_bool` and `bool` functions for accessing elements, even though SuiteSparse:GraphBLAS supports many more types. This fills out the functional API to cover every type the C library supports, following the existing `set_bool` / `bool` pattern exactly. Adds setter/getter pairs for: int8, int16, int32, int64, uint8, uint16, uint32, uint64, fp32, fp64, fc32, fc64. The complex types (fc32, fc64) are gated on `supports_complex()` so the functions are only defined on builds compiled with SuiteSparse complex support enabled (fc32/fc64 are GxB extensions, and the standard suitesparse.sh build disables them via GxB_NO_FC32 / GxB_NO_FC64). Each new function carries a doctest that round-trips a value through its setter and getter. Doctest counts after this change: matrix: 97 attempted, 0 failed vector: 90 attempted, 0 failed scalar: 81 attempted, 0 failed total: 268 attempted, 0 failed
1 parent 292dc06 commit 61813ca

3 files changed

Lines changed: 960 additions & 3 deletions

File tree

suitesparse_graphblas/matrix.py

Lines changed: 312 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from suitesparse_graphblas import check_status, ffi, lib
1+
from suitesparse_graphblas import check_status, ffi, lib, supports_complex
22

33
from .io.serialize import deserialize_matrix as deserialize # noqa: F401
44
from .io.serialize import serialize_matrix as serialize # noqa: F401
@@ -200,3 +200,314 @@ def bool(A, i, j):
200200
value = ffi.new("bool*")
201201
check_status(A, lib.GrB_Matrix_extractElement_BOOL(value, A[0], i, j))
202202
return value[0]
203+
204+
205+
def set_int8(A, value, i, j):
206+
"""Set an int8 value to the matrix at row `i` column `j`.
207+
208+
>>> A = new(lib.GrB_INT8, 3, 3)
209+
>>> set_int8(A, 7, 2, 2)
210+
>>> int8(A, 2, 2) == 7
211+
True
212+
213+
"""
214+
check_status(A, lib.GrB_Matrix_setElement_INT8(A[0], value, i, j))
215+
216+
217+
def int8(A, i, j):
218+
"""Get an int8 value from the matrix at row `i` column `j`.
219+
220+
>>> A = new(lib.GrB_INT8, 3, 3)
221+
>>> set_int8(A, 7, 2, 2)
222+
>>> int8(A, 2, 2) == 7
223+
True
224+
225+
"""
226+
value = ffi.new("int8_t*")
227+
check_status(A, lib.GrB_Matrix_extractElement_INT8(value, A[0], i, j))
228+
return value[0]
229+
230+
231+
def set_int16(A, value, i, j):
232+
"""Set an int16 value to the matrix at row `i` column `j`.
233+
234+
>>> A = new(lib.GrB_INT16, 3, 3)
235+
>>> set_int16(A, 7, 2, 2)
236+
>>> int16(A, 2, 2) == 7
237+
True
238+
239+
"""
240+
check_status(A, lib.GrB_Matrix_setElement_INT16(A[0], value, i, j))
241+
242+
243+
def int16(A, i, j):
244+
"""Get an int16 value from the matrix at row `i` column `j`.
245+
246+
>>> A = new(lib.GrB_INT16, 3, 3)
247+
>>> set_int16(A, 7, 2, 2)
248+
>>> int16(A, 2, 2) == 7
249+
True
250+
251+
"""
252+
value = ffi.new("int16_t*")
253+
check_status(A, lib.GrB_Matrix_extractElement_INT16(value, A[0], i, j))
254+
return value[0]
255+
256+
257+
def set_int32(A, value, i, j):
258+
"""Set an int32 value to the matrix at row `i` column `j`.
259+
260+
>>> A = new(lib.GrB_INT32, 3, 3)
261+
>>> set_int32(A, 7, 2, 2)
262+
>>> int32(A, 2, 2) == 7
263+
True
264+
265+
"""
266+
check_status(A, lib.GrB_Matrix_setElement_INT32(A[0], value, i, j))
267+
268+
269+
def int32(A, i, j):
270+
"""Get an int32 value from the matrix at row `i` column `j`.
271+
272+
>>> A = new(lib.GrB_INT32, 3, 3)
273+
>>> set_int32(A, 7, 2, 2)
274+
>>> int32(A, 2, 2) == 7
275+
True
276+
277+
"""
278+
value = ffi.new("int32_t*")
279+
check_status(A, lib.GrB_Matrix_extractElement_INT32(value, A[0], i, j))
280+
return value[0]
281+
282+
283+
def set_int64(A, value, i, j):
284+
"""Set an int64 value to the matrix at row `i` column `j`.
285+
286+
>>> A = new(lib.GrB_INT64, 3, 3)
287+
>>> set_int64(A, 7, 2, 2)
288+
>>> int64(A, 2, 2) == 7
289+
True
290+
291+
"""
292+
check_status(A, lib.GrB_Matrix_setElement_INT64(A[0], value, i, j))
293+
294+
295+
def int64(A, i, j):
296+
"""Get an int64 value from the matrix at row `i` column `j`.
297+
298+
>>> A = new(lib.GrB_INT64, 3, 3)
299+
>>> set_int64(A, 7, 2, 2)
300+
>>> int64(A, 2, 2) == 7
301+
True
302+
303+
"""
304+
value = ffi.new("int64_t*")
305+
check_status(A, lib.GrB_Matrix_extractElement_INT64(value, A[0], i, j))
306+
return value[0]
307+
308+
309+
def set_uint8(A, value, i, j):
310+
"""Set a uint8 value to the matrix at row `i` column `j`.
311+
312+
>>> A = new(lib.GrB_UINT8, 3, 3)
313+
>>> set_uint8(A, 7, 2, 2)
314+
>>> uint8(A, 2, 2) == 7
315+
True
316+
317+
"""
318+
check_status(A, lib.GrB_Matrix_setElement_UINT8(A[0], value, i, j))
319+
320+
321+
def uint8(A, i, j):
322+
"""Get a uint8 value from the matrix at row `i` column `j`.
323+
324+
>>> A = new(lib.GrB_UINT8, 3, 3)
325+
>>> set_uint8(A, 7, 2, 2)
326+
>>> uint8(A, 2, 2) == 7
327+
True
328+
329+
"""
330+
value = ffi.new("uint8_t*")
331+
check_status(A, lib.GrB_Matrix_extractElement_UINT8(value, A[0], i, j))
332+
return value[0]
333+
334+
335+
def set_uint16(A, value, i, j):
336+
"""Set a uint16 value to the matrix at row `i` column `j`.
337+
338+
>>> A = new(lib.GrB_UINT16, 3, 3)
339+
>>> set_uint16(A, 7, 2, 2)
340+
>>> uint16(A, 2, 2) == 7
341+
True
342+
343+
"""
344+
check_status(A, lib.GrB_Matrix_setElement_UINT16(A[0], value, i, j))
345+
346+
347+
def uint16(A, i, j):
348+
"""Get a uint16 value from the matrix at row `i` column `j`.
349+
350+
>>> A = new(lib.GrB_UINT16, 3, 3)
351+
>>> set_uint16(A, 7, 2, 2)
352+
>>> uint16(A, 2, 2) == 7
353+
True
354+
355+
"""
356+
value = ffi.new("uint16_t*")
357+
check_status(A, lib.GrB_Matrix_extractElement_UINT16(value, A[0], i, j))
358+
return value[0]
359+
360+
361+
def set_uint32(A, value, i, j):
362+
"""Set a uint32 value to the matrix at row `i` column `j`.
363+
364+
>>> A = new(lib.GrB_UINT32, 3, 3)
365+
>>> set_uint32(A, 7, 2, 2)
366+
>>> uint32(A, 2, 2) == 7
367+
True
368+
369+
"""
370+
check_status(A, lib.GrB_Matrix_setElement_UINT32(A[0], value, i, j))
371+
372+
373+
def uint32(A, i, j):
374+
"""Get a uint32 value from the matrix at row `i` column `j`.
375+
376+
>>> A = new(lib.GrB_UINT32, 3, 3)
377+
>>> set_uint32(A, 7, 2, 2)
378+
>>> uint32(A, 2, 2) == 7
379+
True
380+
381+
"""
382+
value = ffi.new("uint32_t*")
383+
check_status(A, lib.GrB_Matrix_extractElement_UINT32(value, A[0], i, j))
384+
return value[0]
385+
386+
387+
def set_uint64(A, value, i, j):
388+
"""Set a uint64 value to the matrix at row `i` column `j`.
389+
390+
>>> A = new(lib.GrB_UINT64, 3, 3)
391+
>>> set_uint64(A, 7, 2, 2)
392+
>>> uint64(A, 2, 2) == 7
393+
True
394+
395+
"""
396+
check_status(A, lib.GrB_Matrix_setElement_UINT64(A[0], value, i, j))
397+
398+
399+
def uint64(A, i, j):
400+
"""Get a uint64 value from the matrix at row `i` column `j`.
401+
402+
>>> A = new(lib.GrB_UINT64, 3, 3)
403+
>>> set_uint64(A, 7, 2, 2)
404+
>>> uint64(A, 2, 2) == 7
405+
True
406+
407+
"""
408+
value = ffi.new("uint64_t*")
409+
check_status(A, lib.GrB_Matrix_extractElement_UINT64(value, A[0], i, j))
410+
return value[0]
411+
412+
413+
def set_fp32(A, value, i, j):
414+
"""Set an fp32 value to the matrix at row `i` column `j`.
415+
416+
>>> A = new(lib.GrB_FP32, 3, 3)
417+
>>> set_fp32(A, 1.5, 2, 2)
418+
>>> fp32(A, 2, 2) == 1.5
419+
True
420+
421+
"""
422+
check_status(A, lib.GrB_Matrix_setElement_FP32(A[0], value, i, j))
423+
424+
425+
def fp32(A, i, j):
426+
"""Get an fp32 value from the matrix at row `i` column `j`.
427+
428+
>>> A = new(lib.GrB_FP32, 3, 3)
429+
>>> set_fp32(A, 1.5, 2, 2)
430+
>>> fp32(A, 2, 2) == 1.5
431+
True
432+
433+
"""
434+
value = ffi.new("float*")
435+
check_status(A, lib.GrB_Matrix_extractElement_FP32(value, A[0], i, j))
436+
return value[0]
437+
438+
439+
def set_fp64(A, value, i, j):
440+
"""Set an fp64 value to the matrix at row `i` column `j`.
441+
442+
>>> A = new(lib.GrB_FP64, 3, 3)
443+
>>> set_fp64(A, 1.5, 2, 2)
444+
>>> fp64(A, 2, 2) == 1.5
445+
True
446+
447+
"""
448+
check_status(A, lib.GrB_Matrix_setElement_FP64(A[0], value, i, j))
449+
450+
451+
def fp64(A, i, j):
452+
"""Get an fp64 value from the matrix at row `i` column `j`.
453+
454+
>>> A = new(lib.GrB_FP64, 3, 3)
455+
>>> set_fp64(A, 1.5, 2, 2)
456+
>>> fp64(A, 2, 2) == 1.5
457+
True
458+
459+
"""
460+
value = ffi.new("double*")
461+
check_status(A, lib.GrB_Matrix_extractElement_FP64(value, A[0], i, j))
462+
return value[0]
463+
464+
465+
if supports_complex():
466+
467+
def set_fc32(A, value, i, j):
468+
"""Set an fc32 value to the matrix at row `i` column `j`.
469+
470+
>>> A = new(lib.GxB_FC32, 3, 3)
471+
>>> set_fc32(A, 2+3j, 2, 2)
472+
>>> fc32(A, 2, 2) == 2+3j
473+
True
474+
475+
"""
476+
check_status(A, lib.GxB_Matrix_setElement_FC32(A[0], value, i, j))
477+
478+
def fc32(A, i, j):
479+
"""Get an fc32 value from the matrix at row `i` column `j`.
480+
481+
>>> A = new(lib.GxB_FC32, 3, 3)
482+
>>> set_fc32(A, 2+3j, 2, 2)
483+
>>> fc32(A, 2, 2) == 2+3j
484+
True
485+
486+
"""
487+
value = ffi.new("GxB_FC32_t*")
488+
check_status(A, lib.GxB_Matrix_extractElement_FC32(value, A[0], i, j))
489+
return value[0]
490+
491+
def set_fc64(A, value, i, j):
492+
"""Set an fc64 value to the matrix at row `i` column `j`.
493+
494+
>>> A = new(lib.GxB_FC64, 3, 3)
495+
>>> set_fc64(A, 2+3j, 2, 2)
496+
>>> fc64(A, 2, 2) == 2+3j
497+
True
498+
499+
"""
500+
check_status(A, lib.GxB_Matrix_setElement_FC64(A[0], value, i, j))
501+
502+
def fc64(A, i, j):
503+
"""Get an fc64 value from the matrix at row `i` column `j`.
504+
505+
>>> A = new(lib.GxB_FC64, 3, 3)
506+
>>> set_fc64(A, 2+3j, 2, 2)
507+
>>> fc64(A, 2, 2) == 2+3j
508+
True
509+
510+
"""
511+
value = ffi.new("GxB_FC64_t*")
512+
check_status(A, lib.GxB_Matrix_extractElement_FC64(value, A[0], i, j))
513+
return value[0]

0 commit comments

Comments
 (0)