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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
//------------------------------------------------------------------------------
// GB_mex_mdiag: compute C=diag(v,k)
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// C = diag (v,k,ctype)
#include "GB_mex.h"
#define USAGE "C = GB_mex_mdiag (v,k,ctype,csc)"
#define FREE_ALL \
{ \
GrB_Matrix_free_(&V) ; \
GrB_Matrix_free_(&C) ; \
GB_mx_put_global (true) ; \
}
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
bool malloc_debug = GB_mx_get_global (true) ;
GrB_Matrix V = NULL, C = NULL ;
// check inputs
if (nargout > 1 || nargin < 1 || nargin > 4)
{
mexErrMsgTxt ("Usage: " USAGE) ;
}
#define GET_DEEP_COPY ;
#define FREE_DEEP_COPY ;
// get V
V = GB_mx_mxArray_to_Matrix (pargin [0], "V", false, true) ;
if (V == NULL)
{
FREE_ALL ;
mexErrMsgTxt ("V failed") ;
}
if (!GB_VECTOR_OK (V))
{
FREE_ALL ;
mexErrMsgTxt ("V must be a column vector") ;
}
// get k
int64_t GET_SCALAR (1, int64_t, k, 0) ;
// get the type
GrB_Type ctype, vtype ;
GxB_Matrix_type (&vtype, V) ;
ctype = GB_mx_string_to_Type (PARGIN (2), vtype) ;
// get fmt
int GET_SCALAR (3, int, fmt, GxB_BY_COL) ;
// construct C
int64_t n ;
GrB_Matrix_nrows (&n, V) ;
n += GB_IABS (k) ;
#undef FREE_DEEP_COPY
#define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
// C = diag (v,k), using either GrB_Matrix_diag or GxB_Matrix_diag.
// The two methods do the same thing. This is just to test.
if (k % 2 == 0 && ctype == vtype)
{
// GrB_Matrix_diag does not handle typecasting
METHOD (GrB_Matrix_diag (&C, (GrB_Vector) V, k)) ;
}
else
{
#undef GET_DEEP_COPY
#define GET_DEEP_COPY \
GrB_Matrix_new (&C, ctype, n, n) ; \
GxB_Matrix_Option_set (C, GxB_FORMAT, fmt) ;
GET_DEEP_COPY ;
METHOD (GxB_Matrix_diag (C, (GrB_Vector) V, k, NULL)) ;
}
// return C as a struct
pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C=diag(v,k)", true) ;
FREE_ALL ;
}
|