1 //------------------------------------------------------------------------------
2 // gbfull: add identity values to a matrix so all entries are present
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: GPL-3.0-or-later
7 
8 //------------------------------------------------------------------------------
9 
10 // The input may be either a GraphBLAS matrix struct or a standard MATLAB
11 // sparse or full matrix.  The output is a GraphBLAS matrix by default, with
12 // all entries present, of the given type.  Entries are filled in with the id
13 // value, whose default value is zero.
14 
15 // If desc.kind = 'grb', or if the descriptor is not present, the output is a
16 // GraphBLAS full matrix.  Otherwise the output is a MATLAB full matrix
17 // (desc.kind = 'full').   The two other cases, desc.kind = 'sparse' and
18 // 'matlab' are treated as 'full'.
19 
20 // Usage:
21 //  C = gbfull (A)
22 //  C = gbfull (A, type)
23 //  C = gbfull (A, type, id)
24 //  C = gbfull (A, type, id, desc)
25 
26 #include "gb_matlab.h"
27 
28 #define USAGE "usage: C = gbfull (A, type, id, desc)"
29 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])30 void mexFunction
31 (
32     int nargout,
33     mxArray *pargout [ ],
34     int nargin,
35     const mxArray *pargin [ ]
36 )
37 {
38 
39     //--------------------------------------------------------------------------
40     // check inputs
41     //--------------------------------------------------------------------------
42 
43     gb_usage (nargin >= 1 && nargin <= 4 && nargout <= 2, USAGE) ;
44 
45     //--------------------------------------------------------------------------
46     // get a shallow copy of the input matrix
47     //--------------------------------------------------------------------------
48 
49     GrB_Matrix A = gb_get_shallow (pargin [0]) ;
50     GrB_Index nrows, ncols ;
51     OK (GrB_Matrix_nrows (&nrows, A)) ;
52     OK (GrB_Matrix_ncols (&ncols, A)) ;
53 
54     //--------------------------------------------------------------------------
55     // get the type of C
56     //--------------------------------------------------------------------------
57 
58     GrB_Matrix type ;
59     if (nargin > 1)
60     {
61         type = gb_mxstring_to_type (pargin [1]) ;
62     }
63     else
64     {
65         // the output type defaults to the same as the input type
66         OK (GxB_Matrix_type (&type, A)) ;
67     }
68 
69     //--------------------------------------------------------------------------
70     // get the identity scalar
71     //--------------------------------------------------------------------------
72 
73     GrB_Matrix id = NULL ;
74     if (nargin > 2)
75     {
76         id = gb_get_shallow (pargin [2]) ;
77     }
78 
79     //--------------------------------------------------------------------------
80     // get the descriptor
81     //--------------------------------------------------------------------------
82 
83     base_enum_t base = BASE_DEFAULT ;
84     kind_enum_t kind = KIND_GRB ;
85     GxB_Format_Value fmt = GxB_NO_FORMAT ;
86     int sparsity = 0 ;
87     GrB_Descriptor desc = NULL ;
88     if (nargin > 3)
89     {
90         desc = gb_mxarray_to_descriptor (pargin [nargin-1], &kind, &fmt,
91             &sparsity, &base) ;
92     }
93     OK (GrB_Descriptor_free (&desc)) ;
94 
95     //--------------------------------------------------------------------------
96     // finalize the kind and format
97     //--------------------------------------------------------------------------
98 
99     // ignore desc.kind = 'sparse' or 'matlab' and just use 'full' instead
100     kind = (kind == KIND_SPARSE || kind == KIND_MATLAB) ? KIND_FULL : kind ;
101 
102     if (kind == KIND_FULL)
103     {
104         // MATLAB matrices are always held by column
105         fmt = GxB_BY_COL ;
106     }
107     else
108     {
109         // A determines the format of C, unless defined by the descriptor
110         fmt = gb_get_format (nrows, ncols, A, NULL, fmt) ;
111     }
112 
113     //--------------------------------------------------------------------------
114     // expand A to a full matrix
115     //--------------------------------------------------------------------------
116 
117     GrB_Matrix C = gb_expand_to_full (A, type, fmt, id) ;
118 
119     OK (GrB_Matrix_free (&A)) ;
120     OK (GxB_Scalar_free (&id)) ;
121 
122     //--------------------------------------------------------------------------
123     // export C
124     //--------------------------------------------------------------------------
125 
126     pargout [0] = gb_export (&C, kind) ;
127     pargout [1] = mxCreateDoubleScalar (kind) ;
128     GB_WRAPUP ;
129 }
130 
131