Line data Source code
1 : //------------------------------------------------------------------------------ 2 : // LAGraph_Matrix_IsEqualOp: compare two matrices with a given op 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 : // LAGraph_Matrix_IsEqualOp: check if two matrices are equal (same 19 : // size, structure, size, and values). 20 : 21 : #define LG_FREE_WORK GrB_free (&C) ; 22 : 23 : #include "LG_internal.h" 24 : 25 : //------------------------------------------------------------------------------ 26 : // LAGraph_Matrix_IsEqualOp: compare two matrices using a given operator 27 : //------------------------------------------------------------------------------ 28 : 29 128 : int LAGraph_Matrix_IsEqualOp 30 : ( 31 : // output: 32 : bool *result, // true if A == B, false if A != B or error 33 : // input: 34 : const GrB_Matrix A, 35 : const GrB_Matrix B, 36 : const GrB_BinaryOp op, // comparator to use 37 : char *msg 38 : ) 39 : { 40 : 41 : //-------------------------------------------------------------------------- 42 : // check inputs 43 : //-------------------------------------------------------------------------- 44 : 45 128 : LG_CLEAR_MSG ; 46 128 : GrB_Matrix C = NULL ; 47 128 : LG_ASSERT (op != NULL && result != NULL, GrB_NULL_POINTER) ; 48 : 49 : //-------------------------------------------------------------------------- 50 : // check for NULL and aliased matrices 51 : //-------------------------------------------------------------------------- 52 : 53 127 : if (A == NULL || B == NULL || A == B) 54 : { 55 : // two NULL matrices are identical, as are two aliased matrices 56 32 : (*result) = (A == B) ; 57 32 : return (GrB_SUCCESS) ; 58 : } 59 : 60 : //-------------------------------------------------------------------------- 61 : // compare the size of A and B 62 : //-------------------------------------------------------------------------- 63 : 64 : GrB_Index nrows1, ncols1, nrows2, ncols2 ; 65 95 : GRB_TRY (GrB_Matrix_nrows (&nrows1, A)) ; 66 95 : GRB_TRY (GrB_Matrix_nrows (&nrows2, B)) ; 67 95 : GRB_TRY (GrB_Matrix_ncols (&ncols1, A)) ; 68 95 : GRB_TRY (GrB_Matrix_ncols (&ncols2, B)) ; 69 95 : if (nrows1 != nrows2 || ncols1 != ncols2) 70 : { 71 : // dimensions differ 72 4 : (*result) = false ; 73 4 : return (GrB_SUCCESS) ; 74 : } 75 : 76 : //-------------------------------------------------------------------------- 77 : // compare the # entries in A and B 78 : //-------------------------------------------------------------------------- 79 : 80 : GrB_Index nvals1, nvals2 ; 81 91 : GRB_TRY (GrB_Matrix_nvals (&nvals1, A)) ; 82 91 : GRB_TRY (GrB_Matrix_nvals (&nvals2, B)) ; 83 91 : if (nvals1 != nvals2) 84 : { 85 : // # of entries differ 86 4 : (*result) = false ; 87 4 : return (GrB_SUCCESS) ; 88 : } 89 : 90 : //-------------------------------------------------------------------------- 91 : // C = A .* B, where the structure of C is the intersection of A and B 92 : //-------------------------------------------------------------------------- 93 : 94 87 : GRB_TRY (GrB_Matrix_new (&C, GrB_BOOL, nrows1, ncols1)) ; 95 51 : GRB_TRY (GrB_eWiseMult (C, NULL, NULL, op, A, B, NULL)) ; 96 : 97 : //-------------------------------------------------------------------------- 98 : // ensure C has the same number of entries as A and B 99 : //-------------------------------------------------------------------------- 100 : 101 : GrB_Index nvals ; 102 24 : GRB_TRY (GrB_Matrix_nvals (&nvals, C)) ; 103 24 : if (nvals != nvals1) 104 : { 105 : // structure of A and B are different 106 2 : LG_FREE_WORK ; 107 2 : (*result) = false ; 108 2 : return (GrB_SUCCESS) ; 109 : } 110 : 111 : //-------------------------------------------------------------------------- 112 : // result = and (C) 113 : //-------------------------------------------------------------------------- 114 : 115 22 : GRB_TRY (GrB_reduce (result, NULL, GrB_LAND_MONOID_BOOL, C, NULL)) ; 116 : 117 : //-------------------------------------------------------------------------- 118 : // free workspace and return result 119 : //-------------------------------------------------------------------------- 120 : 121 22 : LG_FREE_WORK ; 122 22 : return (GrB_SUCCESS) ; 123 : }