26#include <boost/algorithm/string.hpp>
45template <
typename _Scalar =
double,
int _Rows = Dynamic,
int _Cols = Dynamic>
47 public Eigen::Matrix<_Scalar, _Rows, _Cols> {
51 static constexpr auto Rows = _Rows;
52 static constexpr auto Cols = _Cols;
55 using Base = Eigen::Matrix<Scalar, Rows, Cols>;
59 static constexpr bool is_vector = (
Cols == 1);
82 template <
typename Z = Self>
85 return Self(Eigen::Map<
const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>>(v.data(), rows, cols));
95 template <
typename Z = Self>
99 Self result = Self::Zero(rows);
101 std::ifstream file {filename};
103 if (file.is_open()) {
106 while (std::getline(file, line)) {
107 std::vector<std::string> splitted_line;
110 boost::split(splitted_line, line, boost::is_any_of(
" \t"), boost::token_compress_on);
112 if (splitted_line.size() != 1) {
113 throw std::runtime_error(
"Matrix::FromFile(std::string, size_t): Found a line that doesn't contain exactly 1 field delimited by whitespace.");
116 auto value = std::stod(splitted_line[0]);
117 result(index) = value;
124 throw std::runtime_error(
"Matrix::FromFile(std::string, size_t): Cannot open the given file. Maybe you specified a wrong path?");
138 template <
typename Z = Self>
141 Self result = Self::Zero(rows, cols);
143 std::ifstream file {filename};
144 if (file.is_open()) {
147 while (std::getline(file, line)) {
148 std::vector<std::string> splitted_line;
151 boost::split(splitted_line, line, boost::is_any_of(
" \t"), boost::token_compress_on);
153 if (splitted_line.size() != 3) {
154 throw std::runtime_error(
"Matrix::FromFile(std::string, size_t, size_t): Found a line that doesn't contain exactly 3 fields delimited by whitespace.");
157 auto i = std::stoi(splitted_line[0]);
158 auto j = std::stoi(splitted_line[1]);
159 auto value = std::stod(splitted_line[2]);
161 result(i, j) = value;
166 throw std::runtime_error(
"Matrix::FromFile(std::string, size_t, size_t): Cannot open the given file. Maybe you specified a wrong path?");
182 template <
typename Z =
bool>
184 return Self::isApprox(other, tolerance);
197 template <
typename Z =
bool>
201 return (Self::isApprox(other, tolerance) || Self::isApprox(-other, tolerance));
214 template <
typename Z =
bool>
218 if (Self::cols() != other.cols()) {
219 throw std::invalid_argument(
"hasEqualSetsOfEigenvectorsAs(MatrixX<double>, MatrixX<double>, double): Cannot compare the two sets of eigenvectors as they have different dimensions.");
222 if (Self::rows() != other.rows()) {
223 throw std::invalid_argument(
"hasEqualSetsOfEigenvectorsAs(MatrixX<double>, MatrixX<double>, double): Cannot compare the two sets of eigenvectors as they have different dimensions.");
227 for (
size_t i = 0; i < Self::cols(); i++) {
247 template <
typename Z = Self>
252 for (
size_t i = 0; i < rows; i++) {
253 for (
size_t j = 0; j < cols; j++) {
254 size_t row_major_index = i * cols + j;
255 size_t column_major_index = j * rows + i;
257 v_column_major(column_major_index) = v(row_major_index);
274 template <
typename Z = Scalar>
277 const auto index =
static_cast<size_t>(direction);
286 template <
typename Z = Scalar&>
289 const auto index =
static_cast<size_t>(direction);
293 using Base::operator();
307 template <
typename Z = Self>
310 Self this_copy = *
this;
335 const auto matrix = *
this;
338 using MatrixType = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
339 Eigen::SelfAdjointEigenSolver<MatrixType> eigensolver {matrix};
342 const auto& eigenvalues = eigensolver.eigenvalues().transpose();
344 if (eigenvalues[0] < threshold) {
362 template <
typename Z = Matrix<Scalar, Rows, 1>>
369 size_t vector_index = 0;
370 for (
size_t j = start_j; j < this->cols(); j++) {
371 for (
size_t i = start_i; i < this->rows(); i++) {
387 template <
typename Z =
void>
390 for (
size_t i = 0; i < this->rows(); i++) {
391 for (
size_t j = 0; j < this->cols(); j++) {
392 output_stream << i <<
' ' << j <<
" " << this->
operator()(i, j) << std::endl;
407 const size_t rows = this->rows();
408 const size_t columns = this->cols() - 1;
412 this->block(0, i, rows, columns - i) = this->rightCols(columns - i);
415 this->conservativeResize(rows, columns);
426 size_t removed_counter = 0;
427 for (
const auto& i : column_indices) {
443 const size_t rows = this->rows() - 1;
444 const size_t columns = this->cols();
448 this->block(i, 0, rows - i, columns) = this->bottomRows(rows - i);
451 this->conservativeResize(rows, columns);
462 size_t removed_counter = 0;
463 for (
const auto& i : row_indices) {
475template <
typename Scalar,
int Rows>
478template <
typename Scalar>
481template <
typename Scalar>
484template <
typename Scalar>
487template <
typename Scalar>
Definition: Matrix.hpp:47
enable_if_t< Self::is_matrix, Z > pairWiseReduced(const size_t start_i=0, size_t start_j=0) const
Definition: Matrix.hpp:363
enable_if_t< Self::is_vector &&(Rows==3), Z > operator()(const CartesianDirection direction) const
Definition: Matrix.hpp:275
void removeColumns(const std::vector< size_t > &column_indices)
Definition: Matrix.hpp:424
enable_if_t< Self::is_vector, Z > areEqualEigenvaluesAs(const Matrix< Scalar, Dynamic, 1 > &other, double tolerance=1.0e-12) const
Definition: Matrix.hpp:183
Base & Eigen()
Definition: Matrix.hpp:326
enable_if_t< Self::is_vector, Z > isEqualEigenvectorAs(const Matrix< Scalar, Dynamic, 1 > &other, double tolerance=1.0e-12) const
Definition: Matrix.hpp:198
void removeRows(const std::vector< size_t > &row_indices)
Definition: Matrix.hpp:460
const bool isPositiveSemiDefinite(const double threshold=-1.0e-5) const
Definition: Matrix.hpp:333
Matrix< Scalar, Rows, Cols > Self
Definition: Matrix.hpp:54
static constexpr auto Rows
Definition: Matrix.hpp:51
enable_if_t< Self::is_matrix, Z > hasEqualSetsOfEigenvectorsAs(const Matrix< Scalar, Dynamic, Dynamic > &other, double tolerance=1.0e-12) const
Definition: Matrix.hpp:215
enable_if_t< Self::is_matrix, Z > print(std::ostream &output_stream=std::cout) const
Definition: Matrix.hpp:388
void removeRow(const size_t i)
Definition: Matrix.hpp:441
const Base & Eigen() const
Definition: Matrix.hpp:321
_Scalar Scalar
Definition: Matrix.hpp:50
static enable_if_t<(Cols==Dynamic) &&(Rows==Dynamic), Z > FromColumnMajorVector(const Matrix< Scalar, Dynamic, 1 > &v, const size_t rows, const size_t cols)
Definition: Matrix.hpp:83
enable_if_t< Self::is_matrix, Z > calculateMinor(size_t i, size_t j) const
Definition: Matrix.hpp:308
static constexpr auto Cols
Definition: Matrix.hpp:52
void removeColumn(const size_t i)
Definition: Matrix.hpp:405
static enable_if_t<(Cols==Dynamic) &&(Rows==Dynamic), Z > FromRowMajorVector(const Matrix< Scalar, Dynamic, 1 > &v, const size_t rows, const size_t cols)
Definition: Matrix.hpp:248
Eigen::Matrix< Scalar, Rows, Cols > Base
Definition: Matrix.hpp:55
static enable_if_t< Self::is_matrix, Z > FromFile(const std::string &filename, size_t rows, size_t cols)
Definition: Matrix.hpp:139
static enable_if_t< Self::is_vector, Z > FromFile(const std::string &filename, size_t rows)
Definition: Matrix.hpp:96
Definition: BaseOneElectronIntegralBuffer.hpp:25
typename std::enable_if< B, T >::type enable_if_t
Definition: type_traits.hpp:37
constexpr auto Dynamic
Definition: Eigen.hpp:27
CartesianDirection
Definition: CartesianDirection.hpp:27
std::function< VectorX< Scalar >(const VectorX< Scalar > &)> VectorFunction
Definition: Matrix.hpp:485
std::function< MatrixX< Scalar >(const VectorX< Scalar > &)> MatrixFunction
Definition: Matrix.hpp:488