1 //------------------------------------------------------------------------------
2 // gb_matrix_assign_scalar: assign scalar into a GraphBLAS matrix
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 
12 #define OK2(method)                                         \
13 {                                                           \
14     GrB_Info info = method ;                                \
15     if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE))    \
16     {                                                       \
17         ERROR ("GrB:error") ;                               \
18     }                                                       \
19 }
20 
21 // if do_subassign true:  GxB_Matrix_subassign_[TYPE]
22 // if do_subassign false: GrB_Matrix_assign_[TYPE]
23 
24 // The input scalar is held as A(0,0) in a GrB_Matrix A.  If A(0,0) is not
25 // present in A, the value zero is used.
26 
gb_matrix_assign_scalar(GrB_Matrix C,const GrB_Matrix M,const GrB_BinaryOp op,const GrB_Matrix A,const GrB_Index * I,const GrB_Index ni,const GrB_Index * J,const GrB_Index nj,const GrB_Descriptor d,bool do_subassign)27 void gb_matrix_assign_scalar
28 (
29     GrB_Matrix C,               // C can be of any type
30     const GrB_Matrix M,
31     const GrB_BinaryOp op,
32     const GrB_Matrix A,         // the scalar is in A(0,0)
33     const GrB_Index *I,
34     const GrB_Index ni,
35     const GrB_Index *J,
36     const GrB_Index nj,
37     const GrB_Descriptor d,
38     bool do_subassign           // true: use GxB_subassign, false: GrB_assign
39 )
40 {
41 
42     GrB_Type atype ;
43     OK (GxB_Matrix_type (&atype, A)) ;
44     if (atype == GrB_BOOL)
45     {
46         bool x = false ;
47         OK2 (GrB_Matrix_extractElement_BOOL (&x, A, 0, 0)) ;
48         if (do_subassign)
49         {
50             OK1 (C, GxB_Matrix_subassign_BOOL (C, M, op, x, I, ni, J, nj, d)) ;
51         }
52         else
53         {
54             OK1 (C, GrB_Matrix_assign_BOOL (C, M, op, x, I, ni, J, nj, d)) ;
55         }
56     }
57     else if (atype == GrB_INT8)
58     {
59         int8_t x = 0 ;
60         OK2 (GrB_Matrix_extractElement_INT8 (&x, A, 0, 0)) ;
61         if (do_subassign)
62         {
63             OK1 (C, GxB_Matrix_subassign_INT8 (C, M, op, x, I, ni, J, nj, d)) ;
64         }
65         else
66         {
67             OK1 (C, GrB_Matrix_assign_INT8 (C, M, op, x, I, ni, J, nj, d)) ;
68         }
69     }
70     else if (atype == GrB_INT16)
71     {
72         int16_t x = 0 ;
73         OK2 (GrB_Matrix_extractElement_INT16 (&x, A, 0, 0)) ;
74         if (do_subassign)
75         {
76             OK (GxB_Matrix_subassign_INT16 (C, M, op, x, I, ni, J, nj, d)) ;
77         }
78         else
79         {
80             OK1 (C, GrB_Matrix_assign_INT16 (C, M, op, x, I, ni, J, nj, d));
81         }
82     }
83     else if (atype == GrB_INT32)
84     {
85         int32_t x = 0 ;
86         OK2 (GrB_Matrix_extractElement_INT32 (&x, A, 0, 0)) ;
87         if (do_subassign)
88         {
89             OK1 (C, GxB_Matrix_subassign_INT32 (C, M, op, x, I, ni, J, nj, d)) ;
90         }
91         else
92         {
93             OK1 (C, GrB_Matrix_assign_INT32 (C, M, op, x, I, ni, J, nj, d)) ;
94         }
95     }
96     else if (atype == GrB_INT64)
97     {
98         int64_t x = 0 ;
99         OK2 (GrB_Matrix_extractElement_INT64 (&x, A, 0, 0)) ;
100         if (do_subassign)
101         {
102             OK1 (C, GxB_Matrix_subassign_INT64 (C, M, op, x, I, ni, J, nj, d)) ;
103         }
104         else
105         {
106             OK1 (C, GrB_Matrix_assign_INT64 (C, M, op, x, I, ni, J, nj, d)) ;
107         }
108     }
109     else if (atype == GrB_UINT8)
110     {
111         uint8_t x = 0 ;
112         OK2 (GrB_Matrix_extractElement_UINT8 (&x, A, 0, 0)) ;
113         if (do_subassign)
114         {
115             OK1 (C, GxB_Matrix_subassign_UINT8 (C, M, op, x, I, ni, J, nj, d)) ;
116         }
117         else
118         {
119             OK1 (C, GrB_Matrix_assign_UINT8 (C, M, op, x, I, ni, J, nj, d)) ;
120         }
121     }
122     else if (atype == GrB_UINT16)
123     {
124         uint16_t x = 0 ;
125         OK2 (GrB_Matrix_extractElement_UINT16 (&x, A, 0, 0)) ;
126         if (do_subassign)
127         {
128             OK1 (C, GxB_Matrix_subassign_UINT16 (C, M, op, x, I, ni, J, nj, d));
129         }
130         else
131         {
132             OK1 (C, GrB_Matrix_assign_UINT16 (C, M, op, x, I, ni, J, nj, d)) ;
133         }
134     }
135     else if (atype == GrB_UINT32)
136     {
137         uint32_t x = 0 ;
138         OK2 (GrB_Matrix_extractElement_UINT32 (&x, A, 0, 0)) ;
139         if (do_subassign)
140         {
141             OK1 (C, GxB_Matrix_subassign_UINT32 (C, M, op, x, I, ni, J, nj, d));
142         }
143         else
144         {
145             OK1 (C, GrB_Matrix_assign_UINT32 (C, M, op, x, I, ni, J, nj, d)) ;
146         }
147     }
148     else if (atype == GrB_UINT64)
149     {
150         uint64_t x = 0 ;
151         OK2 (GrB_Matrix_extractElement_UINT64 (&x, A, 0, 0)) ;
152         if (do_subassign)
153         {
154             OK1 (C, GxB_Matrix_subassign_UINT64 (C, M, op, x, I, ni, J, nj, d));
155         }
156         else
157         {
158             OK1 (C, GrB_Matrix_assign_UINT64 (C, M, op, x, I, ni, J, nj, d)) ;
159         }
160     }
161     else if (atype == GrB_FP32)
162     {
163         float x = 0 ;
164         OK2 (GrB_Matrix_extractElement_FP32 (&x, A, 0, 0)) ;
165         if (do_subassign)
166         {
167             OK1 (C, GxB_Matrix_subassign_FP32 (C, M, op, x, I, ni, J, nj, d)) ;
168         }
169         else
170         {
171             OK1 (C, GrB_Matrix_assign_FP32 (C, M, op, x, I, ni, J, nj, d)) ;
172         }
173     }
174     else if (atype == GrB_FP64)
175     {
176         double x = 0 ;
177         OK2 (GrB_Matrix_extractElement_FP64 (&x, A, 0, 0)) ;
178         if (do_subassign)
179         {
180             OK1 (C, GxB_Matrix_subassign_FP64 (C, M, op, x, I, ni, J, nj, d)) ;
181         }
182         else
183         {
184             OK1 (C, GrB_Matrix_assign_FP64 (C, M, op, x, I, ni, J, nj, d)) ;
185         }
186     }
187     else if (atype == GxB_FC32)
188     {
189         GxB_FC32_t x = GxB_CMPLXF (0,0) ;
190         OK2 (GxB_Matrix_extractElement_FC32 (&x, A, 0, 0)) ;
191         if (do_subassign)
192         {
193             OK1 (C, GxB_Matrix_subassign_FC32 (C, M, op, x, I, ni, J, nj, d)) ;
194         }
195         else
196         {
197             OK1 (C, GxB_Matrix_assign_FC32 (C, M, op, x, I, ni, J, nj, d)) ;
198         }
199     }
200     else if (atype == GxB_FC64)
201     {
202         GxB_FC64_t x = GxB_CMPLX (0,0) ;
203         OK2 (GxB_Matrix_extractElement_FC64 (&x, A, 0, 0)) ;
204         if (do_subassign)
205         {
206             OK1 (C, GxB_Matrix_subassign_FC64 (C, M, op, x, I, ni, J, nj, d)) ;
207         }
208         else
209         {
210             OK1 (C, GxB_Matrix_assign_FC64 (C, M, op, x, I, ni, J, nj, d)) ;
211         }
212     }
213     else
214     {
215         ERROR ("unsupported type") ;
216     }
217 }
218 
219