Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LG_nself_edges: count the # of diagonal entries in a matrix
3 : //------------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //------------------------------------------------------------------------------
17 :
18 : #define LG_FREE_ALL \
19 : { \
20 : GrB_free (&M) ; \
21 : GrB_free (&D) ; \
22 : GrB_free (&d) ; \
23 : }
24 :
25 : #include "LG_internal.h"
26 :
27 941 : int LG_nself_edges
28 : (
29 : // output:
30 : int64_t *nself_edges, // # of entries
31 : // input:
32 : GrB_Matrix A, // matrix to count
33 : char *msg // error message
34 : )
35 : {
36 :
37 : //--------------------------------------------------------------------------
38 : // extract the diagonal and count its entries
39 : //--------------------------------------------------------------------------
40 :
41 941 : GrB_Matrix D = NULL, M = NULL ;
42 941 : GrB_Vector d = NULL ;
43 941 : LG_ASSERT (nself_edges != NULL, GrB_NULL_POINTER) ;
44 941 : (*nself_edges) = LAGRAPH_UNKNOWN ;
45 :
46 : GrB_Index nrows, ncols ;
47 941 : GRB_TRY (GrB_Matrix_nrows (&nrows, A)) ;
48 941 : GRB_TRY (GrB_Matrix_ncols (&ncols, A)) ;
49 941 : GrB_Index n = LAGRAPH_MIN (nrows, ncols) ;
50 :
51 : // FUTURE: use a method that does not require atype
52 :
53 : GrB_Type atype ;
54 : char atype_name [LAGRAPH_MAX_NAME_LEN] ;
55 941 : LG_TRY (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
56 941 : LG_TRY (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
57 :
58 : #if LAGRAPH_SUITESPARSE
59 :
60 : //----------------------------------------------------------------------
61 : // SuiteSparse:GraphBLAS v5.0.2: use GxB_Vector_diag
62 : //----------------------------------------------------------------------
63 :
64 941 : GRB_TRY (GrB_Vector_new (&d, atype, n)) ;
65 809 : GRB_TRY (GxB_Vector_diag (d, A, 0, NULL)) ;
66 316 : GRB_TRY (GrB_Vector_nvals ((GrB_Index *) nself_edges, d)) ;
67 :
68 : #else
69 :
70 : //----------------------------------------------------------------------
71 : // pure GrB version with no GxB extensions
72 : //----------------------------------------------------------------------
73 :
74 : GRB_TRY (GrB_Matrix_new (&M, GrB_BOOL, nrows, ncols)) ;
75 : GRB_TRY (GrB_Matrix_new (&D, atype, nrows, ncols)) ;
76 : for (int64_t i = 0 ; i < n ; i++)
77 : {
78 : // M (i,i) = true
79 : GRB_TRY (GrB_Matrix_setElement (M, (bool) true, i, i)) ;
80 : }
81 :
82 : // D<M,struct> = A
83 : GRB_TRY (GrB_assign (D, M, NULL, A, GrB_ALL, nrows, GrB_ALL, ncols,
84 : GrB_DESC_S)) ;
85 : GRB_TRY (GrB_Matrix_nvals ((GrB_Index *) nself_edges, D)) ;
86 :
87 : #endif
88 :
89 316 : LG_FREE_ALL ;
90 316 : return (GrB_SUCCESS) ;
91 : }
|