Tutorial_sparse_example.cpp File Reference
#include <Eigen/Sparse>
#include <vector>
#include <iostream>

Typedefs

typedef Eigen::SparseMatrix< doubleSpMat
 
typedef Eigen::Triplet< doubleT
 

Functions

void buildProblem (std::vector< T > &coefficients, Eigen::VectorXd &b, int n)
 
void saveAsBitmap (const Eigen::VectorXd &x, int n, const char *filename)
 
int main (int argc, char **argv)
 

Typedef Documentation

◆ SpMat

◆ T

Function Documentation

◆ buildProblem()

void buildProblem ( std::vector< T > &  coefficients,
Eigen::VectorXd &  b,
int  n 
)
21  {
22  b.setZero();
23  Eigen::ArrayXd boundary = Eigen::ArrayXd::LinSpaced(n, 0, M_PI).sin().pow(2);
24  for (int j = 0; j < n; ++j) {
25  for (int i = 0; i < n; ++i) {
26  int id = i + j * n;
27  insertCoefficient(id, i - 1, j, -1, coefficients, b, boundary);
28  insertCoefficient(id, i + 1, j, -1, coefficients, b, boundary);
29  insertCoefficient(id, i, j - 1, -1, coefficients, b, boundary);
30  insertCoefficient(id, i, j + 1, -1, coefficients, b, boundary);
31  insertCoefficient(id, i, j, 4, coefficients, b, boundary);
32  }
33  }
34 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
void insertCoefficient(int id, int i, int j, double w, std::vector< T > &coeffs, Eigen::VectorXd &b, const Eigen::VectorXd &boundary)
Definition: Tutorial_sparse_example_details.cpp:8
Scalar * b
Definition: benchVecAdd.cpp:17
#define M_PI
Definition: main.h:121
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References b, i, insertCoefficient(), j, M_PI, and n.

Referenced by main().

◆ main()

int main ( int argc  ,
char **  argv 
)
11  {
12  if (argc != 2) {
13  std::cerr << "Error: expected one and only one argument.\n";
14  return -1;
15  }
16 
17  int n = 300; // size of the image
18  int m = n * n; // number of unknowns (=number of pixels)
19 
20  // Assembly:
21  std::vector<T> coefficients; // list of non-zeros coefficients
22  Eigen::VectorXd b(m); // the right hand side-vector resulting from the constraints
23  buildProblem(coefficients, b, n);
24 
25  SpMat A(m, m);
26  A.setFromTriplets(coefficients.begin(), coefficients.end());
27 
28  // Solving:
29  Eigen::SimplicialCholesky<SpMat> chol(A); // performs a Cholesky factorization of A
30  Eigen::VectorXd x = chol.solve(b); // use the factorization to solve for the given right hand side
31 
32  // Export the result to a file:
33  saveAsBitmap(x, n, argv[1]);
34 
35  return 0;
36 }
void buildProblem(std::vector< T > &coefficients, Eigen::VectorXd &b, int n)
Definition: Tutorial_sparse_example_details.cpp:21
void saveAsBitmap(const Eigen::VectorXd &x, int n, const char *filename)
Definition: Tutorial_sparse_example_details.cpp:36
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Definition: SimplicialCholesky.h:694
int * m
Definition: level2_cplx_impl.h:294
list x
Definition: plotDoE.py:28

References b, buildProblem(), m, n, saveAsBitmap(), Eigen::SparseSolverBase< Derived >::solve(), and plotDoE::x.

◆ saveAsBitmap()

void saveAsBitmap ( const Eigen::VectorXd &  x,
int  n,
const char filename 
)
36  {
38  QImage img(bits.data(), n, n, QImage::Format_Indexed8);
39  img.setColorCount(256);
40  for (int i = 0; i < 256; i++) img.setColor(i, qRgb(i, i, i));
41  img.save(filename);
42 }
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:48
Map< const Array< unsigned char, sizeof(T), 1 > > bits(const T &x)
Definition: packetmath_test_shared.h:36
string filename
Definition: MergeRestartFiles.py:39

References Eigen::test::bits(), MergeRestartFiles::filename, i, n, and plotDoE::x.

Referenced by main().