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