Mimetic Operators Library Enhanced 4.0
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include "utils.h"
2#include <cassert>
3
4#ifdef EIGEN
5#include <eigen3/Eigen/SparseLU>
6vec Utils::spsolve_eigen(const sp_mat &A, const vec &b)
7{
8 Eigen::SparseMatrix<Real> eigen_A(A.n_rows, A.n_cols);
9 std::vector<Eigen::Triplet<Real>> triplets;
10 Eigen::SparseLU<Eigen::SparseMatrix<Real>, Eigen::COLAMDOrdering<int>> solver;
11
12 Eigen::VectorXd eigen_x(A.n_rows);
13 triplets.reserve(5*A.n_rows);
14
15 auto it = A.begin();
16 while(it != A.end()) {
17 triplets.push_back(Eigen::Triplet<Real>(it.row(), it.col(), *it));
18 ++it;
19 }
20
21 eigen_A.setFromTriplets(triplets.begin(), triplets.end());
22 triplets.clear();
23
24 auto b_ = conv_to<std::vector<Real> >::from(b);
25 Eigen::Map<Eigen::VectorXd> eigen_b(b_.data(), b_.size());
26
27 solver.analyzePattern(eigen_A);
28 solver.factorize(eigen_A);
29 eigen_x = solver.solve(eigen_b);
30
31 return vec(eigen_x.data(), eigen_x.size());
32}
33#endif
34
35// Basic implementation of Kronecker product
36/*
37sp_mat Utils::spkron(const sp_mat &A, const sp_mat &B)
38{
39 sp_mat result;
40
41 for (u32 i = 0; i < A.n_rows; i++) {
42 sp_mat BLOCK;
43 for (u32 j = 0; j < A.n_cols; j++) {
44 BLOCK = join_rows(BLOCK, A(i, j)*B);
45 }
46 result = join_cols(result, BLOCK);
47 }
48
49 return result;
50}
51*/
52
53sp_mat Utils::spkron(const sp_mat &A, const sp_mat &B)
54{
55 sp_mat::const_iterator itA = A.begin();
56 sp_mat::const_iterator endA = A.end();
57 sp_mat::const_iterator itB = B.begin();
58 sp_mat::const_iterator endB = B.end();
59 u32 j = 0;
60
61 vec a = nonzeros(A);
62 vec b = nonzeros(B);
63
64 umat locations(2, a.n_elem*b.n_elem);
65 vec values(a.n_elem*b.n_elem);
66
67 while(itA != endA) {
68 while(itB != endB) {
69 locations(0, j) = itA.row()*B.n_rows + itB.row();
70 locations(1, j) = itA.col()*B.n_cols + itB.col();
71 values(j) = (*itA)*(*itB);
72 ++j;
73 ++itB;
74 }
75
76 ++itA;
77 itB = B.begin();
78 }
79
80 sp_mat result(locations, values, A.n_rows*B.n_rows, A.n_cols*B.n_cols, true);
81
82 return result;
83}
84
85sp_mat Utils::spjoin_rows(const sp_mat &A, const sp_mat &B)
86{
87 sp_mat::const_iterator itA = A.begin();
88 sp_mat::const_iterator endA = A.end();
89 sp_mat::const_iterator itB = B.begin();
90 sp_mat::const_iterator endB = B.end();
91 u32 j = 0;
92
93 vec a = nonzeros(A);
94 vec b = nonzeros(B);
95
96 umat locations(2, a.n_elem + b.n_elem);
97 vec values(a.n_elem + b.n_elem);
98
99 while(itA != endA) {
100 locations(0, j) = itA.row();
101 locations(1, j) = itA.col();
102 values(j) = (*itA);
103 ++itA;
104 ++j;
105 }
106
107 while(itB != endB) {
108 locations(0, j) = itB.row();
109 locations(1, j) = itB.col() + A.n_cols;
110 values(j) = (*itB);
111 ++itB;
112 ++j;
113 }
114
115 sp_mat result(locations, values, A.n_rows, A.n_cols+B.n_cols, true);
116
117 return result;
118}
119
120sp_mat Utils::spjoin_cols(const sp_mat &A, const sp_mat &B)
121{
122 sp_mat::const_iterator itA = A.begin();
123 sp_mat::const_iterator endA = A.end();
124 sp_mat::const_iterator itB = B.begin();
125 sp_mat::const_iterator endB = B.end();
126 u32 j = 0;
127
128 vec a = nonzeros(A);
129 vec b = nonzeros(B);
130
131 umat locations(2, a.n_elem + b.n_elem);
132 vec values(a.n_elem + b.n_elem);
133
134 while(itA != endA) {
135 locations(0, j) = itA.row();
136 locations(1, j) = itA.col();
137 values(j) = (*itA);
138 ++itA;
139 ++j;
140 }
141
142 while(itB != endB) {
143 locations(0, j) = itB.row() + A.n_rows;
144 locations(1, j) = itB.col();
145 values(j) = (*itB);
146 ++itB;
147 ++j;
148 }
149
150 sp_mat result(locations, values, A.n_rows+B.n_rows, A.n_cols, true);
151
152 return result;
153}
154
155void Utils::meshgrid( const vec &x, const vec &y, mat &X, mat &Y)
156{
157 int m = x.n_elem;
158 int n = y.n_elem;
159
160 assert (m > 0);
161 assert (n > 0);
162
163 // Build X
164 vec t(n, fill::ones);
165
166 X.zeros(n, m);
167 Y.zeros(n, m);
168
169 for(int ii = 0; ii < m; ++ii) {
170 X.col(ii) = x(ii) * t;
171 t.ones();
172 }
173
174 for(int ii = 0; ii < m; ++ii)
175 Y.col(ii) = y;
176}
177
178void Utils::meshgrid( const vec &x, const vec &y, const vec &z, cube &X, cube &Y, cube &Z)
179{
180 int m = x.n_elem;
181 int n = y.n_elem;
182 int o = z.n_elem;
183
184 assert(m > 0);
185 assert(n > 0);
186 assert(o > 0);
187
188 // Temporary Holder of sheet of cube
189 mat sheet(m, n, fill::zeros);
190
191 // Build X
192 vec t(n, fill::ones);
193
194 X.zeros(m, n, o);
195 Y.zeros(m, n, o);
196 Z.zeros(m, n, o);
197
198 // Sheet that repeats each slice
199 for(int ii = 0; ii < m; ++ii) {
200 sheet.row(ii) = x(ii) * t.t();
201 t.ones();
202 }
203
204 for(int kk = 0; kk < o; ++kk)
205 X.slice(kk) = sheet;
206
207 // Y Cube, repeats same sheet as well
208 for(int ii = 0; ii < m; ++ii)
209 sheet.row(ii) = y.t();
210
211 for(int kk = 0; kk < o; ++kk)
212 Y.slice(kk) = sheet;
213
214 // Z cube goes by slices each with same value
215 for(int kk = 0; kk < o; ++kk)
216 Z.slice(kk).fill(z(kk));
217}
static sp_mat spjoin_cols(const sp_mat &A, const sp_mat &B)
Definition utils.cpp:120
static sp_mat spjoin_rows(const sp_mat &A, const sp_mat &B)
Definition utils.cpp:85
static vec spsolve_eigen(const sp_mat &A, const vec &b)
static sp_mat spkron(const sp_mat &A, const sp_mat &B)
Definition utils.cpp:53
void meshgrid(const vec &x, const vec &y, mat &X, mat &Y)
Definition utils.cpp:155