1 //------------------------------------------------------------------------------
2 // gb_is_equal: check two matrices for exact equality
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: GPL-3.0-or-later
7 
8 //------------------------------------------------------------------------------
9 
10 // gb_is_equal checks if two matrices are identically equal (same size,
11 // type, pattern, size, and values).
12 
13 // If the two matrices are GrB_FP32, GrB_FP64, GxB_FC32, or GxB_FC64,
14 // and have NaNs, then gb_is_equal returns false, since NaN == NaN is
15 // false.  To check for NaN equality (like isequaln in MATLAB), use gb_is_all
16 // with a user-defined operator f(x,y) that returns true if x and y are equal,
17 // or if both are NaN, and false otherwise.
18 
19 #include "gb_matlab.h"
20 
gb_is_equal(GrB_Matrix A,GrB_Matrix B)21 bool gb_is_equal            // true if A == B, false if A ~= B
22 (
23     GrB_Matrix A,
24     GrB_Matrix B
25 )
26 {
27 
28     // check the type of A and B
29     GrB_Type atype, btype ;
30     OK (GxB_Matrix_type (&atype, A)) ;
31     OK (GxB_Matrix_type (&btype, B)) ;
32     if (atype != btype)
33     {
34         // types differ
35         return (false) ;
36     }
37 
38     // select the comparator operator
39     GrB_BinaryOp op ;
40     if      (atype == GrB_BOOL  ) op = GrB_EQ_BOOL   ;
41     else if (atype == GrB_INT8  ) op = GrB_EQ_INT8   ;
42     else if (atype == GrB_INT16 ) op = GrB_EQ_INT16  ;
43     else if (atype == GrB_INT32 ) op = GrB_EQ_INT32  ;
44     else if (atype == GrB_INT64 ) op = GrB_EQ_INT64  ;
45     else if (atype == GrB_UINT8 ) op = GrB_EQ_UINT8  ;
46     else if (atype == GrB_UINT16) op = GrB_EQ_UINT16 ;
47     else if (atype == GrB_UINT32) op = GrB_EQ_UINT32 ;
48     else if (atype == GrB_UINT64) op = GrB_EQ_UINT64 ;
49     else if (atype == GrB_FP32  ) op = GrB_EQ_FP32   ;
50     else if (atype == GrB_FP64  ) op = GrB_EQ_FP64   ;
51     else if (atype == GxB_FC32  ) op = GxB_EQ_FC32   ;
52     else if (atype == GxB_FC64  ) op = GxB_EQ_FC64   ;
53     else
54     {
55         ERROR ("unsupported type") ;
56     }
57 
58     // check the size, pattern, and values of A and B
59     return (gb_is_all (A, B, op)) ;
60 }
61 
62