1 //------------------------------------------------------------------------------
2 // GB_mex_resize: resize a matrix
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 #include "GB_mex.h"
11 
12 #define USAGE "C = GB_mex_resize (A, nrows_new, ncols_new)"
13 
14 #define FREE_ALL                        \
15 {                                       \
16     GrB_Matrix_free_(&C) ;              \
17     GB_mx_put_global (true) ;           \
18 }
19 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])20 void mexFunction
21 (
22     int nargout,
23     mxArray *pargout [ ],
24     int nargin,
25     const mxArray *pargin [ ]
26 )
27 {
28 
29     bool malloc_debug = GB_mx_get_global (true) ;
30     GrB_Matrix C = NULL ;
31 
32     // check inputs
33     if (nargout > 1 || nargin < 1 || nargin > 3)
34     {
35         mexErrMsgTxt ("Usage: " USAGE) ;
36     }
37 
38     #define GET_DEEP_COPY \
39     C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
40     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
41 
42     GET_DEEP_COPY ;
43     if (C == NULL)
44     {
45         FREE_ALL ;
46         mexErrMsgTxt ("C failed") ;
47     }
48 
49     // get vlen_new
50     int64_t GET_SCALAR (1, int64_t, vlen_new, C->vlen) ;
51 
52     // get vdim_new
53     int64_t GET_SCALAR (2, int64_t, vdim_new, C->vdim) ;
54 
55     // resize the matrix
56     if (vlen_new % 5 == 0)
57     {
58         // test the old GxB functions
59         if (GB_VECTOR_OK (C) && vdim_new == 1)
60         {
61             // resize C as a vector
62             METHOD (GxB_Vector_resize ((GrB_Vector) C, vlen_new)) ;
63         }
64         else
65         {
66             // resize C as a matrix
67             METHOD (GxB_Matrix_resize (C, vlen_new, vdim_new)) ;
68         }
69     }
70     else
71     {
72         // test the new GrB functions
73         if (GB_VECTOR_OK (C) && vdim_new == 1)
74         {
75             // resize C as a vector
76             METHOD (GrB_Vector_resize_((GrB_Vector) C, vlen_new)) ;
77         }
78         else
79         {
80             // resize C as a matrix
81             METHOD (GrB_Matrix_resize_(C, vlen_new, vdim_new)) ;
82         }
83     }
84 
85     // return C to MATLAB as a struct and free the GraphBLAS C
86     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
87 
88     FREE_ALL ;
89 }
90 
91