1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_MATRIX_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_MATRIX_H
44 throw std::runtime_error(
"data buffer is of the wrong size");
49 throw std::runtime_error(
"data buffer is of the wrong size");
67 std::vector<T>&&
data() && {
82 throw std::out_of_range(
"index is larger than size");
98 return Slice(*
this, std::move(upper_left), std::move(bottom_right));
111 return CSlice(*
this, std::move(upper_left), std::move(bottom_right));
129 if (dims.second ==
m_cols) {
135 auto bottom_right =
coord_type{std::min(
rows(), m.rows()), std::min(
cols(), m.cols())};
136 m.slice(upper_left, bottom_right) =
slice(upper_left, bottom_right);
144 throw std::runtime_error(
"row is out of range");
152 throw std::runtime_error(
"column is out of range");
181 :
mat(matrix),
upl(std::move(upper_left)),
btr(std::move(bottom_right)) {
183 throw std::runtime_error(
"specified incorrect corners");
184 if (
upl.first < 0 ||
upl.second < 0)
185 throw std::runtime_error(
"slice is out of range");
187 throw std::runtime_error(
"slice is out of range");
197 throw std::runtime_error(
"attempting to copy slices of different dimensions");
198 for (
size_t i = 0; i <
dimensions().first; ++i) {
199 for (
size_t j = 0; j <
dimensions().second; ++j) {
200 mat(
upl.first + i,
upl.second + j) = right.mat(right.upl.first + i, right.upl.second + j);
211 throw std::runtime_error(
"attempting to copy slices of different dimensions");
212 for (
size_t i = 0; i <
dimensions().first; ++i) {
213 for (
size_t j = 0; j <
dimensions().second; ++j) {
227 for (
size_t i = 0; i <
dimensions().first; ++i)
228 for (
size_t j = 0; j <
dimensions().second; ++j)
235 for (
size_t i = 0; i <
dimensions().first; ++i)
236 for (
size_t j = 0; j <
dimensions().second; ++j)
267 :
m_slice(const_cast<
Matrix<T>&>(matrix), std::move(upper_left), std::move(bottom_right)) {}
273 T operator()(
size_t i,
size_t j)
const {
return const_cast<Slice&
>(
m_slice)(i, j); }
280template <
class ML,
class MR>
282 assert(ml.rows() == mr.cols() && ml.cols() == mr.rows());
283 for (
size_t i = 0; i < ml.rows(); ++i)
284 for (
size_t j = 0; j < ml.cols(); ++j)
290 auto s = std::stringstream{};
291 s << std::setprecision(precision);
292 auto dims = m.dimensions();
293 if (dims.first * dims.second != 0) {
295 for (
size_t i = 0; i < dims.first; ++i) {
297 for (
size_t j = 0; j < dims.second; ++j) {
299 if (j != dims.second - 1)
303 if (i != dims.first - 1)
Constant slice that cannot be assigned to.
Definition: Matrix.h:262
CSlice(CSlice &&) noexcept=default
Slice m_slice
Definition: Matrix.h:276
CSlice(const Matrix< T > &matrix, coord_type upper_left, coord_type bottom_right)
Definition: Matrix.h:266
CSlice(const CSlice &)=delete
Proxy mapping to a rectangular slice of the matrix data. Implements simple assignment.
Definition: Matrix.h:178
coord_type upl
upper left corner
Definition: Matrix.h:257
Slice & operator=(const CSlice &right)
Definition: Matrix.h:241
Slice(Slice &&) noexcept=default
coord_type dimensions() const
Definition: Matrix.h:251
Matrix< T > & mat
matrix being sliced
Definition: Matrix.h:256
Slice & fill(T a)
Fill all elements of the slice with new values.
Definition: Matrix.h:234
Slice(Matrix< T > &matrix, coord_type upper_left, coord_type bottom_right)
Definition: Matrix.h:180
Slice(const Slice &)=delete
size_t rows() const
Definition: Matrix.h:252
T operator()(size_t i, size_t j) const
Definition: Matrix.h:207
T & operator()(size_t i, size_t j)
Definition: Matrix.h:206
coord_type btr
bottom right corner
Definition: Matrix.h:258
Slice & operator=(const Matrix< T > &right)
Definition: Matrix.h:246
size_t cols() const
Definition: Matrix.h:253
Slice & axpy(T a, const CSlice &x)
Definition: Matrix.h:220
Slice & scal(T a)
Scale all elements of the slice.
Definition: Matrix.h:226
Slice & axpy(T a, const Slice &x)
Definition: Matrix.h:209
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition: Matrix.h:28
Matrix(const std::vector< T > &data, coord_type dims)
Definition: Matrix.h:46
index_type m_rows
number of rows
Definition: Matrix.h:171
Slice slice()
Access the whole matrix as a slice.
Definition: Matrix.h:102
T operator()(index_type i, index_type j) const
Access element.
Definition: Matrix.h:62
Slice row(size_t i)
Access row slice.
Definition: Matrix.h:118
T value_type
Definition: Matrix.h:34
Slice col(size_t j)
Access column slice.
Definition: Matrix.h:122
std::pair< size_t, size_t > coord_type
Definition: Matrix.h:36
void remove_row(index_type row)
removes a row from the matrix
Definition: Matrix.h:142
Slice slice(coord_type upper_left, coord_type bottom_right)
Access a rectangular slice of the matrix.
Definition: Matrix.h:97
std::vector< T > m_buffer
data buffer
Definition: Matrix.h:173
void remove_row_col(index_type row, index_type col)
removes row and column
Definition: Matrix.h:160
CSlice row(size_t i) const
Definition: Matrix.h:119
Matrix(const Matrix< T > &)=default
CSlice col(size_t j) const
Definition: Matrix.h:123
CSlice slice(coord_type upper_left, coord_type bottom_right) const
Access a constant rectangular slice of the matrix.
Definition: Matrix.h:110
const std::vector< T > & data() const &
Access the underlying data buffer.
Definition: Matrix.h:65
void clear()
Clears all elements and sets dimensions to 0.
Definition: Matrix.h:77
void remove_col(index_type col)
removes a column from the matrix
Definition: Matrix.h:150
coord_type dimensions() const
Definition: Matrix.h:167
Matrix(coord_type dims)
Definition: Matrix.h:39
size_t size() const
Definition: Matrix.h:168
bool empty() const
Returns true if matrix is empty.
Definition: Matrix.h:74
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:41
void fill(T value)
Sets all elements of matrix to value.
Definition: Matrix.h:89
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:126
CSlice slice() const
Access the whole matrix as a slice.
Definition: Matrix.h:115
index_type rows() const
Definition: Matrix.h:165
coord_type to_coord(size_t ind) const
Converts index of 1D data to matrix coordinate.
Definition: Matrix.h:80
size_t index_type
Definition: Matrix.h:35
index_type cols() const
Definition: Matrix.h:166
std::vector< T > && data() &&
Access the raw data buffer of an r-value matrix. The matrix is left empty.
Definition: Matrix.h:67
index_type m_cols
number of columns
Definition: Matrix.h:172
std::string as_string(const Mat &m, int precision=6)
Definition: Matrix.h:289
void transpose_copy(ML &&ml, const MR &mr)
Definition: Matrix.h:281