|
| 1 | +""" |
| 2 | + GrB_Matrix(I, J, X,[ nrows, ncols, nvals, dup]) |
| 3 | +
|
| 4 | +Create a GraphBLAS matrix of dimensions nrows x ncols such that A[I[k], J[k]] = X[k]. |
| 5 | +dup is a GraphBLAS binary operator used to combine duplicates, it defaults to `FIRST`. |
| 6 | +If nrows and ncols are not specified, they are set to maximum(I) and maximum(J) respectively. |
| 7 | +nvals is set to length(I) is not specified. |
| 8 | +
|
| 9 | +# Examples |
| 10 | +```jldoctest |
| 11 | +julia> using SuiteSparseGraphBLAS |
| 12 | +
|
| 13 | +julia> A = GrB_Matrix([1, 1, 2, 3], [1, 1, 2, 3], UInt32[1, 10, 1, 1], dup = GrB_PLUS_UINT32) |
| 14 | +GrB_Matrix{UInt32} |
| 15 | +
|
| 16 | +julia> A[1, 1] |
| 17 | +0x0000000b |
| 18 | +``` |
| 19 | +""" |
| 20 | +function GrB_Matrix( |
| 21 | + I::Vector{U}, |
| 22 | + J::Vector{U}, |
| 23 | + X::Vector{T}; |
| 24 | + nrows::U = maximum(I), |
| 25 | + ncols::U = maximum(J), |
| 26 | + nvals::U = length(I), |
| 27 | + dup::GrB_BinaryOp = default_dup(T)) where {T <: valid_types, U <: GrB_Index} |
| 28 | + |
| 29 | + A = GrB_Matrix{T}() |
| 30 | + GrB_T = get_GrB_Type(T) |
| 31 | + res = GrB_Matrix_new(A, GrB_T, nrows, ncols) |
| 32 | + if res != GrB_SUCCESS |
| 33 | + error(res) |
| 34 | + end |
| 35 | + res = GrB_Matrix_build(A, I.-1, J.-1, X, nvals, dup) |
| 36 | + if res != GrB_SUCCESS |
| 37 | + error(res) |
| 38 | + end |
| 39 | + return A |
| 40 | +end |
| 41 | + |
| 42 | +""" |
| 43 | + GrB_Matrix(T, nrows, ncols) |
| 44 | +
|
| 45 | +Create an empty GraphBLAS matrix of type T and dimensions nrows x ncols. |
| 46 | +
|
| 47 | +# Examples |
| 48 | +```jldoctest |
| 49 | +julia> using SuiteSparseGraphBLAS |
| 50 | +
|
| 51 | +julia> GrB_init(GrB_NONBLOCKING) |
| 52 | +GrB_SUCCESS::GrB_Info = 0 |
| 53 | +
|
| 54 | +julia> A = GrB_Matrix(Float64, 4, 4) |
| 55 | +GrB_Matrix{Float64} |
| 56 | +
|
| 57 | +julia> nnz(A) |
| 58 | +0 |
| 59 | +``` |
| 60 | +""" |
| 61 | +function GrB_Matrix(T::DataType, nrows::GrB_Index, ncols::GrB_Index) |
| 62 | + A = GrB_Matrix{T}() |
| 63 | + GrB_T = get_GrB_Type(T) |
| 64 | + res = GrB_Matrix_new(A, GrB_T, nrows, ncols) |
| 65 | + if res != GrB_SUCCESS |
| 66 | + error(res) |
| 67 | + end |
| 68 | + return A |
| 69 | +end |
| 70 | + |
| 71 | +""" |
| 72 | + findnz(A) |
| 73 | +
|
| 74 | +Return a tuple (I, J, X) where I and J are the row and column indices of the stored values in GraphBLAS matrix A, |
| 75 | +and X is a vector of the values. |
| 76 | +
|
| 77 | +# Examples |
| 78 | +```jldoctest |
| 79 | +julia> using SuiteSparseGraphBLAS |
| 80 | +
|
| 81 | +julia> GrB_init(GrB_NONBLOCKING) |
| 82 | +GrB_SUCCESS::GrB_Info = 0 |
| 83 | +
|
| 84 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 85 | +GrB_Matrix{Int64} |
| 86 | +
|
| 87 | +julia> findnz(A) |
| 88 | +([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 89 | +``` |
| 90 | +""" |
| 91 | +function findnz(A::GrB_Matrix) |
| 92 | + res = GrB_Matrix_extractTuples(A) |
| 93 | + if typeof(res) == GrB_Info |
| 94 | + error(res) |
| 95 | + end |
| 96 | + I, J, X = res |
| 97 | + return I.+1, J.+1, X |
| 98 | +end |
| 99 | + |
| 100 | +""" |
| 101 | + nnz(A) |
| 102 | +
|
| 103 | +Return the number of stored (filled) elements in a GraphBLAS matrix. |
| 104 | +
|
| 105 | +# Examples |
| 106 | +```jldoctest |
| 107 | +julia> using SuiteSparseGraphBLAS |
| 108 | +
|
| 109 | +julia> GrB_init(GrB_NONBLOCKING) |
| 110 | +GrB_SUCCESS::GrB_Info = 0 |
| 111 | +
|
| 112 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 113 | +GrB_Matrix{Int64} |
| 114 | +
|
| 115 | +julia> nnz(A) |
| 116 | +3 |
| 117 | +``` |
| 118 | +""" |
| 119 | +function nnz(A::GrB_Matrix) |
| 120 | + nvals = GrB_Matrix_nvals(A) |
| 121 | + if typeof(nvals) == GrB_Info |
| 122 | + error(nvals) |
| 123 | + end |
| 124 | + return nvals |
| 125 | +end |
| 126 | + |
| 127 | +""" |
| 128 | + size(A,[ dim]) |
| 129 | +
|
| 130 | +Return number of rows or/and columns in a GraphBLAS matrix. |
| 131 | +
|
| 132 | +# Examples |
| 133 | +```jldoctest |
| 134 | +julia> using SuiteSparseGraphBLAS |
| 135 | +
|
| 136 | +julia> GrB_init(GrB_NONBLOCKING) |
| 137 | +GrB_SUCCESS::GrB_Info = 0 |
| 138 | +
|
| 139 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 140 | +GrB_Matrix{Int64} |
| 141 | +
|
| 142 | +julia> size(A) |
| 143 | +(3, 3) |
| 144 | +
|
| 145 | +julia> size(A, 1) |
| 146 | +3 |
| 147 | +
|
| 148 | +julia> size(A, 2) |
| 149 | +3 |
| 150 | +``` |
| 151 | +""" |
| 152 | +function size(A::GrB_Matrix) |
| 153 | + nrows = GrB_Matrix_nrows(A) |
| 154 | + if typeof(nrows) == GrB_Info |
| 155 | + error(nrows) |
| 156 | + end |
| 157 | + ncols = GrB_Matrix_ncols(A) |
| 158 | + if typeof(ncols) == GrB_Info |
| 159 | + error(ncols) |
| 160 | + end |
| 161 | + return (nrows, ncols) |
| 162 | +end |
| 163 | + |
| 164 | +function size(A::GrB_Matrix, dim::Int64) |
| 165 | + if dim <= 0 |
| 166 | + return error("dimension out of range") |
| 167 | + end |
| 168 | + |
| 169 | + if dim == 1 |
| 170 | + nrows = GrB_Matrix_nrows(A) |
| 171 | + if typeof(nrows) == GrB_Info |
| 172 | + error(nrows) |
| 173 | + end |
| 174 | + return nrows |
| 175 | + elseif dim == 2 |
| 176 | + ncols = GrB_Matrix_ncols(A) |
| 177 | + if typeof(ncols) == GrB_Info |
| 178 | + error(ncols) |
| 179 | + end |
| 180 | + return ncols |
| 181 | + end |
| 182 | + |
| 183 | + return 1 |
| 184 | +end |
| 185 | + |
| 186 | +""" |
| 187 | + getindex(A, row_index, col_index) |
| 188 | +
|
| 189 | +Return A[row_index, col_index] where A is a GraphBLAS matrix. |
| 190 | +
|
| 191 | +# Examples |
| 192 | +```jldoctest |
| 193 | +julia> using SuiteSparseGraphBLAS |
| 194 | +
|
| 195 | +julia> GrB_init(GrB_NONBLOCKING) |
| 196 | +GrB_SUCCESS::GrB_Info = 0 |
| 197 | +
|
| 198 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 199 | +GrB_Matrix{Int64} |
| 200 | +
|
| 201 | +julia> A[1, 1] |
| 202 | +1 |
| 203 | +``` |
| 204 | +""" |
| 205 | +function getindex(A::GrB_Matrix, row_index::GrB_Index, col_index::GrB_Index) |
| 206 | + res = GrB_Matrix_extractElement(A, row_index-1, col_index-1) |
| 207 | + if typeof(res) == GrB_Info |
| 208 | + error(res) |
| 209 | + end |
| 210 | + return res |
| 211 | +end |
| 212 | + |
| 213 | +""" |
| 214 | + setindex!(A, X, I, J) |
| 215 | +
|
| 216 | +Set A[I, J] = X where A is a GraphBLAS matrix. |
| 217 | +
|
| 218 | +# Examples |
| 219 | +```jldoctest |
| 220 | +julia> using SuiteSparseGraphBLAS |
| 221 | +
|
| 222 | +julia> GrB_init(GrB_NONBLOCKING) |
| 223 | +GrB_SUCCESS::GrB_Info = 0 |
| 224 | +
|
| 225 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 226 | +GrB_Matrix{Int64} |
| 227 | +
|
| 228 | +julia> A[1, 1] |
| 229 | +1 |
| 230 | +
|
| 231 | +julia> A[1, 1] = 5 |
| 232 | +5 |
| 233 | +
|
| 234 | +julia> A[1, 1] |
| 235 | +5 |
| 236 | +``` |
| 237 | +""" |
| 238 | +function setindex!(A::GrB_Matrix{T}, X::T, I::GrB_Index, J::GrB_Index) where {T <: valid_types} |
| 239 | + res = GrB_Matrix_setElement(A, X, I-1, J-1) |
| 240 | + if res != GrB_SUCCESS |
| 241 | + error(res) |
| 242 | + end |
| 243 | +end |
| 244 | + |
| 245 | +""" |
| 246 | + empty!(A) |
| 247 | +
|
| 248 | +Remove all stored entries from GraphBLAS matrix A. |
| 249 | +
|
| 250 | +# Examples |
| 251 | +```jldoctest |
| 252 | +julia> using SuiteSparseGraphBLAS |
| 253 | +
|
| 254 | +julia> GrB_init(GrB_NONBLOCKING) |
| 255 | +GrB_SUCCESS::GrB_Info = 0 |
| 256 | +
|
| 257 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 258 | +GrB_Matrix{Int64} |
| 259 | +
|
| 260 | +julia> nnz(A) |
| 261 | +3 |
| 262 | +
|
| 263 | +julia> empty!(A) |
| 264 | +
|
| 265 | +julia> nnz(A) |
| 266 | +0 |
| 267 | +``` |
| 268 | +""" |
| 269 | +function empty!(A::GrB_Matrix) |
| 270 | + res = GrB_Matrix_clear(A) |
| 271 | + if res != GrB_SUCCESS |
| 272 | + error(res) |
| 273 | + end |
| 274 | +end |
| 275 | + |
| 276 | +""" |
| 277 | + copy(A) |
| 278 | +
|
| 279 | +Create a new GraphBLAS matrix with the same domain, dimensions, and contents as GraphBLAS matrix A. |
| 280 | +
|
| 281 | +# Examples |
| 282 | +```jldoctest |
| 283 | +julia> using SuiteSparseGraphBLAS |
| 284 | +
|
| 285 | +julia> GrB_init(GrB_NONBLOCKING) |
| 286 | +GrB_SUCCESS::GrB_Info = 0 |
| 287 | +
|
| 288 | +julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 289 | +GrB_Matrix{Int64} |
| 290 | +
|
| 291 | +julia> B = copy(A) |
| 292 | +GrB_Matrix{Int64} |
| 293 | +
|
| 294 | +julia> findnz(B) |
| 295 | +([1, 2, 3], [1, 2, 3], [1, 1, 1]) |
| 296 | +``` |
| 297 | +""" |
| 298 | +function copy(A::GrB_Matrix{T}) where T <: valid_types |
| 299 | + C = GrB_Matrix{T}() |
| 300 | + res = GrB_Matrix_dup(C, A) |
| 301 | + if res != GrB_SUCCESS |
| 302 | + error(res) |
| 303 | + end |
| 304 | + return C |
| 305 | +end |
0 commit comments