1 //------------------------------------------------------------------------------
2 // GxB_Matrix_Option_set: set an option in 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_transpose.h"
11 
12 #define GB_FREE_ALL ;
13 
GxB_Matrix_Option_set(GrB_Matrix A,GxB_Option_Field field,...)14 GrB_Info GxB_Matrix_Option_set      // set an option in a matrix
15 (
16     GrB_Matrix A,                   // descriptor to modify
17     GxB_Option_Field field,         // option to change
18     ...                             // value to change it to
19 )
20 {
21 
22     //--------------------------------------------------------------------------
23     // check inputs
24     //--------------------------------------------------------------------------
25 
26     GrB_Info info ;
27     GB_WHERE (A, "GxB_Matrix_Option_set (A, field, value)") ;
28     GB_BURBLE_START ("GxB_set") ;
29     GB_RETURN_IF_NULL_OR_FAULTY (A) ;
30     ASSERT_MATRIX_OK (A, "A to set option", GB0) ;
31 
32     //--------------------------------------------------------------------------
33     // set the matrix option
34     //--------------------------------------------------------------------------
35 
36     va_list ap ;
37 
38     switch (field)
39     {
40 
41         case GxB_HYPER_SWITCH :
42 
43             {
44                 va_start (ap, field) ;
45                 double hyper_switch = va_arg (ap, double) ;
46                 va_end (ap) ;
47                 A->hyper_switch = (float) hyper_switch ;
48             }
49             break ;
50 
51         case GxB_BITMAP_SWITCH :
52 
53             {
54                 va_start (ap, field) ;
55                 double bitmap_switch = va_arg (ap, double) ;
56                 va_end (ap) ;
57                 A->bitmap_switch = (float) bitmap_switch ;
58             }
59             break ;
60 
61         case GxB_SPARSITY_CONTROL :
62 
63             {
64                 va_start (ap, field) ;
65                 int sparsity = va_arg (ap, int) ;
66                 va_end (ap) ;
67                 A->sparsity = GB_sparsity_control (sparsity, (int64_t) (-1)) ;
68             }
69             break ;
70 
71         case GxB_FORMAT :
72 
73             {
74                 va_start (ap, field) ;
75                 int format = va_arg (ap, int) ;
76                 va_end (ap) ;
77                 if (! (format == GxB_BY_ROW || format == GxB_BY_COL))
78                 {
79                     return (GrB_INVALID_VALUE) ;
80                 }
81                 // the value is normally GxB_BY_ROW (0) or GxB_BY_COL (1), but
82                 // any nonzero value results in GxB_BY_COL.
83                 bool new_csc = (format != GxB_BY_ROW) ;
84                 // conform the matrix to the new CSR/CSC format
85                 if (A->is_csc != new_csc)
86                 {
87                     // A = A', done in-place, and change to the new format.
88                     // transpose: no typecast, no op, in-place of A
89                     GB_BURBLE_N (GB_NNZ (A), "(transpose) ") ;
90                     GB_OK (GB_transpose (NULL, NULL, new_csc, A, // in_place_A
91                         NULL, NULL, NULL, false, Context)) ;
92                     ASSERT (A->is_csc == new_csc) ;
93                     ASSERT (GB_JUMBLED_OK (A)) ;
94                 }
95             }
96             break ;
97 
98         default :
99 
100             return (GrB_INVALID_VALUE) ;
101     }
102 
103     //--------------------------------------------------------------------------
104     // conform the matrix to its new desired sparsity structure
105     //--------------------------------------------------------------------------
106 
107     GB_OK (GB_conform (A, Context)) ;
108     GB_BURBLE_END ;
109     ASSERT_MATRIX_OK (A, "A set", GB0) ;
110     return (GrB_SUCCESS) ;
111 }
112 
113