1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_MATRIX_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_MATRIX_H
46 throw std::runtime_error(
"data buffer is of the wrong size");
51 throw std::runtime_error(
"data buffer is of the wrong size");
69 std::vector<T>&&
data() && {
84 throw std::out_of_range(
"index is larger than size");
100 return Slice(*
this, std::move(upper_left), std::move(bottom_right));
113 return CSlice(*
this, std::move(upper_left), std::move(bottom_right));
131 if (dims.second ==
m_cols) {
137 auto bottom_right =
coord_type{std::min(
rows(), m.rows()), std::min(
cols(), m.cols())};
138 m.slice(upper_left, bottom_right) =
slice(upper_left, bottom_right);
146 throw std::runtime_error(
"row is out of range");
154 throw std::runtime_error(
"column is out of range");
183 :
mat(matrix),
upl(std::move(upper_left)),
btr(std::move(bottom_right)) {
185 throw std::runtime_error(
"specified incorrect corners");
186 if (
upl.first < 0 ||
upl.second < 0)
187 throw std::runtime_error(
"slice is out of range");
189 throw std::runtime_error(
"slice is out of range");
199 throw std::runtime_error(
"attempting to copy slices of different dimensions");
200 for (
size_t i = 0; i <
dimensions().first; ++i) {
201 for (
size_t j = 0; j <
dimensions().second; ++j) {
202 mat(
upl.first + i,
upl.second + j) = right.mat(right.upl.first + i, right.upl.second + j);
213 throw std::runtime_error(
"attempting to copy slices of different dimensions");
214 for (
size_t i = 0; i <
dimensions().first; ++i) {
215 for (
size_t j = 0; j <
dimensions().second; ++j) {
229 for (
size_t i = 0; i <
dimensions().first; ++i)
230 for (
size_t j = 0; j <
dimensions().second; ++j)
237 for (
size_t i = 0; i <
dimensions().first; ++i)
238 for (
size_t j = 0; j <
dimensions().second; ++j)
269 :
m_slice(const_cast<
Matrix<T>&>(matrix), std::move(upper_left), std::move(bottom_right)) {}
275 T operator()(
size_t i,
size_t j)
const {
return const_cast<Slice&
>(
m_slice)(i, j); }
282template <
class ML,
class MR>
284 assert(ml.rows() == mr.cols() && ml.cols() == mr.rows());
285 for (
size_t i = 0; i < ml.rows(); ++i)
286 for (
size_t j = 0; j < ml.cols(); ++j)
292 auto s = std::stringstream{};
293 s << std::setprecision(precision);
294 auto dims = m.dimensions();
295 if (dims.first * dims.second != 0) {
297 for (
size_t i = 0; i < dims.first; ++i) {
299 for (
size_t j = 0; j < dims.second; ++j) {
301 if (j != dims.second - 1)
305 if (i != dims.first - 1)
318struct std::formatter<
molpro::linalg::itsolv::subspace::Matrix<T>> : std::formatter<std::string_view> {
319 using Base = std::formatter<std::string_view>;
321 std::size_t precision = 6;
323 constexpr auto parse(std::format_parse_context &ctx) {
324 auto it = ctx.begin();
326 if (it == ctx.end()) {
331 return Base::parse(ctx);
336 if (it == ctx.end() || ( *it < '0' && *it >
'9' )) {
337 throw std::format_error(
"Format string uses precision quantifier '.' without giving a (numeric) precision");
343 while (it != ctx.end() && ( *it >=
'0' && *it <=
'9' )) {
345 precision += *it -
'0';
351 return Base::parse(ctx);
357 return Base::format(tmp, ctx);
Constant slice that cannot be assigned to.
Definition: Matrix.h:264
CSlice(CSlice &&) noexcept=default
Slice m_slice
Definition: Matrix.h:278
CSlice(const Matrix< T > &matrix, coord_type upper_left, coord_type bottom_right)
Definition: Matrix.h:268
CSlice(const CSlice &)=delete
Proxy mapping to a rectangular slice of the matrix data. Implements simple assignment.
Definition: Matrix.h:180
coord_type upl
upper left corner
Definition: Matrix.h:259
Slice & operator=(const CSlice &right)
Definition: Matrix.h:243
Slice(Slice &&) noexcept=default
coord_type dimensions() const
Definition: Matrix.h:253
Matrix< T > & mat
matrix being sliced
Definition: Matrix.h:258
Slice & fill(T a)
Fill all elements of the slice with new values.
Definition: Matrix.h:236
Slice(Matrix< T > &matrix, coord_type upper_left, coord_type bottom_right)
Definition: Matrix.h:182
Slice(const Slice &)=delete
size_t rows() const
Definition: Matrix.h:254
T operator()(size_t i, size_t j) const
Definition: Matrix.h:209
T & operator()(size_t i, size_t j)
Definition: Matrix.h:208
coord_type btr
bottom right corner
Definition: Matrix.h:260
Slice & operator=(const Matrix< T > &right)
Definition: Matrix.h:248
size_t cols() const
Definition: Matrix.h:255
Slice & axpy(T a, const CSlice &x)
Definition: Matrix.h:222
Slice & scal(T a)
Scale all elements of the slice.
Definition: Matrix.h:228
Slice & axpy(T a, const Slice &x)
Definition: Matrix.h:211
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition: Matrix.h:30
Matrix(const std::vector< T > &data, coord_type dims)
Definition: Matrix.h:48
index_type m_rows
number of rows
Definition: Matrix.h:173
Slice slice()
Access the whole matrix as a slice.
Definition: Matrix.h:104
T operator()(index_type i, index_type j) const
Access element.
Definition: Matrix.h:64
Slice row(size_t i)
Access row slice.
Definition: Matrix.h:120
T value_type
Definition: Matrix.h:36
Slice col(size_t j)
Access column slice.
Definition: Matrix.h:124
std::pair< size_t, size_t > coord_type
Definition: Matrix.h:38
void remove_row(index_type row)
removes a row from the matrix
Definition: Matrix.h:144
Slice slice(coord_type upper_left, coord_type bottom_right)
Access a rectangular slice of the matrix.
Definition: Matrix.h:99
std::vector< T > m_buffer
data buffer
Definition: Matrix.h:175
void remove_row_col(index_type row, index_type col)
removes row and column
Definition: Matrix.h:162
CSlice row(size_t i) const
Definition: Matrix.h:121
Matrix(const Matrix< T > &)=default
CSlice col(size_t j) const
Definition: Matrix.h:125
CSlice slice(coord_type upper_left, coord_type bottom_right) const
Access a constant rectangular slice of the matrix.
Definition: Matrix.h:112
const std::vector< T > & data() const &
Access the underlying data buffer.
Definition: Matrix.h:67
void clear()
Clears all elements and sets dimensions to 0.
Definition: Matrix.h:79
void remove_col(index_type col)
removes a column from the matrix
Definition: Matrix.h:152
coord_type dimensions() const
Definition: Matrix.h:169
Matrix(coord_type dims)
Definition: Matrix.h:41
size_t size() const
Definition: Matrix.h:170
bool empty() const
Returns true if matrix is empty.
Definition: Matrix.h:76
Matrix(std::vector< T > &&data, coord_type dims)
Construct a matrix by taking ownership of an existing data buffer which must be of correct size.
Definition: Matrix.h:43
void fill(T value)
Sets all elements of matrix to value.
Definition: Matrix.h:91
Matrix(Matrix< T > &&) noexcept=default
void resize(const coord_type &dims)
Resize the matrix. The old data is preserved and any new rows/cols are zeroed.
Definition: Matrix.h:128
CSlice slice() const
Access the whole matrix as a slice.
Definition: Matrix.h:117
index_type rows() const
Definition: Matrix.h:167
coord_type to_coord(size_t ind) const
Converts index of 1D data to matrix coordinate.
Definition: Matrix.h:82
size_t index_type
Definition: Matrix.h:37
index_type cols() const
Definition: Matrix.h:168
std::vector< T > && data() &&
Access the raw data buffer of an r-value matrix. The matrix is left empty.
Definition: Matrix.h:69
index_type m_cols
number of columns
Definition: Matrix.h:174
std::string as_string(const Mat &m, int precision=6)
Definition: Matrix.h:291
void transpose_copy(ML &&ml, const MR &mr)
Definition: Matrix.h:283