5#include <eigen3/Eigen/SparseLU>
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;
12 Eigen::VectorXd eigen_x(A.n_rows);
13 triplets.reserve(5*A.n_rows);
16 while(it != A.end()) {
17 triplets.push_back(Eigen::Triplet<Real>(it.row(), it.col(), *it));
21 eigen_A.setFromTriplets(triplets.begin(), triplets.end());
24 auto b_ = conv_to<std::vector<Real> >::from(b);
25 Eigen::Map<Eigen::VectorXd> eigen_b(b_.data(), b_.size());
27 solver.analyzePattern(eigen_A);
28 solver.factorize(eigen_A);
29 eigen_x = solver.solve(eigen_b);
31 return vec(eigen_x.data(), eigen_x.size());
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();
64 umat locations(2, a.n_elem*b.n_elem);
65 vec values(a.n_elem*b.n_elem);
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);
80 sp_mat result(locations, values, A.n_rows*B.n_rows, A.n_cols*B.n_cols,
true);
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();
96 umat locations(2, a.n_elem + b.n_elem);
97 vec values(a.n_elem + b.n_elem);
100 locations(0, j) = itA.row();
101 locations(1, j) = itA.col();
108 locations(0, j) = itB.row();
109 locations(1, j) = itB.col() + A.n_cols;
115 sp_mat result(locations, values, A.n_rows, A.n_cols+B.n_cols,
true);
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();
131 umat locations(2, a.n_elem + b.n_elem);
132 vec values(a.n_elem + b.n_elem);
135 locations(0, j) = itA.row();
136 locations(1, j) = itA.col();
143 locations(0, j) = itB.row() + A.n_rows;
144 locations(1, j) = itB.col();
150 sp_mat result(locations, values, A.n_rows+B.n_rows, A.n_cols,
true);
164 vec t(n, fill::ones);
169 for(
int ii = 0; ii < m; ++ii) {
170 X.col(ii) = x(ii) * t;
174 for(
int ii = 0; ii < m; ++ii)
178void Utils::meshgrid(
const vec &x,
const vec &y,
const vec &z, cube &X, cube &Y, cube &Z)
189 mat sheet(m, n, fill::zeros);
192 vec t(n, fill::ones);
199 for(
int ii = 0; ii < m; ++ii) {
200 sheet.row(ii) = x(ii) * t.t();
204 for(
int kk = 0; kk < o; ++kk)
208 for(
int ii = 0; ii < m; ++ii)
209 sheet.row(ii) = y.t();
211 for(
int kk = 0; kk < o; ++kk)
215 for(
int kk = 0; kk < o; ++kk)
216 Z.slice(kk).fill(z(kk));
static sp_mat spjoin_cols(const sp_mat &A, const sp_mat &B)
static sp_mat spjoin_rows(const sp_mat &A, const sp_mat &B)
static vec spsolve_eigen(const sp_mat &A, const vec &b)
static sp_mat spkron(const sp_mat &A, const sp_mat &B)
void meshgrid(const vec &x, const vec &y, mat &X, mat &Y)