1 //------------------------------------------------------------------------------
2 // GB_mex_mdiag: compute C=diag(v,k)
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 // C = diag (v,k,ctype)
11
12 #include "GB_mex.h"
13
14 #define USAGE "C = GB_mex_mdiag (v,k,ctype,csc)"
15
16 #define FREE_ALL \
17 { \
18 GrB_Matrix_free_(&V) ; \
19 GrB_Matrix_free_(&C) ; \
20 GB_mx_put_global (true) ; \
21 }
22
23
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])24 void mexFunction
25 (
26 int nargout,
27 mxArray *pargout [ ],
28 int nargin,
29 const mxArray *pargin [ ]
30 )
31 {
32
33 bool malloc_debug = GB_mx_get_global (true) ;
34 GrB_Matrix V = NULL, C = NULL ;
35
36 // check inputs
37 if (nargout > 1 || nargin < 1 || nargin > 4)
38 {
39 mexErrMsgTxt ("Usage: " USAGE) ;
40 }
41
42 #define GET_DEEP_COPY ;
43 #define FREE_DEEP_COPY ;
44
45 // get V
46 V = GB_mx_mxArray_to_Matrix (pargin [0], "V", false, true) ;
47 if (V == NULL)
48 {
49 FREE_ALL ;
50 mexErrMsgTxt ("V failed") ;
51 }
52
53 if (!GB_VECTOR_OK (V))
54 {
55 FREE_ALL ;
56 mexErrMsgTxt ("V must be a column vector") ;
57 }
58
59 // get k
60 int64_t GET_SCALAR (1, int64_t, k, 0) ;
61
62 // get the type
63 GrB_Type ctype ;
64 GxB_Matrix_type (&ctype, V) ;
65 ctype = GB_mx_string_to_Type (PARGIN (2), ctype) ;
66
67 // get fmt
68 int GET_SCALAR (3, int, fmt, GxB_BY_COL) ;
69
70 // construct C
71 int64_t n ;
72 GrB_Matrix_nrows (&n, V) ;
73 n += GB_IABS (k) ;
74
75 #undef GET_DEEP_COPY
76 #undef FREE_DEEP_COPY
77
78 #define GET_DEEP_COPY \
79 GrB_Matrix_new (&C, ctype, n, n) ; \
80 GxB_Matrix_Option_set (C, GxB_FORMAT, fmt) ;
81
82 #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
83
84 GET_DEEP_COPY ;
85
86 // C = diag (v,k)
87 METHOD (GxB_Matrix_diag (C, (GrB_Vector) V, k, NULL)) ;
88
89 // return C to MATLAB as a struct
90 pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C=diag(v,k)", true) ;
91 FREE_ALL ;
92 }
93
94