22#include <eigen3/Eigen/SparseLU>
25 Eigen::SparseMatrix<Real> eigen_A(A.n_rows, A.n_cols);
26 std::vector<Eigen::Triplet<Real>> triplets;
27 Eigen::SparseLU<Eigen::SparseMatrix<Real>, Eigen::COLAMDOrdering<int>> solver;
29 Eigen::VectorXd eigen_x(A.n_rows);
30 triplets.reserve(5 * A.n_rows);
33 while (it != A.end()) {
34 triplets.push_back(Eigen::Triplet<Real>(it.row(), it.col(), *it));
38 eigen_A.setFromTriplets(triplets.begin(), triplets.end());
41 auto b_ = conv_to<std::vector<Real>>::from(b);
42 Eigen::Map<Eigen::VectorXd> eigen_b(b_.data(), b_.size());
44 solver.analyzePattern(eigen_A);
45 solver.factorize(eigen_A);
46 eigen_x = solver.solve(eigen_b);
48 return vec(eigen_x.data(), eigen_x.size());
71 sp_mat::const_iterator itA = A.begin();
72 sp_mat::const_iterator endA = A.end();
73 sp_mat::const_iterator itB = B.begin();
74 sp_mat::const_iterator endB = B.end();
80 umat locations(2, a.n_elem * b.n_elem);
81 vec values(a.n_elem * b.n_elem);
85 locations(0, j) = itA.row() * B.n_rows + itB.row();
86 locations(1, j) = itA.col() * B.n_cols + itB.col();
87 values(j) = (*itA) * (*itB);
96 sp_mat result(locations, values, A.n_rows * B.n_rows, A.n_cols * B.n_cols,
104 sp_mat::const_iterator itA = A.begin();
105 sp_mat::const_iterator endA = A.end();
106 sp_mat::const_iterator itB = B.begin();
107 sp_mat::const_iterator endB = B.end();
113 umat locations(2, a.n_elem + b.n_elem);
114 vec values(a.n_elem + b.n_elem);
116 while (itA != endA) {
117 locations(0, j) = itA.row();
118 locations(1, j) = itA.col();
124 while (itB != endB) {
125 locations(0, j) = itB.row();
126 locations(1, j) = itB.col() + A.n_cols;
132 sp_mat result(locations, values, A.n_rows, A.n_cols + B.n_cols,
true);
139 sp_mat::const_iterator itA = A.begin();
140 sp_mat::const_iterator endA = A.end();
141 sp_mat::const_iterator itB = B.begin();
142 sp_mat::const_iterator endB = B.end();
148 umat locations(2, a.n_elem + b.n_elem);
149 vec values(a.n_elem + b.n_elem);
151 while (itA != endA) {
152 locations(0, j) = itA.row();
153 locations(1, j) = itA.col();
159 while (itB != endB) {
160 locations(0, j) = itB.row() + A.n_rows;
161 locations(1, j) = itB.col();
167 sp_mat result(locations, values, A.n_rows + B.n_rows, A.n_cols,
true);
181 vec t(n, fill::ones);
186 for (
int ii = 0; ii < m; ++ii) {
187 X.col(ii) = x(ii) * t;
191 for (
int ii = 0; ii < m; ++ii)
207 mat sheet(m, n, fill::zeros);
210 vec t(n, fill::ones);
217 for (
int ii = 0; ii < m; ++ii) {
218 sheet.row(ii) = x(ii) * t.t();
222 for (
int kk = 0; kk < o; ++kk)
226 for (
int ii = 0; ii < m; ++ii)
227 sheet.row(ii) = y.t();
229 for (
int kk = 0; kk < o; ++kk)
233 for (
int kk = 0; kk < o; ++kk)
234 Z.slice(kk).fill(z(kk));
static sp_mat spjoin_cols(const sp_mat &A, const sp_mat &B)
An in place operation for joining two matrices by columns.
static sp_mat spjoin_rows(const sp_mat &A, const sp_mat &B)
An in place operation for joining two matrices by rows.
static vec spsolve_eigen(const sp_mat &A, const vec &b)
A wrappper for implementing a sparse solve using Eigen from SuperLU.
static sp_mat spkron(const sp_mat &A, const sp_mat &B)
A wrappper for implementing a sparse Kroenecker product.
void meshgrid(const vec &x, const vec &y, mat &X, mat &Y)
An analog to the MATLAB 2D meshgrid operation.