Skip to content

Commit 3bece15

Browse files
add LowerTriangular, UpperTriangular and Diagonal functions to interface
1 parent 2fcf359 commit 3bece15

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/Interface/Interface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ import SuiteSparseGraphBLAS:
1212
include("./Object_Methods/Matrix_Methods.jl")
1313
include("./Object_Methods/Vector_Methods.jl")
1414
include("./Object_Methods/Descriptor_Methods.jl")
15-
export findnz, nnz
15+
export findnz, nnz, LowerTriangular, UpperTriangular, Diagonal
1616

1717
end # end of module

src/Interface/Object_Methods/Matrix_Methods.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,45 @@ function copy(A::GrB_Matrix{T}) where T <: valid_types
303303
end
304304
return C
305305
end
306+
307+
"""
308+
LowerTriangular(A)
309+
310+
Return lower triangle of a GraphBLAS matrix.
311+
"""
312+
function LowerTriangular(A::GrB_Matrix{T}) where T <: valid_types
313+
nrows, ncols = size(A)
314+
if nrows != ncols
315+
error("Matrix is not square")
316+
end
317+
L = GrB_Matrix(T, nrows, ncols)
318+
GxB_select(L, GrB_NULL, GrB_NULL, GxB_TRIL, A, 0, GrB_NULL)
319+
return L
320+
end
321+
322+
"""
323+
UpperTriangular(A)
324+
325+
Return upper triangle of a GraphBLAS matrix.
326+
"""
327+
function UpperTriangular(A::GrB_Matrix{T}) where T <: valid_types
328+
nrows, ncols = size(A)
329+
if nrows != ncols
330+
error("Matrix is not square")
331+
end
332+
U = GrB_Matrix(T, nrows, ncols)
333+
GxB_select(U, GrB_NULL, GrB_NULL, GxB_TRIU, A, 0, GrB_NULL)
334+
return U
335+
end
336+
337+
"""
338+
Diagonal(A)
339+
340+
Return diagonal of a GraphBLAS matrix.
341+
"""
342+
function Diagonal(A::GrB_Matrix{T}) where T <: valid_types
343+
nrows, ncols = size(A)
344+
D = GrB_Matrix(T, nrows, ncols)
345+
GxB_select(D, GrB_NULL, GrB_NULL, GxB_DIAG, A, 0, GrB_NULL)
346+
return D
347+
end

src/Operations/Select.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ function GxB_Matrix_select( # C<Mask> = accum (C, op(A,k)) or op(A',
4444
C.p, Mask.p, accum.p, op.p, A.p, Ref(thunk), desc.p
4545
)
4646
)
47-
end
47+
end

src/SuiteSparseGraphBLAS.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ include("Interface/Interface.jl")
170170

171171
using .Interface
172172

173-
export findnz, nnz
173+
export findnz, nnz, LowerTriangular, UpperTriangular, Diagonal
174174

175175
export
176176
# Context Methods

0 commit comments

Comments
 (0)