1function C = ne (A, B)
2%A ~= B not 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% See also GrB/lt, GrB/le, GrB/gt, GrB/ge, GrB/eq.
7
8% FUTURE: ne(A,B) for two matrices A and B is slower than it could be.
9% See comments in gb_union_op.
10
11% The pattern of C depends on the type of inputs:
12% A scalar, B scalar:  C is scalar.
13% A scalar, B matrix:  C is full if A~=0, otherwise C is a subset of B.
14% B scalar, A matrix:  C is full if B~=0, otherwise C is a subset of A.
15% A matrix, B matrix:  C is sparse, with the pattern of A+B.
16% Zeroes are then dropped from C after it is computed.
17
18% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
19% SPDX-License-Identifier: GPL-3.0-or-later
20
21if (isobject (A))
22    A = A.opaque ;
23end
24
25if (isobject (B))
26    B = B.opaque ;
27end
28
29[am, an, atype] = gbsize (A) ;
30[bm, bn, btype] = gbsize (B) ;
31a_is_scalar = (am == 1) && (an == 1) ;
32b_is_scalar = (bm == 1) && (bn == 1) ;
33ctype = gboptype (atype, btype) ;
34
35if (a_is_scalar)
36    if (b_is_scalar)
37        % both A and B are scalars.  C is sparse.
38        C = GrB (gb_union_op ('~=', A, B)) ;
39    else
40        % A is a scalar, B is a matrix
41        if (gb_scalar (A) ~= 0)
42            % since a ~= 0, entries not present in B result in a true
43            % value, so the result is full.  Expand A to a full matrix.
44            A = gb_scalar_to_full (bm, bn, ctype, gb_fmt (B), A) ;
45            C = GrB (gbemult (A, '~=', gbfull (B, ctype))) ;
46        else
47            % since a == 0, entries not present in B result in a false
48            % value, so the result is a sparse subset of B.  select all
49            % entries in B ~= 0, then convert to true.
50            C = GrB (gbnew (B, 'logical')) ;
51        end
52    end
53else
54    if (b_is_scalar)
55        % A is a matrix, B is a scalar
56        if (gb_scalar (B) ~= 0)
57            % since b ~= 0, entries not present in A result in a true
58            % value, so the result is full.  Expand B to a full matrix.
59            B = gb_scalar_to_full (am, an, ctype, gb_fmt (A), B) ;
60            C = GrB (gbemult (gbfull (A, ctype), '~=', B)) ;
61        else
62            % since b == 0, entries not present in A result in a false
63            % value, so the result is a sparse subset of A.  Simply
64            % typecast A to logical.  Explicit zeroes in A become explicit
65            % false entries.  Any other explicit entries not equal to zero
66            % become true.
67            C = GrB (gbnew (A, 'logical')) ;
68        end
69    else
70        % both A and B are matrices.  C is sparse.
71        C = GrB (gb_union_op ('~=', A, B)) ;
72    end
73end
74
75