File: gb_eunion.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 503; asm: 369; python: 125; awk: 10
file content (42 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function C = gb_eunion (A, op, B)
%GB_EUNION C = A+B, sparse matrix 'addition' using the given op.
% The pattern of C is the set union of A and B.  Entries in A but not B,
% or in B but not A, are assumed to have the value zero.  The op is applied
% to all entries in the set union of the pattern of A and B.
%
% The inputs A and B are built-in matrices or GraphBLAS structs (not GrB
% objects).  The result is a typically a GraphBLAS struct.
%
% See also GrB/plus, GrB/minus, GrB/bitxor, GrB/bitor, GrB/hypot.

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

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

if (a_is_scalar)
    if (b_is_scalar)
        % both A and B are scalars.  Result is also a scalar.
        C = gbeadd (A, op, B) ;
    else
        % A is a scalar, B is a matrix.  Result is full.
        % expand A to a full matrix
        A = gb_scalar_to_full (bm, bn, type, gb_fmt (B), A) ;
        C = gbeadd (A, op, B) ;
    end
else
    if (b_is_scalar)
        % A is a matrix, B is a scalar.  Result is full.
        % expand B to a full matrix
        B = gb_scalar_to_full (am, an, type, gb_fmt (A), B) ;
        C = gbeadd (A, op, B) ;
    else
        % both A and B are matrices.  Result is sparse.
        C = gbeunion (A, 0, op, B, 0) ;
    end
end