Skip to content

Commit dab6bd0

Browse files
support all GraphBLAS methods & operations for OneBasedIndex and ZeroBasedIndex
1 parent 7df9d26 commit dab6bd0

19 files changed

+2512
-2439
lines changed

src/Global_Variables.jl

Lines changed: 1836 additions & 0 deletions
Large diffs are not rendered by default.

src/Interface/Object_Methods/Matrix_Methods.jl

Lines changed: 17 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
44
Create a GraphBLAS matrix of dimensions nrows x ncols such that A[I[k], J[k]] = X[k].
55
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)+1 and maximum(J)+1 (because of 0-based indexing)
7-
respectively. nvals is set to length(I) is not specified.
6+
If nrows and ncols are not specified, they are set to maximum(I) and maximum(J) (for one based indices).
7+
respectively. nvals is set to min(length(I), length(J)) if not specified.
88
"""
99
function GrB_Matrix(
1010
I::Vector{U},
1111
J::Vector{U},
1212
X::Vector{T};
13-
nrows::U = maximum(I)+1,
14-
ncols::U = maximum(J)+1,
15-
nvals::U = length(I),
16-
dup::GrB_BinaryOp = default_dup(T)) where {T, U <: GrB_Index}
13+
nrows::Union{Int64, UInt64} = maximum(I).x,
14+
ncols::Union{Int64, UInt64} = maximum(J).x,
15+
nvals::Union{Int64, UInt64} = min(length(I), length(J)),
16+
dup::GrB_BinaryOp = default_dup(T)) where {T, U <: Abstract_GrB_Index}
1717

1818
A = GrB_Matrix{T}()
1919
GrB_T = get_GrB_Type(T)
20+
if U <: ZeroBasedIndex
21+
nrows += 1
22+
ncols += 1
23+
end
2024
res = GrB_Matrix_new(A, GrB_T, nrows, ncols)
2125
if res != GrB_SUCCESS
2226
error(res)
@@ -32,22 +36,8 @@ end
3236
GrB_Matrix(T, nrows, ncols)
3337
3438
Create an empty GraphBLAS matrix of type T and dimensions nrows x ncols.
35-
36-
# Examples
37-
```jldoctest
38-
julia> using SuiteSparseGraphBLAS
39-
40-
julia> GrB_init(GrB_NONBLOCKING)
41-
GrB_SUCCESS::GrB_Info = 0
42-
43-
julia> A = GrB_Matrix(Float64, 4, 4)
44-
GrB_Matrix{Float64}
45-
46-
julia> nnz(A)
47-
0
48-
```
4939
"""
50-
function GrB_Matrix(T, nrows::GrB_Index, ncols::GrB_Index)
40+
function GrB_Matrix(T, nrows::Union{Int64, UInt64}, ncols::Union{Int64, UInt64})
5141
A = GrB_Matrix{T}()
5242
GrB_T = get_GrB_Type(T)
5343
res = GrB_Matrix_new(A, GrB_T, nrows, ncols)
@@ -61,35 +51,6 @@ end
6151
==(A, B)
6252
6353
Check if two GraphBLAS matrices are equal.
64-
65-
# Examples
66-
```jldoctest
67-
julia> using SuiteSparseGraphBLAS
68-
69-
julia> GrB_init(GrB_NONBLOCKING)
70-
GrB_SUCCESS::GrB_Info = 0
71-
72-
julia> A = GrB_Matrix([1, 2, 3], [2, 4, 5], [1, 1, 1])
73-
GrB_Matrix{Int64}
74-
75-
julia> B = GrB_Matrix([1, 2, 3], [2, 4, 5], [1, 1, 1])
76-
GrB_Matrix{Int64}
77-
78-
julia> A == B
79-
true
80-
81-
julia> B = GrB_Matrix([1, 2, 3], [2, 4, 5], [1, 1, 2])
82-
GrB_Matrix{Int64}
83-
84-
julia> A == B
85-
false
86-
87-
julia> B = GrB_Matrix([1, 2, 3], [2, 4, 3], [1, 1, 1])
88-
GrB_Matrix{Int64}
89-
90-
julia> A == B
91-
false
92-
```
9354
"""
9455
function ==(A::GrB_Matrix{T}, B::GrB_Matrix{U}) where {T, U}
9556
T != U && return false
@@ -127,27 +88,13 @@ function ==(A::GrB_Matrix{T}, B::GrB_Matrix{U}) where {T, U}
12788
end
12889

12990
"""
130-
findnz(A)
91+
findnz(A, [index_type])
13192
13293
Return a tuple (I, J, X) where I and J are the row and column indices of the stored values in GraphBLAS matrix A,
133-
and X is a vector of the values.
134-
135-
# Examples
136-
```jldoctest
137-
julia> using SuiteSparseGraphBLAS
138-
139-
julia> GrB_init(GrB_NONBLOCKING)
140-
GrB_SUCCESS::GrB_Info = 0
141-
142-
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
143-
GrB_Matrix{Int64}
144-
145-
julia> findnz(A)
146-
([1, 2, 3], [1, 2, 3], [1, 1, 1])
147-
```
94+
and X is a vector of the values. Indices are zero based by default if not specified.
14895
"""
149-
function findnz(A::GrB_Matrix)
150-
res = GrB_Matrix_extractTuples(A)
96+
function findnz(A::GrB_Matrix, index_type::Type{<:Abstract_GrB_Index} = ZeroBasedIndex)
97+
res = GrB_Matrix_extractTuples(A, index_type)
15198
if typeof(res) == GrB_Info
15299
error(res)
153100
end
@@ -159,20 +106,6 @@ end
159106
nnz(A)
160107
161108
Return the number of stored (filled) elements in a GraphBLAS matrix.
162-
163-
# Examples
164-
```jldoctest
165-
julia> using SuiteSparseGraphBLAS
166-
167-
julia> GrB_init(GrB_NONBLOCKING)
168-
GrB_SUCCESS::GrB_Info = 0
169-
170-
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
171-
GrB_Matrix{Int64}
172-
173-
julia> nnz(A)
174-
3
175-
```
176109
"""
177110
function nnz(A::GrB_Matrix)
178111
nvals = GrB_Matrix_nvals(A)
@@ -186,26 +119,6 @@ end
186119
size(A,[ dim])
187120
188121
Return number of rows or/and columns in a GraphBLAS matrix.
189-
190-
# Examples
191-
```jldoctest
192-
julia> using SuiteSparseGraphBLAS
193-
194-
julia> GrB_init(GrB_NONBLOCKING)
195-
GrB_SUCCESS::GrB_Info = 0
196-
197-
julia> A = GrB_Matrix([0, 1, 2], [0, 1, 2], [1, 1, 1])
198-
GrB_Matrix{Int64}
199-
200-
julia> size(A)
201-
(3, 3)
202-
203-
julia> size(A, 1)
204-
3
205-
206-
julia> size(A, 2)
207-
3
208-
```
209122
"""
210123
function size(A::GrB_Matrix)
211124
nrows = GrB_Matrix_nrows(A)
@@ -245,22 +158,8 @@ end
245158
getindex(A, row_index, col_index)
246159
247160
Return A[row_index, col_index] where A is a GraphBLAS matrix.
248-
249-
# Examples
250-
```jldoctest
251-
julia> using SuiteSparseGraphBLAS
252-
253-
julia> GrB_init(GrB_NONBLOCKING)
254-
GrB_SUCCESS::GrB_Info = 0
255-
256-
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
257-
GrB_Matrix{Int64}
258-
259-
julia> A[1, 1]
260-
1
261-
```
262161
"""
263-
function getindex(A::GrB_Matrix, row_index::GrB_Index, col_index::GrB_Index)
162+
function getindex(A::GrB_Matrix, row_index::Abstract_GrB_Index, col_index::Abstract_GrB_Index)
264163
res = GrB_Matrix_extractElement(A, row_index, col_index)
265164
if typeof(res) == GrB_Info
266165
error(res)
@@ -272,28 +171,8 @@ end
272171
setindex!(A, X, I, J)
273172
274173
Set A[I, J] = X where A is a GraphBLAS matrix.
275-
276-
# Examples
277-
```jldoctest
278-
julia> using SuiteSparseGraphBLAS
279-
280-
julia> GrB_init(GrB_NONBLOCKING)
281-
GrB_SUCCESS::GrB_Info = 0
282-
283-
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
284-
GrB_Matrix{Int64}
285-
286-
julia> A[1, 1]
287-
1
288-
289-
julia> A[1, 1] = 5
290-
5
291-
292-
julia> A[1, 1]
293-
5
294-
```
295174
"""
296-
function setindex!(A::GrB_Matrix{T}, X::T, I::GrB_Index, J::GrB_Index) where T
175+
function setindex!(A::GrB_Matrix{T}, X::T, I::Abstract_GrB_Index, J::Abstract_GrB_Index) where T
297176
res = GrB_Matrix_setElement(A, X, I, J)
298177
if res != GrB_SUCCESS
299178
error(res)
@@ -304,25 +183,6 @@ end
304183
empty!(A)
305184
306185
Remove all stored entries from GraphBLAS matrix A.
307-
308-
# Examples
309-
```jldoctest
310-
julia> using SuiteSparseGraphBLAS
311-
312-
julia> GrB_init(GrB_NONBLOCKING)
313-
GrB_SUCCESS::GrB_Info = 0
314-
315-
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
316-
GrB_Matrix{Int64}
317-
318-
julia> nnz(A)
319-
3
320-
321-
julia> empty!(A)
322-
323-
julia> nnz(A)
324-
0
325-
```
326186
"""
327187
function empty!(A::GrB_Matrix)
328188
res = GrB_Matrix_clear(A)
@@ -335,23 +195,6 @@ end
335195
copy(A)
336196
337197
Create a new GraphBLAS matrix with the same domain, dimensions, and contents as GraphBLAS matrix A.
338-
339-
# Examples
340-
```jldoctest
341-
julia> using SuiteSparseGraphBLAS
342-
343-
julia> GrB_init(GrB_NONBLOCKING)
344-
GrB_SUCCESS::GrB_Info = 0
345-
346-
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
347-
GrB_Matrix{Int64}
348-
349-
julia> B = copy(A)
350-
GrB_Matrix{Int64}
351-
352-
julia> findnz(B)
353-
([1, 2, 3], [1, 2, 3], [1, 1, 1])
354-
```
355198
"""
356199
function copy(A::GrB_Matrix{T}) where T
357200
C = GrB_Matrix{T}()
@@ -366,20 +209,6 @@ end
366209
adjoint(A)
367210
368211
Compute transpose of a GraphBLAS matrix.
369-
370-
# Examples
371-
```jldoctest
372-
julia> using SuiteSparseGraphBLAS
373-
374-
julia> GrB_init(GrB_NONBLOCKING)
375-
GrB_SUCCESS::GrB_Info = 0
376-
377-
julia> M = GrB_Matrix([1, 1], [2, 3], [1, 1])
378-
GrB_Matrix{Int64}
379-
380-
julia> findnz(M')
381-
([2, 3], [1, 1], [1, 1])
382-
```
383212
"""
384213
function adjoint(A::GrB_Matrix{T}) where T
385214
C = GrB_Matrix(T, size(A, 2), size(A, 1))

0 commit comments

Comments
 (0)