1function C = eq (A, B)
2%A == B equal.
3% C = (A == B) is an element-by-element comparison of A and B.  One or
4% both may be scalars.  Otherwise, A and B must have the same size.
5%
6% The input matrices may be either GraphBLAS and/or MATLAB matrices, in
7% any combination.  C is returned as a GraphBLAS matrix.
8%
9% See also GrB/lt, GrB/le, GrB/gt, GrB/ge, GrB/ne.
10
11% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
12% SPDX-License-Identifier: GPL-3.0-or-later
13
14% The pattern of C depends on the type of inputs:
15% A scalar, B scalar:  C is scalar.
16% A scalar, B matrix:  C is full if A==0, otherwise C is a subset of B.
17% B scalar, A matrix:  C is full if B==0, otherwise C is a subset of A.
18% A matrix, B matrix:  C is full.
19
20if (isobject (A))
21    A = A.opaque ;
22end
23
24if (isobject (B))
25    B = B.opaque ;
26end
27
28[am, an, atype] = gbsize (A) ;
29[bm, bn, btype] = gbsize (B) ;
30a_is_scalar = (am == 1) && (an == 1) ;
31b_is_scalar = (bm == 1) && (bn == 1) ;
32ctype = gboptype (atype, btype) ;
33
34if (a_is_scalar)
35    if (b_is_scalar)
36        % both A and B are scalars.  C is full.
37        C = GrB (gbemult (gbfull (A, ctype), '==', gbfull (B, ctype))) ;
38    else
39        % A is a scalar, B is a matrix
40        if (gb_scalar (A) == 0)
41            % since a == 0, entries not present in B result in a true
42            % value, so the result is full.  Expand A to a full matrix.
43            A = gb_scalar_to_full (bm, bn, ctype, gb_fmt (B), A) ;
44            C = GrB (gbemult (A, '==', gbfull (B, ctype))) ;
45        else
46            % since a ~= 0, entries not present in B result in a false
47            % value, so the result is a sparse subset of B.  select all
48            % entries in B == a, then convert to true.
49            C = GrB (gbapply ('1.logical', gbselect (B, '==', A))) ;
50        end
51    end
52else
53    if (b_is_scalar)
54        % A is a matrix, B is a scalar
55        if (gb_scalar (B) == 0)
56            % since b == 0, entries not present in A result in a true
57            % value, so the result is full.  Expand B to a full matrix.
58            B = gb_scalar_to_full (am, an, ctype, gb_fmt (A), B) ;
59            C = GrB (gbemult (gbfull (A, ctype), '==', B)) ;
60        else
61            % since b ~= 0, entries not present in A result in a false
62            % value, so the result is a sparse subset of A.  select all
63            % entries in A == b, then convert to true.
64            C = GrB (gbapply ('1.logical', gbselect (A, '==', B))) ;
65        end
66    else
67        % both A and B are matrices.  C is full.
68        C = GrB (gbemult (gbfull (A, ctype), '==', gbfull (B, ctype))) ;
69    end
70end
71
72