1 //------------------------------------------------------------------------------
2 // GB_mx_mxArray_to_UnaryOp
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 // Convert a MATLAB string or struct to a built-in GraphBLAS UnaryOp.  The
11 // mxArray is either a struct containing two terms: opname (an operator name),
12 // and an optional MATLAB string optype (a string, 'logical', 'double', etc).
13 // If not present, the default type is used (provided on input).
14 //
15 // That is:
16 // op = 'identity' ;    % the GrB_IDENTITY_*, type is default_optype.
17 //
18 // op.opname = 'minv' ; op.type = 'int8' ; % the GrB_MINV_INT8 operator.
19 
20 #include "GB_mex.h"
21 
GB_mx_mxArray_to_UnaryOp(GrB_UnaryOp * op_handle,const mxArray * op_matlab,const char * name,const GrB_Type default_optype,const bool user_complex)22 bool GB_mx_mxArray_to_UnaryOp           // true if successful
23 (
24     GrB_UnaryOp *op_handle,             // the unary op
25     const mxArray *op_matlab,           // MATLAB version of op
26     const char *name,                   // name of the argument
27     const GrB_Type default_optype,      // default operator type
28     const bool user_complex             // if true, use user-defined Complex
29 )
30 {
31     (*op_handle) = NULL ;
32     const mxArray *opname_mx = NULL, *optype_mx = NULL ;
33 
34     if (op_matlab == NULL || mxIsEmpty (op_matlab))
35     {
36         // op is not present; defaults will be used
37         ;
38     }
39     else if (mxIsStruct (op_matlab))
40     {
41         // look for op.opname
42         int fieldnumber = mxGetFieldNumber (op_matlab, "opname") ;
43         if (fieldnumber >= 0)
44         {
45             opname_mx = mxGetFieldByNumber (op_matlab, 0, fieldnumber) ;
46         }
47         // look for op.type
48         fieldnumber = mxGetFieldNumber (op_matlab, "optype") ;
49         if (fieldnumber >= 0)
50         {
51             optype_mx = mxGetFieldByNumber (op_matlab, 0, fieldnumber) ;
52         }
53     }
54     else if (mxIsChar (op_matlab))
55     {
56         // op is a string
57         opname_mx = op_matlab ;
58     }
59     else
60     {
61         mexWarnMsgIdAndTxt ("GB:warn",
62             "unrecognized unary op: must be string or struct") ;
63         return (false) ;
64     }
65 
66     // find the corresponding built-in GraphBLAS operator
67     GrB_UnaryOp op ;
68     if (!GB_mx_string_to_UnaryOp (&op, default_optype,
69         opname_mx, optype_mx, user_complex))
70     {
71         mexWarnMsgIdAndTxt ("GB:warn", "unary op failed") ;
72         return (false) ;
73     }
74 
75     // return the op
76     ASSERT_UNARYOP_OK (op, name, GB0) ;
77     (*op_handle) = op ;
78     return (true) ;
79 }
80 
81