-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKronecker_Product.h
More file actions
137 lines (113 loc) · 3.26 KB
/
Kronecker_Product.h
File metadata and controls
137 lines (113 loc) · 3.26 KB
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
//
// Created by simonepanzeri on 25/11/2021.
//
#ifndef DEV_FDAPDE_KRONECKER_PRODUCT_H
#define DEV_FDAPDE_KRONECKER_PRODUCT_H
#include "FdaPDE.h"
//DENSE
/*
SpMat kroneckerProduct(const SpMat& A, const SpMat& B)
{
UInt Nr = A.rows();
UInt Nc = A.cols();
UInt Mr = B.rows();
UInt Mc = B.cols();
MatrixXr AB_dense(Nr*Mr, Nc*Mc);
MatrixXr A_dense = Eigen::MatrixXd(A);
for (UInt i = 0; i < Nr; ++i)
for (UInt j = 0; j < Nc; ++j)
AB_dense.block(i*Mr, j*Mc, Mr, Mc) = A_dense.coeffRef(i,j)*B;
SpMat AB = AB_dense.sparseView();
AB.makeCompressed();
return(AB);
}
*/
// SPARSE
/*
SpMat kroneckerProduct(const SpMat& A, const SpMat& B)
{
UInt Nr = A.rows();
UInt Nc = A.cols();
UInt Mr = B.rows();
UInt Mc = B.cols();
SpMat AB(Nr*Mr, Nc*Mc);
Real a;
for (UInt i = 0; i < Nr; ++i) {
for (UInt j = 0; j < Nc; ++j) {
a = A.coeff(i,j);
if(a != 0) {
for (UInt k = 0; k < Mr; ++k)
for (UInt l = 0; l < Mc; ++l) AB.insert(Mr*i+k, Mc*j+l) = a*B.coeff(k,l);
}
}
}
return(AB);
}
*/
SpMat kroneckerProduct(const SpMat& A, const SpMat& B) {
UInt Ar = A.rows();
UInt Ac = A.cols();
UInt Br = B.rows();
UInt Bc = B.cols();
const Real *Avalues = A.valuePtr();
const UInt *Ainner = A.innerIndexPtr();
const UInt *Aouter = A.outerIndexPtr();
UInt Anz = A.nonZeros();
const Real *Bvalues = B.valuePtr();
const UInt *Binner = B.innerIndexPtr();
const UInt *Bouter = B.outerIndexPtr();
UInt Bnz = B.nonZeros();
UInt ABr = Ar * Br;
UInt ABc = Ac * Bc;
UInt ABnz = Anz * Bnz;
//Real *ABvalues = new Real[ABnz];
//UInt *ABinner = new UInt[ABnz];
//UInt *ABouter = new UInt[ABc+1];
std::vector<Real> ABvalues(ABnz);
std::vector<UInt> ABinner(ABnz);
std::vector<UInt> ABouter(ABc+1);
ABouter[0] = 0.0;
UInt Acurrent = Aouter[0];
UInt ij = 0;
UInt iijj = 0;
for (UInt i = 1; i <= Ac; i++) {
UInt Bcurrent = Bouter[0];
for (UInt j = 1; j <= Bc; j++) {
ij++;
ABouter[ij] = ABouter[ij-1] + (Aouter[i]-Aouter[i-1]) * (Bouter[j]-Bouter[j-1]);
for (UInt ii = Acurrent; ii < Aouter[i]; ii++) {
for (UInt jj = Bcurrent; jj < Bouter[j]; jj++) {
ABinner[iijj] = Ainner[ii] * Br + Binner[jj];
ABvalues[iijj] = Avalues[ii] * Bvalues[jj];
iijj++;
}
}
Bcurrent = Bouter[j];
}
Acurrent = Aouter[i];
}
Eigen::MappedSparseMatrix<Real, Eigen::ColMajor, UInt> ABM(ABr, ABc, ABnz, ABouter.data(), ABinner.data(), ABvalues.data());
SpMat AB(ABM);
// delete[] ABvalues;
// delete[] ABinner;
// delete[] ABouter;
return(AB);
}
MatrixXr kroneckerProduct_Matrix (const MatrixXr& A, const MatrixXr& B)
{
MatrixXr C;
C.resize(A.rows()*B.rows(),A.cols()*B.cols());
UInt r = 0, c = 0;
for (UInt i = 0; i < A.rows(); i++) {
for (UInt k = 0; k < B.rows(); k++) {
for (UInt j = 0; j < A.cols(); j++) {
for (UInt l = 0; l < B.cols(); l++){
C(r,c) = A(i,j) * B(k,l); c++;
}
}
c=0; r++;
}
}
return C;
}
#endif //DEV_FDAPDE_KRONECKER_PRODUCT_H