function C = gb_max2 (op, A, B)
%GB_MAX2 2-input max
% Implements C = max (A,B)

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2020, All Rights
% Reserved. http://suitesparse.com.  See GraphBLAS/Doc/License.txt.

[am, an, atype] = gbsize (A) ;
[bm, bn, btype] = gbsize (B) ;
a_is_scalar = (am == 1) && (an == 1) ;
b_is_scalar = (bm == 1) && (bn == 1) ;
ctype = gboptype (atype, btype) ;

if (a_is_scalar)
    if (b_is_scalar)
        % both A and B are scalars.  Result is also a scalar.
        C = gb_union_op (op, A, B) ;
    else
        % A is a scalar, B is a matrix
        if (gb_scalar (A) > 0)
            % since A > 0, the result is full
            A = gb_scalar_to_full (bm, bn, ctype, A) ;
            C = gbeadd (A, op, B) ;
        else
            % since A <= 0, the result is sparse.
            C = gbapply2 (A, op, B) ;
        end
    end
else
    if (b_is_scalar)
        % A is a matrix, B is a scalar
        if (gb_scalar (B) > 0)
            % since B > 0, the result is full
            B = gb_scalar_to_full (am, an, ctype, B) ;
            C = gbeadd (A, op, B) ;
        else
            % since B <= 0, the result is sparse.
            C = gbapply2 (A, op, B) ;
        end
    else
        % both A and B are matrices.  Result is sparse.
        C = gb_union_op (op, A, B) ;
    end
end

