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 100 101 102 103 104
|
//------------------------------------------------------------------------------
// gbmdiag: construct a diaogonal matrix from a vector
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Usage:
// C = gbmdiag (v, k, desc)
#include "gb_interface.h"
#define USAGE "usage: C = gbmdiag (v, k, desc)"
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
gb_usage (nargin >= 1 && nargin <= 3 && nargout <= 2, USAGE) ;
//--------------------------------------------------------------------------
// get the descriptor
//--------------------------------------------------------------------------
base_enum_t base ;
kind_enum_t kind ;
GxB_Format_Value fmt ;
int sparsity ;
GrB_Descriptor desc = NULL ;
desc = gb_mxarray_to_descriptor (pargin [nargin-1], &kind, &fmt,
&sparsity, &base) ;
// if present, remove the descriptor from consideration
if (desc != NULL) nargin-- ;
//--------------------------------------------------------------------------
// get the inputs
//--------------------------------------------------------------------------
GrB_Matrix C = NULL ;
GrB_Matrix V = gb_get_shallow (pargin [0]) ;
int64_t k = 0 ;
int64_t ncols ;
OK (GrB_Matrix_ncols (&ncols, V)) ;
CHECK_ERROR (ncols != 1, "v must be a column vector") ;
int s ;
OK (GxB_Matrix_Option_get (V, GxB_SPARSITY_STATUS, &s)) ;
CHECK_ERROR (s == GxB_HYPERSPARSE, "v cannot be hypersparse") ;
if (nargin > 1)
{
CHECK_ERROR (!gb_mxarray_is_scalar (pargin [1]), "k must be a scalar") ;
double x = mxGetScalar (pargin [1]) ;
k = (int64_t) x ;
CHECK_ERROR ((double) k != x, "k must be an integer scalar") ;
}
//--------------------------------------------------------------------------
// construct C
//--------------------------------------------------------------------------
GrB_Type ctype = NULL ;
int64_t n ;
OK (GxB_Matrix_type (&ctype, V)) ;
OK (GrB_Matrix_nrows (&n, V)) ;
n += GB_IABS (k) ;
fmt = gb_get_format (n, n, NULL, NULL, fmt) ;
C = gb_new (ctype, n, n, fmt, 0) ;
//--------------------------------------------------------------------------
// compute C = diag (v, k)
//--------------------------------------------------------------------------
OK1 (C, GxB_Matrix_diag (C, (GrB_Vector) V, k, desc)) ;
//--------------------------------------------------------------------------
// free shallow copies
//--------------------------------------------------------------------------
OK (GrB_Matrix_free (&V)) ;
OK (GrB_Descriptor_free (&desc)) ;
//--------------------------------------------------------------------------
// export the output matrix C
//--------------------------------------------------------------------------
pargout [0] = gb_export (&C, kind) ;
pargout [1] = mxCreateDoubleScalar (kind) ;
GB_WRAPUP ;
}
|