1 //------------------------------------------------------------------------------
2 // GrB_kronecker: Kronecker product
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_kron.h"
11 
12 //------------------------------------------------------------------------------
13 // GrB_Matrix_kronecker_BinaryOp: Kronecker product with binary operator
14 //------------------------------------------------------------------------------
15 
GrB_Matrix_kronecker_BinaryOp(GrB_Matrix C,const GrB_Matrix M,const GrB_BinaryOp accum,const GrB_BinaryOp op,const GrB_Matrix A,const GrB_Matrix B,const GrB_Descriptor desc)16 GrB_Info GrB_Matrix_kronecker_BinaryOp  // C<M> = accum (C, kron(A,B))
17 (
18     GrB_Matrix C,                   // input/output matrix for results
19     const GrB_Matrix M,             // optional mask for C, unused if NULL
20     const GrB_BinaryOp accum,       // optional accum for Z=accum(C,T)
21     const GrB_BinaryOp op,          // defines '*' for T=kron(A,B)
22     const GrB_Matrix A,             // first input:  matrix A
23     const GrB_Matrix B,             // second input: matrix B
24     const GrB_Descriptor desc       // descriptor for C, M, A, and B
25 )
26 {
27 
28     //--------------------------------------------------------------------------
29     // check inputs
30     //--------------------------------------------------------------------------
31 
32     GB_WHERE (C, "GrB_Matrix_kronecker_BinaryOp "
33         "(C, M, accum, op, A, B, desc)") ;
34     GB_BURBLE_START ("GrB_kronecker") ;
35 
36     // get the descriptor
37     GB_GET_DESCRIPTOR (info, desc, C_replace, Mask_comp, Mask_struct,
38         A_tran, B_tran, xx, xx7) ;
39 
40     //--------------------------------------------------------------------------
41     // C = kron(A,B)
42     //--------------------------------------------------------------------------
43 
44     // C<M> = accum (C,T) where T = kron(A,B), or with A' and/or B'
45     info = GB_kron (
46         C,          C_replace,      // C matrix and its descriptor
47         M, Mask_comp, Mask_struct,  // mask matrix and its descriptor
48         accum,                      // for accum (C,T)
49         op,                         // operator that defines T=kron(A,B)
50         A,          A_tran,         // A matrix and its descriptor
51         B,          B_tran,         // B matrix and its descriptor
52         Context) ;
53 
54     GB_BURBLE_END ;
55     return (info) ;
56 }
57 
58 //------------------------------------------------------------------------------
59 // GrB_Matrix_kronecker_Monoid: Kronecker product with monoid
60 //------------------------------------------------------------------------------
61 
GrB_Matrix_kronecker_Monoid(GrB_Matrix C,const GrB_Matrix M,const GrB_BinaryOp accum,const GrB_Monoid monoid,const GrB_Matrix A,const GrB_Matrix B,const GrB_Descriptor desc)62 GrB_Info GrB_Matrix_kronecker_Monoid  // C<M> = accum (C, kron(A,B))
63 (
64     GrB_Matrix C,                   // input/output matrix for results
65     const GrB_Matrix M,             // optional mask for C, unused if NULL
66     const GrB_BinaryOp accum,       // optional accum for Z=accum(C,T)
67     const GrB_Monoid monoid,        // defines '*' for T=kron(A,B)
68     const GrB_Matrix A,             // first input:  matrix A
69     const GrB_Matrix B,             // second input: matrix B
70     const GrB_Descriptor desc       // descriptor for C, M, A, and B
71 )
72 {
73 
74     //--------------------------------------------------------------------------
75     // check inputs
76     //--------------------------------------------------------------------------
77 
78     GB_WHERE (C, "GrB_Matrix_kronecker_Monoid "
79         "(C, M, accum, op, monoid, B, desc)");
80     GB_BURBLE_START ("GrB_kronecker") ;
81     GB_RETURN_IF_NULL_OR_FAULTY (monoid) ;
82 
83     // get the descriptor
84     GB_GET_DESCRIPTOR (info, desc, C_replace, Mask_comp, Mask_struct,
85         A_tran, B_tran, xx, xx7) ;
86 
87     //--------------------------------------------------------------------------
88     // C = kron(A,B)
89     //--------------------------------------------------------------------------
90 
91     // C<M> = accum (C,T) where T = kron(A,B), or with A' and/or B'
92     info = GB_kron (
93         C,          C_replace,      // C matrix and its descriptor
94         M, Mask_comp, Mask_struct,  // mask matrix and its descriptor
95         accum,                      // for accum (C,T)
96         monoid->op,                 // operator that defines T=kron(A,B)
97         A,          A_tran,         // A matrix and its descriptor
98         B,          B_tran,         // B matrix and its descriptor
99         Context) ;
100 
101     GB_BURBLE_END ;
102     return (info) ;
103 }
104 
105 //------------------------------------------------------------------------------
106 // GrB_Matrix_kronecker_Semiring: Kronecker product with semiring
107 //------------------------------------------------------------------------------
108 
GrB_Matrix_kronecker_Semiring(GrB_Matrix C,const GrB_Matrix M,const GrB_BinaryOp accum,const GrB_Semiring semiring,const GrB_Matrix A,const GrB_Matrix B,const GrB_Descriptor desc)109 GrB_Info GrB_Matrix_kronecker_Semiring  // C<M> = accum (C, kron(A,B))
110 (
111     GrB_Matrix C,                   // input/output matrix for results
112     const GrB_Matrix M,             // optional mask for C, unused if NULL
113     const GrB_BinaryOp accum,       // optional accum for Z=accum(C,T)
114     const GrB_Semiring semiring,    // defines '*' for T=kron(A,B)
115     const GrB_Matrix A,             // first input:  matrix A
116     const GrB_Matrix B,             // second input: matrix B
117     const GrB_Descriptor desc       // descriptor for C, M, A, and B
118 )
119 {
120 
121     //--------------------------------------------------------------------------
122     // check inputs
123     //--------------------------------------------------------------------------
124 
125     GB_WHERE (C, "GrB_Matrix_kronecker_Semiring "
126         "(C, M, accum, semiring, A, B, desc)") ;
127     GB_BURBLE_START ("GrB_kronecker") ;
128     GB_RETURN_IF_NULL_OR_FAULTY (semiring) ;
129 
130     // get the descriptor
131     GB_GET_DESCRIPTOR (info, desc, C_replace, Mask_comp, Mask_struct,
132         A_tran, B_tran, xx, xx7) ;
133 
134     //--------------------------------------------------------------------------
135     // C = kron(A,B)
136     //--------------------------------------------------------------------------
137 
138     // C<M> = accum (C,T) where T = kron(A,B), or with A' and/or B'
139     info = GB_kron (
140         C,          C_replace,      // C matrix and its descriptor
141         M, Mask_comp, Mask_struct,  // mask matrix and its descriptor
142         accum,                      // for accum (C,T)
143         semiring->multiply,         // operator that defines T=kron(A,B)
144         A,          A_tran,         // A matrix and its descriptor
145         B,          B_tran,         // B matrix and its descriptor
146         Context) ;
147 
148     GB_BURBLE_END ;
149     return (info) ;
150 }
151 
152 //------------------------------------------------------------------------------
153 // GxB_kron: Kronecker product (historical)
154 //------------------------------------------------------------------------------
155 
156 // GrB_kronecker is now a built-in operation; use that one instead.  They
157 // compute the same thing.  New user code should switch from GxB_kron to
158 // GrB_kronecker.  The new name is preferred.  The old name will be kept for
159 // historical compatibility.
160 
GxB_kron(GrB_Matrix C,const GrB_Matrix M,const GrB_BinaryOp accum,const GrB_BinaryOp op,const GrB_Matrix A,const GrB_Matrix B,const GrB_Descriptor desc)161 GrB_Info GxB_kron                   // C<M> = accum (C, kron(A,B))
162 (
163     GrB_Matrix C,                   // input/output matrix for results
164     const GrB_Matrix M,             // optional mask for C, unused if NULL
165     const GrB_BinaryOp accum,       // optional accum for Z=accum(C,T)
166     const GrB_BinaryOp op,          // defines '*' for T=kron(A,B)
167     const GrB_Matrix A,             // first input:  matrix A
168     const GrB_Matrix B,             // second input: matrix B
169     const GrB_Descriptor desc       // descriptor for C, M, A, and B
170 )
171 {
172     // call the new GrB_kronecker (the binary op version)
173     return (GrB_Matrix_kronecker_BinaryOp (C, M, accum, op, A, B, desc)) ;
174 }
175 
176