1function C = and (A, B)
2%& logical AND.
3% C = (A & B) is the element-by-element logical AND 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/or, GrB/xor, GrB/not.
7
8% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
9% SPDX-License-Identifier: GPL-3.0-or-later
10
11if (isobject (A))
12    A = A.opaque ;
13end
14
15if (isobject (B))
16    B = B.opaque ;
17end
18
19[am, an] = gbsize (A) ;
20[bm, bn] = gbsize (B) ;
21a_is_scalar = (am == 1) && (an == 1) ;
22b_is_scalar = (bm == 1) && (bn == 1) ;
23
24if (a_is_scalar)
25    if (b_is_scalar)
26        % A and B are scalars
27        C = GrB (gbemult (A, '&.logical', B)) ;
28    else
29        % A is a scalar, B is a matrix
30        if (gb_scalar (A) == 0)
31            % A is false, so C is empty, the same size as B
32            C = GrB (gbnew (bm, bn, 'logical')) ;
33        else
34            % A is true, so C is B typecasted to logical
35            C = GrB (gbnew (B, 'logical')) ;
36        end
37    end
38else
39    if (b_is_scalar)
40        % A is a matrix, B is a scalar
41        if (gb_scalar (B) == 0)
42            % B is false, so C is empty, the same size as A
43            C = GrB (gbnew (am, an, 'logical')) ;
44        else
45            % B is true, so C is A typecasted to logical
46            C = GrB (gbnew (A, 'logical')) ;
47        end
48    else
49        % both A and B are matrices.  C is the set intersection of A and B
50        C = GrB (gbemult (A, '&.logical', B)) ;
51    end
52end
53
54