1 //------------------------------------------------------------------------------
2 // gb_expand_to_full: 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 #include "gb_matlab.h"
11 
gb_expand_to_full(const GrB_Matrix A,GrB_Type type,GxB_Format_Value fmt,GrB_Matrix id)12 GrB_Matrix gb_expand_to_full    // C = full (A), and typecast
13 (
14     const GrB_Matrix A,         // input matrix to expand to full
15     GrB_Type type,              // type of C, if NULL use the type of A
16     GxB_Format_Value fmt,       // format of C
17     GrB_Matrix id               // identity value, use zero if NULL
18 )
19 {
20 
21     //--------------------------------------------------------------------------
22     // get the size and type of A
23     //--------------------------------------------------------------------------
24 
25     GrB_Type atype ;
26     GrB_Index nrows, ncols ;
27     OK (GrB_Matrix_nrows (&nrows, A)) ;
28     OK (GrB_Matrix_ncols (&ncols, A)) ;
29     OK (GxB_Matrix_type (&atype, A)) ;
30 
31     // C defaults to the same type of A
32     if (type == NULL)
33     {
34         type = atype ;
35     }
36 
37     //--------------------------------------------------------------------------
38     // get the identity, use zero if NULL
39     //--------------------------------------------------------------------------
40 
41     GrB_Matrix id2 = NULL ;
42     if (id == NULL)
43     {
44         OK (GrB_Matrix_new (&id2, type, 1, 1)) ;
45         id = id2 ;
46     }
47 
48     //--------------------------------------------------------------------------
49     // expand the identity into a full matrix B the same size as C
50     //--------------------------------------------------------------------------
51 
52     GrB_Matrix B = gb_new (type, nrows, ncols, fmt, 0) ;
53     gb_matrix_assign_scalar (B, NULL, NULL, id, GrB_ALL, 0, GrB_ALL, 0, NULL,
54         false) ;
55 
56     //--------------------------------------------------------------------------
57     // typecast A from float to integer using the MATLAB rules
58     //--------------------------------------------------------------------------
59 
60     GrB_Matrix S, T = NULL ;
61     if (gb_is_integer (type) && gb_is_float (atype))
62     {
63         // T = (type) round (A)
64         T = gb_new (type, nrows, ncols, fmt, 0) ;
65         OK1 (T, GrB_Matrix_apply (T, NULL, NULL, gb_round_binop (atype), A,
66             NULL)) ;
67         S = T ;
68     }
69     else
70     {
71         // T = A, and let GrB_Matrix_eWiseAdd_BinaryOp do the typecasting
72         S = A ;
73     }
74 
75     //--------------------------------------------------------------------------
76     // C = first (S, B)
77     //--------------------------------------------------------------------------
78 
79     GrB_Matrix C = gb_new (type, nrows, ncols, fmt, 0) ;
80     OK1 (C, GrB_Matrix_eWiseAdd_BinaryOp (C, NULL, NULL,
81         gb_first_binop (type), S, B, NULL)) ;
82 
83     //--------------------------------------------------------------------------
84     // free workspace and return result
85     //--------------------------------------------------------------------------
86 
87     OK (GrB_Matrix_free (&id2)) ;
88     OK (GrB_Matrix_free (&B)) ;
89     OK (GrB_Matrix_free (&T)) ;
90     return (C) ;
91 }
92 
93