def new(T, nrows=lib.GxB_INDEX_MAX, ncols=lib.GxB_INDEX_MAX, *, free=free): """Create a new `GrB_Matrix` of type `T` and initialize it. The following example creates an eight bit unsigned 2x2 matrix: >>> A = new(lib.GrB_UINT8, 2, 2) >>> shape(A) (2, 2) The default value for `nrows` and `ncols` is `lib.GxB_INDEX_MAX` which creates a Matrix with maximal bounds: >>> A = new(lib.GrB_UINT8) >>> shape(A) == (lib.GxB_INDEX_MAX, lib.GxB_INDEX_MAX) True The `free` argument is called when the object is garbage collected, the default is `matrix.free()`. If `free` is None then there is no automatic garbage collection and it is up to the user to free the matrix. """ A = ffi.new("GrB_Matrix*") check_status(A, lib.GrB_Matrix_new(A, T, nrows, ncols)) if free: return ffi.gc(A, free) return A
def set_bool(v, value, i): """Set a boolean value to the vector at position `i`. >>> v = new(lib.GrB_BOOL, 3) >>> set_bool(v, True, 2) >>> bool(v, 2) == True True """ check_status(v, lib.GrB_Vector_setElement_BOOL(v[0], value, i))
def set_bool(A, value, i, j): """Set a boolean value to the matrix at row `i` column `j`. >>> A = new(lib.GrB_BOOL, 3, 3) >>> set_bool(A, True, 2, 2) >>> bool(A, 2, 2) == True True """ check_status(A, lib.GrB_Matrix_setElement_BOOL(A[0], value, i, j))
def set_bool(s, value): """Set a boolean value to the scalar. >>> s = new(lib.GrB_BOOL) >>> set_bool(s, True) >>> bool(s) == True True """ check_status(s, lib.GxB_Scalar_setElement_BOOL(s[0], value))
def size(v): """Return the size of the vector. >>> v = new(lib.GrB_UINT8, 2) >>> size(v) == 2 True """ n = ffi.new("GrB_Index*") check_status(v, lib.GrB_Vector_size(n, v[0])) return n[0]
def nvals(A): """Return the number of stored elements in the matrix. >>> A = new(lib.GrB_UINT8, 2, 3) >>> nvals(A) 0 """ n = ffi.new("GrB_Index*") check_status(A, lib.GrB_Matrix_nvals(n, A[0])) return n[0]
def format(A): """Return the format of the matrix. >>> A = new(lib.GrB_UINT8, 2, 2) >>> format(A) == lib.GxB_BY_ROW True """ format = ffi.new("GxB_Format_Value*") check_status(A, lib.GxB_Matrix_Option_get(A[0], lib.GxB_FORMAT, format)) return format[0]
def ncols(A): """Return the number of columns in the matrix. >>> A = new(lib.GrB_UINT8, 2, 3) >>> ncols(A) 3 """ n = ffi.new("GrB_Index*") check_status(A, lib.GrB_Matrix_ncols(n, A[0])) return n[0]
def type(A): """Return the GraphBLAS type of the vector. >>> A = new(lib.GrB_UINT8) >>> type(A) == lib.GrB_UINT8 True """ T = ffi.new("GrB_Type*") check_status(A, lib.GxB_Matrix_type(T, A[0])) return T[0]
def type(s): """Return the GraphBLAS type of the scalar. >>> S = new(lib.GrB_UINT8) >>> type(S) == lib.GrB_UINT8 True """ T = ffi.new("GrB_Type*") check_status(s, lib.GxB_Scalar_type(T, s[0])) return T[0]
def nrows(A): """Return the number of rows in the matrix. >>> A = new(lib.GrB_UINT8, 2, 3) >>> nrows(A) 2 """ n = ffi.new("GrB_Index*") check_status(A, lib.GrB_Matrix_nrows(n, A[0])) return n[0]
def type(v): """Return the GraphBLAS type of the vector. >>> v = new(lib.GrB_UINT8, 2) >>> type(v) == lib.GrB_UINT8 True """ T = ffi.new("GrB_Type*") check_status(v, lib.GxB_Vector_type(T, v[0])) return T[0]
def set_format(A, format): """Set the format of the matrix. >>> A = new(lib.GrB_UINT8, 2, 2) >>> set_format(A, lib.GxB_BY_COL) >>> format(A) == lib.GxB_BY_COL True """ check_status( A, lib.GxB_Matrix_Option_set(A[0], lib.GxB_FORMAT, ffi.cast("GxB_Format_Value", format)) )
def bool(A, i, j): """Get a boolean value from the matrix at row `i` column `j`. >>> A = new(lib.GrB_BOOL, 3, 3) >>> set_bool(A, True, 2, 2) >>> bool(A, 2, 2) == True True """ value = ffi.new("bool*") check_status(A, lib.GrB_Matrix_extractElement_BOOL(value, A[0], i, j)) return value[0]
def bool(v, i): """Get a boolean value from the vector at position `i`. >>> v = new(lib.GrB_BOOL, 3) >>> set_bool(v, True, 2) >>> bool(v, 2) == True True """ value = ffi.new("bool*") check_status(v, lib.GrB_Vector_extractElement_BOOL(value, v[0], i)) return value[0]
def nvals(v): """Return the number of stored elements in the vector. >>> v = new(lib.GrB_BOOL, 2) >>> nvals(v) 0 >>> set_bool(v, True, 1) >>> nvals(v) 1 """ n = ffi.new("GrB_Index*") check_status(v, lib.GrB_Vector_nvals(n, v[0])) return n[0]
def new(T, *, free=free): """Create a new `GxB_Scalar` of type `T` and initialize it. The `free` argument is called when the object is garbage collected, the default is `scalar.free()`. If `free` is None then there is no automatic garbage collection and it is up to the user to free the scalar. >>> S = new(lib.GrB_UINT8) """ s = ffi.new("GxB_Scalar*") check_status(s, lib.GxB_Scalar_new(s, T)) if free: return ffi.gc(s, free) return s
def test_check_status(): A = ffi.new("GrB_Matrix*") check_status(A, lib.GrB_Matrix_new(A, lib.GrB_BOOL, 2, 2)) with pytest.raises(exceptions.Panic): check_status(A, lib.GrB_PANIC) with pytest.raises(exceptions.Panic): check_status(A[0], lib.GrB_PANIC)
def bool(s): """Get a boolean value from the scalar. >>> s = new(lib.GrB_BOOL) >>> set_bool(s, True) >>> bool(s) == True True """ value = ffi.new("bool*") res = check_status(s, lib.GxB_Scalar_extractElement_BOOL(value, s[0])) if res == exceptions.NoValue: return None return value[0]
def new(T, size=lib.GxB_INDEX_MAX, *, free=free): """Create a new `GrB_Vector` of type `T` and initialize it. >>> A = new(lib.GrB_UINT8, 2) >>> size(A) 2 The default `size` is `lib.GxB_INDEX_MAX`. >>> A = new(lib.GrB_UINT8) >>> size(A) == lib.GxB_INDEX_MAX True The `free` argument is called when the object is garbage collected, the default is `vector.free()`. If `free` is None then there is no automatic garbage collection and it is up to the user to free the vector. """ v = ffi.new("GrB_Vector*") check_status(v, lib.GrB_Vector_new(v, T, size)) if free: return ffi.gc(v, free) return v
def set_sparsity_control(A, sparsity): """Set the sparsity control of the matrix.""" check_status( A, lib.GxB_Matrix_Option_set(A[0], lib.GxB_SPARSITY_CONTROL, ffi.cast("int", sparsity)) )
def free(v): """Free a scalar.""" check_status(v, lib.GxB_Scalar_free(v))
def free(A): """Free a matrix.""" check_status(A, lib.GrB_Matrix_free(A))
def set_bitmap_switch(A, bitmap_switch): """Set the bitmap switch of the matrix.""" check_status( A, lib.GxB_Matrix_Option_set(A[0], lib.GxB_BITMAP_SWITCH, ffi.cast("double", bitmap_switch)) )
def bitmap_switch(A): """Get the bitmap switch of the matrix.""" bitmap_switch = ffi.new("double*") check_status(A, lib.GxB_Matrix_Option_get(A[0], lib.GxB_BITMAP_SWITCH, bitmap_switch)) return bitmap_switch[0]
def set_hyper_switch(A, hyper_switch): """Set the hyper switch of the matrix.""" check_status( A, lib.GxB_Matrix_Option_set(A[0], lib.GxB_HYPER_SWITCH, ffi.cast("double", hyper_switch)) )
def sparsity_status(A): """Get the sparsity status of the matrix.""" sparsity_status = ffi.new("int32_t*") check_status(A, lib.GxB_Matrix_Option_get(A[0], lib.GxB_SPARSITY_STATUS, sparsity_status)) return sparsity_status[0]
def hyper_switch(A): """Get the hyper switch of the matrix.""" hyper_switch = ffi.new("double*") check_status(A, lib.GxB_Matrix_Option_get(A[0], lib.GxB_HYPER_SWITCH, hyper_switch)) return hyper_switch[0]
def sparsity_control(A): """Get the sparsity control of the matrix.""" sparsity_control = ffi.new("int32_t*") check_status(A, lib.GxB_Matrix_Option_get(A[0], lib.GxB_SPARSITY_CONTROL, sparsity_control)) return sparsity_control[0]
def free(v): """Free a vector.""" check_status(v, lib.GrB_Vector_free(v))