1 //------------------------------------------------------------------------------
2 // GB_mex_AplusB_M_aliased: compute C<B>=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_M_aliased (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
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])25 void mexFunction
26 (
27 int nargout,
28 mxArray *pargout [ ],
29 int nargin,
30 const mxArray *pargin [ ]
31 )
32 {
33 struct GB_Matrix_opaque C_header ;
34 GrB_Matrix C = GB_clear_static_header (&C_header) ;
35
36 bool malloc_debug = GB_mx_get_global (true) ;
37 GrB_Matrix A = NULL ;
38 GrB_Matrix B = NULL ;
39 GrB_BinaryOp op = NULL ;
40
41 GB_CONTEXT (USAGE) ;
42
43 // check inputs
44 if (nargout > 1 || nargin != 3)
45 {
46 mexErrMsgTxt ("Usage: " USAGE) ;
47 }
48
49 #define GET_DEEP_COPY ;
50 #define FREE_DEEP_COPY ;
51
52 // get A and B
53 A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
54 B = GB_mx_mxArray_to_Matrix (pargin [1], "B", false, true) ;
55 if (A == NULL || B == NULL)
56 {
57 FREE_ALL ;
58 mexErrMsgTxt ("failed") ;
59 }
60
61 // get op
62 bool user_complex = (Complex != GxB_FC64)
63 && (A->type == Complex || B->type == Complex) ;
64 if (!GB_mx_mxArray_to_BinaryOp (&op, pargin [2], "op",
65 A->type, user_complex) || op == NULL)
66 {
67 FREE_ALL ;
68 mexErrMsgTxt ("op failed") ;
69 }
70
71 // C<B> = A+B using the op. M == B alias
72 bool ignore ;
73 METHOD (GB_add (C, A->type, true, B, false, false, &ignore, A, B, op,
74 Context)) ;
75
76 // return C to MATLAB as a plain sparse matrix
77 pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C<B>=A+B result", false) ;
78
79 FREE_ALL ;
80 }
81
82