18#ifndef EIGEN_MATRIX_BASE_HPP
19#define EIGEN_MATRIX_BASE_HPP
23#include <external/Eigen/Dense>
24#include <external/Eigen/IterativeLinearSolvers>
25#include <external/EigenUnsupported/Eigen/IterativeSolvers>
45template <
typename T,
typename MatrixType>
141 matrix_ = std::make_shared<MatrixType> (
matrix);
156 matrix_ = std::move(
matrix);
167 return matrix_->rows();
177 return matrix_->cols();
187 return matrix_->size();
229 matrix_->setIdentity();
243 (*matrix_) =
xd.raw_matrix().transpose();
255 xd.raw_matrix() = matrix_->diagonal().asDiagonal();
353 Eigen::GMRES<MatrixType, Eigen::IdentityPreconditioner>
solver;
357 solver.setMaxIterations(1000);
361 if (
solver.info() != Eigen::Success)
362 throw std::runtime_error(
"Matrix solver initialization failed.");
367 xd.raw_matrix() =
solver.solve(
bd.raw_matrix());
369 std::cout <<
"GMRES iterations: " <<
solver.iterations() <<
" | tolerance: " <<
solver.tolerance() <<
" | residual: " <<
solver.error() << std::endl;
375 if (
solver.info() != Eigen::Success)
376 std::cout <<
"Warning: GMRES solve failed." << std::endl;
392 Eigen::BiCGSTAB<MatrixType, Eigen::IdentityPreconditioner>
solver;
396 solver.setMaxIterations(1000);
399 if (
solver.info() != Eigen::Success)
400 throw std::runtime_error(
"Matrix solver initialization failed.");
405 xd.raw_matrix() =
solver.solve(
bd.raw_matrix());
407 std::cout <<
"BiCGStab iterations: " <<
solver.iterations() <<
" | tolerance: " <<
solver.tolerance() <<
" | residual: " <<
solver.error() << std::endl;
413 if (
solver.info() != Eigen::Success)
414 std::cout <<
"Warning: BiCGStab solve failed." << std::endl;
429 Eigen::BDCSVD<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>
svd (
430 (Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>)
raw_matrix()
433 return s(0) /
s(
s.size() - 1);
440 void print(
const std::string name =
"matrix")
const override
442 std::cout <<
"\n--- " << name <<
" (" <<
num_rows() <<
" x " <<
num_cols() <<
") ---" << std::endl;
444 std::cout <<
"---" << std::endl;
451 std::shared_ptr<MatrixType> matrix_ =
nullptr;
Base class for Eigen-based matrices.
void clear() override
Clears all data in the matrix and sets its size to 0.
Index num_cols() const override
Returns the total number of rows in the matrix.
void set_identity() override
Sets the matrix to identity (ones along the diagonal).
virtual void add_block(const MatrixBase< T > &x, Index row_start, Index col_start, const T &a=T(1))=0
Adds a block of values to this matrix from the values of a given matrix, starting at a given position...
Index num_rows() const override
Returns the total number of rows in the matrix.
const MatrixType & raw_matrix() const
Returns a read-only reference to the underlying raw matrix.
void mat_solve_bicgstab(MatrixBase< T > &x, const MatrixBase< T > &b, const Float tol=1e-3)
Solves for matrix with the BiCGSTAB iterative solver, where is this matrix, and is a given right-...
void set_transpose(const MatrixBase< T > &x)
Computes where is this matrix, is a given matrix, and is its transpose.
Index size() const override
Returns the size (rows * cols) of the matrix.
virtual void get_block(MatrixBase< T > &x, Index row_start, Index col_start, Index b_rows, Index b_cols) const =0
Retrieves a block of values from this matrix.
void resize(Index rows, Index cols) override
Resizes the matrix to a new number of rows and columns.
void get_diagonal(MatrixBase< T > &x) const
Retrieves matrix values on the diagonal, zeroing out all other entries.
void bind_raw_matrix(std::shared_ptr< MatrixType > matrix)
Binds the underlying raw matrix to the data pointed at by a given matrix pointer.
void mat_solve_gmres(MatrixBase< T > &x, const MatrixBase< T > &b, const Float tol=1e-3, const Index restart=100)
Solves for matrix with the GMRES iterative solver, where is this matrix, and is a given right-han...
virtual void set_block(const MatrixBase< T > &x, Index row_start, Index col_start, const T &a=T(1))=0
Sets a block of values in this matrix to the values of a given matrix, starting at a given position.
void print(const std::string name="matrix") const override
Prints the matrix to the terminal in a formatted manner.
void set_axpby(const MatrixBase< T > &x, const MatrixBase< T > &y, const T &a=T(1), const T &b=T(1))
Computes where is this matrix, and are scalars, and and are matrices.
void mat_solve_iterative(MatrixBase< T > &x, const MatrixBase< T > &b, const Float tol=1e-3)
Solves for matrix with an iterative solver, where is this matrix, and is a given right-hand side ...
Float cond() const
Computes the condition number (ratio of largest to smallest singular value) of the matrix.
void set_mat_mul(const MatrixBase< T > &x, const MatrixBase< T > &y, const T &a=T(1))
Computes where is this matrix, is a scalar, and and are matrices.
EigenMatrixBase()
Constructs an empty zero-size EigenMatrixBase object.
void set_zero() override
Sets all matrix entries to zero.
void set_raw_matrix(const MatrixType &matrix)
Sets the underlying raw matrix by copying over data from a given matrix.
void add_mat_mul(const MatrixBase< T > &x, const MatrixBase< T > &y, const T &a=T(1))
Computes where is this matrix, is a scalar, and and are matrices.
void add_ax(const MatrixBase< T > &x, const T &a=T(1)) override
Computes where is this matrix, is a scalar, and is a matrix.
MatrixType & raw_matrix()
Returns a writable reference to the underlying raw matrix - use with caution.
EigenMatrixBase(Index rows, Index cols)
Constructs an EigenMatrixBase object with a specified number of rows and columns.
Base class for for matrix algebra containers.
double Float
Floating point number.
Eigen::Matrix< T, N, 1 > EigColVecN
Fixed-size column vector of size N containing type T.
std::size_t Index
Unsigned integer type for indices and container sizes.
Primary namespace for the OpenBEM library.