1 //------------------------------------------------------------------------------
2 // GB_mex_AplusB: compute C=A+B
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 // This is for testing only.  See GrB_eWiseAdd instead.  Returns a plain MATLAB
11 // matrix, in double.
12 
13 #include "GB_mex.h"
14 
15 #define USAGE "C = GB_mex_AplusB (A, B, op)"
16 
17 #define FREE_ALL                \
18 {                               \
19     GrB_Matrix_free_(&A) ;      \
20     GrB_Matrix_free_(&B) ;      \
21     GrB_Matrix_free_(&C) ;      \
22     GB_mx_put_global (true) ;   \
23 }
24 
25 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])26 void mexFunction
27 (
28     int nargout,
29     mxArray *pargout [ ],
30     int nargin,
31     const mxArray *pargin [ ]
32 )
33 {
34     struct GB_Matrix_opaque C_header ;
35     GrB_Matrix C = GB_clear_static_header (&C_header) ;
36 
37     bool malloc_debug = GB_mx_get_global (true) ;
38     GrB_Matrix A = NULL ;
39     GrB_Matrix B = NULL ;
40     GrB_BinaryOp op = NULL ;
41 
42     GB_CONTEXT (USAGE) ;
43 
44     // check inputs
45     if (nargout > 1 || nargin != 3)
46     {
47         mexErrMsgTxt ("Usage: " USAGE) ;
48     }
49 
50     #define GET_DEEP_COPY ;
51     #define FREE_DEEP_COPY ;
52 
53     // get A and B
54     A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
55     B = GB_mx_mxArray_to_Matrix (pargin [1], "B", false, true) ;
56     if (A == NULL || B == NULL)
57     {
58         FREE_ALL ;
59         mexErrMsgTxt ("failed") ;
60     }
61 
62     // get op
63     bool user_complex = (Complex != GxB_FC64)
64         && (A->type == Complex || B->type == Complex) ;
65     if (!GB_mx_mxArray_to_BinaryOp (&op, pargin [2], "op",
66         A->type, user_complex) || op == NULL)
67     {
68         FREE_ALL ;
69         mexErrMsgTxt ("op failed") ;
70     }
71 
72     // C = A+B using the op.  No mask
73     bool ignore ;
74     METHOD (GB_add (C, A->type, true, NULL, false, false, &ignore, A, B, op,
75         Context)) ;
76 
77     // return C to MATLAB as a plain sparse matrix
78     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C AplusB result", false) ;
79 
80     FREE_ALL ;
81 }
82 
83