File: gbnew.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (289 lines) | stat: -rw-r--r-- 9,694 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//------------------------------------------------------------------------------
// gbnew: create a GraphBLAS matrix
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// A may be a built-in sparse matrix, or a built-in struct containing a
// GraphBLAS matrix.  C is returned as a built-in struct containing a GraphBLAS
// matrix.

// Usage:

// C = gbnew (A)
// C = gbnew (A, type)
// C = gbnew (A, format)
// C = gbnew (m, n)
// C = gbnew (m, n, format)
// C = gbnew (m, n, type)
// C = gbnew (A, type, format)
// C = gbnew (A, format, type)
// C = gbnew (m, n, type, format)
// C = gbnew (m, n, format, type)

#include "gb_interface.h"

#define USAGE "usage: C = GrB (m,n,type,format) or C = GrB (A,type,format)"

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    gb_usage (nargin >= 1 && nargin <= 4 && nargout <= 1, USAGE) ;

    //--------------------------------------------------------------------------
    // construct the GraphBLAS matrix
    //--------------------------------------------------------------------------

    GrB_Matrix C ;
    GxB_Format_Value fmt ;
    int sparsity = 0 ;

    if (nargin == 1)
    { 

        //----------------------------------------------------------------------
        // C = GrB (A)
        //----------------------------------------------------------------------

        // GraphBLAS copy of A, same type and format as A
        C = gb_get_deep (pargin [0]) ;

    }
    else if (nargin == 2)
    {

        //----------------------------------------------------------------------
        // C = GrB (A, type)
        // C = GrB (A, format)
        // C = GrB (m, n)
        //----------------------------------------------------------------------

        if (mxIsChar (pargin [1]))
        {

            //------------------------------------------------------------------
            // C = GrB (A, type)
            // C = GrB (A, format)
            //------------------------------------------------------------------

            GrB_Type type = gb_mxstring_to_type (pargin [1]) ;
            bool ok = gb_mxstring_to_format (pargin [1], &fmt, &sparsity) ;

            if (type != NULL)
            {

                //--------------------------------------------------------------
                // C = GrB (A, type)
                //--------------------------------------------------------------

                if (gb_mxarray_is_empty (pargin [0]))
                { 
                    // A is a 0-by-0 built-in matrix.  create a new 0-by-0
                    // GraphBLAS matrix C of the given type, with the default
                    // format.
                    C = gb_new (type, 0, 0, -1, 0) ;
                }
                else
                { 
                    // get a shallow copy and then typecast it to type.
                    // use the same format as A
                    GrB_Matrix A = gb_get_shallow (pargin [0]) ;
                    OK (GxB_Matrix_Option_get (A, GxB_FORMAT, &fmt)) ;
                    C = gb_typecast (A, type, fmt, 0) ;
                    OK (GrB_Matrix_free (&A)) ;
                }

            }
            else if (ok)
            { 

                //--------------------------------------------------------------
                // C = GrB (A, format)
                //--------------------------------------------------------------

                // get a shallow copy of A
                GrB_Matrix A = gb_get_shallow (pargin [0]) ;
                // C = A with the requested format and sparsity, no typecast
                C = gb_typecast (A, NULL, fmt, sparsity) ;
                OK (GrB_Matrix_free (&A)) ;

            }
            else
            { 
                ERROR ("unknown type or format") ;
            }

        }
        else if (gb_mxarray_is_scalar (pargin [0]) &&
                 gb_mxarray_is_scalar (pargin [1]))
        { 

            //------------------------------------------------------------------
            // C = GrB (m, n)
            //------------------------------------------------------------------

            // m-by-n GraphBLAS double matrix, no entries, default format
            GrB_Index nrows = gb_mxget_uint64_scalar (pargin [0], "m") ;
            GrB_Index ncols = gb_mxget_uint64_scalar (pargin [1], "n") ;
            C = gb_new (GrB_FP64, nrows, ncols, -1, 0) ;

        }
        else
        { 
            ERROR ("usage: C=GrB(m,n), C=GrB(A,type), or C=GrB(A,format)") ;
        }

    }
    else if (nargin == 3)
    {

        //----------------------------------------------------------------------
        // C = GrB (m, n, format)
        // C = GrB (m, n, type)
        // C = GrB (A, type, format)
        // C = GrB (A, format, type)
        //----------------------------------------------------------------------

        if (gb_mxarray_is_scalar (pargin [0]) &&
            gb_mxarray_is_scalar (pargin [1]) && mxIsChar (pargin [2]))
        {

            //------------------------------------------------------------------
            // C = GrB (m, n, format)
            // C = GrB (m, n, type)
            //------------------------------------------------------------------

            // create an m-by-n matrix with no entries
            GrB_Index nrows = gb_mxget_uint64_scalar (pargin [0], "m") ;
            GrB_Index ncols = gb_mxget_uint64_scalar (pargin [1], "n") ;
            GrB_Type type = gb_mxstring_to_type (pargin [2]) ;
            bool ok = gb_mxstring_to_format (pargin [2], &fmt, &sparsity) ;

            if (type != NULL)
            { 
                // create an m-by-n matrix of the desired type, no entries,
                // use the default format.
                C = gb_new (type, nrows, ncols, -1, sparsity) ;
            }
            else if (ok)
            { 
                // create an m-by-n double matrix of the desired format
                C = gb_new (GrB_FP64, nrows, ncols, fmt, sparsity) ;
            }
            else
            { 
                ERROR ("unknown type or format") ;
            }

        }
        else if (mxIsChar (pargin [1]) && mxIsChar (pargin [2]))
        {

            //------------------------------------------------------------------
            // C = GrB (A, type, format)
            // C = GrB (A, format, type)
            //------------------------------------------------------------------

            GrB_Type type = gb_mxstring_to_type (pargin [1]) ;
            bool ok = gb_mxstring_to_format (pargin [2], &fmt, &sparsity) ;

            if (ok)
            { 
                // C = GrB (A, type, format)
            }
            else
            { 
                // C = GrB (A, format, type)
                ok = gb_mxstring_to_format (pargin [1], &fmt, &sparsity) ;
                type = gb_mxstring_to_type (pargin [2]) ;
            }

            if (type == NULL || !ok)
            { 
                ERROR ("unknown type and/or format") ;
            }

            if (gb_mxarray_is_empty (pargin [0]))
            { 
                C = gb_new (type, 0, 0, fmt, sparsity) ;
            }
            else
            { 
                // get a shallow copy, typecast it, and set the format
                GrB_Matrix A = gb_get_shallow (pargin [0]) ;
                C = gb_typecast (A, type, fmt, sparsity) ;
                OK (GrB_Matrix_free (&A)) ;
            }
        }
        else
        { 
            ERROR ("unknown usage") ;
        }

    }
    else // if (nargin == 4)
    {

        //----------------------------------------------------------------------
        // C = GrB (m, n, type, format)
        // C = GrB (m, n, format, type)
        //----------------------------------------------------------------------

        if (gb_mxarray_is_scalar (pargin [0]) &&
            gb_mxarray_is_scalar (pargin [1]) &&
            mxIsChar (pargin [2]) && mxIsChar (pargin [3]))
        {

            // create an m-by-n matrix with no entries, of the requested
            // type and format
            GrB_Index nrows = gb_mxget_uint64_scalar (pargin [0], "m") ;
            GrB_Index ncols = gb_mxget_uint64_scalar (pargin [1], "n") ;

            GrB_Type type = gb_mxstring_to_type (pargin [2]) ;
            bool ok = gb_mxstring_to_format (pargin [3], &fmt, &sparsity) ;

            if (ok)
            { 
                // C = GrB (m, n, type, format)
            }
            else
            { 
                // C = GrB (m, n, format, type)
                ok = gb_mxstring_to_format (pargin [2], &fmt, &sparsity) ;
                type = gb_mxstring_to_type (pargin [3]) ;
            }

            if (type == NULL || !ok)
            { 
                ERROR ("unknown type and/or format") ;
            }

            C = gb_new (type, nrows, ncols, fmt, sparsity) ;
        }
        else
        { 
            ERROR ("unknown usage") ;
        }
    }

    //--------------------------------------------------------------------------
    // export the output matrix C as a GraphBLAS matrix
    //--------------------------------------------------------------------------

    pargout [0] = gb_export (&C, KIND_GRB) ;
    GB_WRAPUP ;
}