2#include "qip/Vector.hpp"
6#include <gsl/gsl_blas.h>
7#include <gsl/gsl_complex.h>
8#include <gsl/gsl_complex_math.h>
9#include <gsl/gsl_eigen.h>
10#include <gsl/gsl_linalg.h>
11#include <gsl/gsl_math.h>
22struct is_complex<std::complex<T>> : std::true_type {};
76 View(T *
data, std::size_t start, std::size_t
size, std::size_t stride)
77 : m_size(
size), m_stride(stride), m_data(
data + long(start)) {}
80 std::size_t
size()
const {
return m_size; }
83 T &
operator[](std::size_t i) {
return m_data[i * m_stride]; }
85 T
operator[](std::size_t i)
const {
return m_data[i * m_stride]; }
88 T &
at(std::size_t i) {
90 return m_data[i * m_stride];
93 T
at(std::size_t i)
const {
95 return m_data[i * m_stride];
132 : m_rows(m.
rows()), m_cols(m.
cols()), m_data(m.
data()) {}
135 template <
typename U = T,
typename = std::enable_if_t<std::is_const_v<U>>>
137 : m_rows(m.
rows()), m_cols(m.
cols()), m_data(m.
data()) {}
140 std::size_t
rows()
const {
return m_rows; }
142 std::size_t
cols()
const {
return m_cols; }
144 std::size_t
size()
const {
return m_rows * m_cols; }
146 bool empty()
const {
return m_rows == 0 || m_cols == 0; }
151 const T *
data()
const {
return m_data; }
156 const T *
operator[](std::size_t i)
const {
return m_data + i * m_cols; }
159 T &
at(std::size_t i, std::size_t j) {
160 assert(i < m_rows && j < m_cols);
161 return m_data[i * m_cols + j];
164 T
at(std::size_t i, std::size_t j)
const {
165 assert(i < m_rows && j < m_cols);
166 return m_data[i * m_cols + j];
169 const T &
atc(std::size_t i, std::size_t j)
const {
170 assert(i < m_rows && j < m_cols);
171 return m_data[i * m_cols + j];
181 auto end() {
return m_data + m_rows * m_cols; }
182 auto cbegin()
const {
return m_data; }
183 auto cend()
const {
return m_data + m_rows * m_cols; }
207template <
typename T =
double>
213 std::vector<T> m_data{};
230 : m_rows(dimension), m_cols(dimension), m_data(dimension * dimension) {}
234 Matrix(std::initializer_list<std::initializer_list<T>> ll)
237 m_data.reserve(m_rows * m_cols);
239 m_data.insert(m_data.end(), l.begin(), l.end());
245 : m_rows(
rows), m_cols(
cols), m_data{l} {
246 assert(m_data.size() ==
rows *
cols &&
247 "initializer_list must be rows*cols");
252 : m_rows(
rows), m_cols(
cols), m_data{std::forward<std::vector<T>>(v)} {
253 assert(m_data.size() ==
rows *
cols &&
254 "initializer_list must be rows*cols");
258 : m_rows(
rows), m_cols(
cols), m_data{v} {
259 assert(m_data.size() ==
rows *
cols &&
260 "initializer_list must be rows*cols");
281 std::size_t
rows()
const {
return m_rows; }
283 std::size_t
cols()
const {
return m_cols; }
285 std::size_t
size()
const {
return m_data.size(); }
288 bool empty()
const {
return m_data.empty(); }
291 T *
data() {
return m_data.data(); }
293 const T *
data()
const {
return m_data.data(); }
298 T *
operator[](std::size_t i) {
return &(m_data[i * m_cols]); }
300 const T *
operator[](std::size_t i)
const {
return &(m_data[i * m_cols]); }
303 T &
at(std::size_t row_i, std::size_t col_j) {
304 assert(row_i < m_rows && col_j < m_cols);
305 return m_data[row_i * m_cols + col_j];
308 T
at(std::size_t row_i, std::size_t col_j)
const {
309 assert(row_i < m_rows && col_j < m_cols);
310 return m_data[row_i * m_cols + col_j];
314 const T &
atc(std::size_t row_i, std::size_t col_j)
const {
315 assert(row_i < m_rows && col_j < m_cols);
316 return m_data[row_i * m_cols + col_j];
327 auto begin() {
return m_data.begin(); }
328 auto cbegin()
const {
return m_data.cbegin(); }
329 auto end() {
return m_data.end(); }
330 auto cend()
const {
return m_data.cend(); }
335 return m_data.data() + long(
row * m_cols);
340 return View<T>(this->
data(), row * m_cols, m_cols, 1ul);
447 [[nodiscard]]
auto real()
const;
449 [[nodiscard]]
auto imag()
const;
451 [[nodiscard]]
auto complex()
const;
529 template <
typename U>
533 template <
typename U>
543 return trans ? CblasTrans : CblasNoTrans;
564void GEMM(
const Matrix<T> &a,
const Matrix<T> &b, Matrix<T> *c,
565 bool trans_A =
false,
bool trans_B =
false);
605void PENTA_GEMM(
const Matrix<T> &A,
const Matrix<T> &B,
const Matrix<T> &C,
606 const Matrix<T> &D,
const Matrix<T> &E, Matrix<T> *M);
620 PENTA_GEMM<T>(A, B, C, D, E, &M);
651template <
typename T,
bool PARALLEL = false>
652void PENTA(
const Matrix<T> &A,
const Matrix<T> &B,
const Matrix<T> &C,
653 const Matrix<T> &D,
const Matrix<T> &E, Matrix<T> *M);
657template <
typename T,
bool PARALLEL = false>
661 PENTA<T, PARALLEL>(A, B, C, D, E, &M);
671constexpr auto myEps();
688bool equal(
const Matrix<T> &lhs,
const Matrix<T> &rhs, T eps = myEps<T>());
Non-owning 2D view onto a Matrix.
Definition Matrix.hpp:120
std::size_t size() const
Return rows*columns [total array size].
Definition Matrix.hpp:144
T operator()(std::size_t i, std::size_t j) const
() index access with range checking, const
Definition Matrix.hpp:177
T * data()
Raw pointer to first element, mutable.
Definition Matrix.hpp:149
auto begin()
Iterators over flat data buffer.
Definition Matrix.hpp:180
T * operator[](std::size_t i)
[] index access (no range checking). [i] returns pointer to ith row, mutable
Definition Matrix.hpp:154
const T * operator[](std::size_t i) const
[] index access (no range checking). [i] returns const pointer to ith row
Definition Matrix.hpp:156
const T & atc(std::size_t i, std::size_t j) const
at(i,j): element access with range checking, const ref
Definition Matrix.hpp:169
T & at(std::size_t i, std::size_t j)
at(i,j): element access with range checking, mutable
Definition Matrix.hpp:159
Matrix_view(T *data, std::size_t rows, std::size_t cols)
Construct from raw pointer and dimensions (no ownership)
Definition Matrix.hpp:127
T & operator()(std::size_t i, std::size_t j)
() index access with range checking, mutable
Definition Matrix.hpp:175
std::size_t rows() const
Return rows [major index size].
Definition Matrix.hpp:140
bool empty() const
Bool - is empty? (same as size==0)
Definition Matrix.hpp:146
Matrix_view(Matrix< std::remove_const_t< T > > &m)
Implicit conversion from mutable Matrix (works for both mutable and const view)
Definition Matrix.hpp:131
std::size_t cols() const
Return columns [minor index size].
Definition Matrix.hpp:142
T at(std::size_t i, std::size_t j) const
at(i,j): element access with range checking, const
Definition Matrix.hpp:164
const T * data() const
Raw pointer to first element, const.
Definition Matrix.hpp:151
Matrix_view(const Matrix< std::remove_const_t< T > > &m)
Implicit conversion from const Matrix (only for Matrix_view<const T>)
Definition Matrix.hpp:136
Row-major dense matrix with arithmetic and linear algebra support.
Definition Matrix.hpp:208
T at(std::size_t row_i, std::size_t col_j) const
at(i,j): element access with range checking, const
Definition Matrix.hpp:308
const T * row(std::size_t row) const
Returns raw c pointer to start of a row.
Definition Matrix.hpp:334
Matrix< T > & make_identity()
Constructs a diagonal unit matrix (identity), in place; only for square.
Definition Matrix.ipp:126
Matrix< T > & invert_in_place()
Inverts the matrix in place.
Definition Matrix.ipp:54
const T & atc(std::size_t row_i, std::size_t col_j) const
at(i,j): element access with range checking, const ref
Definition Matrix.hpp:314
friend Matrix< T > operator/(Matrix< T > lhs, const T x)
Scalar divide: returns M / x.
Definition Matrix.hpp:508
Matrix(std::size_t rows, std::size_t cols, const std::vector< T > &v)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:257
std::size_t size() const
Return rows*columns [total array size].
Definition Matrix.hpp:285
const T * data() const
Const pointer to first element; for std::complex<T> this is const complex<T>*, not const T*.
Definition Matrix.hpp:293
friend std::ostream & operator<<(std::ostream &os, const Matrix< U > &a)
Prints matrix elements row by row to output stream.
Matrix_view< const T > matrix_view() const
Returns an immutable view of the entire matrix (no ownership, no resize)
Definition Matrix.hpp:360
void resize(std::size_t rows, std::size_t cols, const T &value)
Resizes matrix to new dimension; all values reset to 'value'.
Definition Matrix.hpp:272
friend Matrix< T > operator+(Matrix< T > lhs, const Matrix< T > &rhs)
Elementwise addition: returns M + N.
Definition Matrix.hpp:490
T * data()
Pointer to first element; for std::complex<T> this is complex<T>*, not T*.
Definition Matrix.hpp:291
std::size_t rows() const
Return rows [major index size].
Definition Matrix.hpp:281
friend Matrix< T > operator+(Matrix< T > M, T aI)
Returns M + aI (adds a to diagonal)
Definition Matrix.hpp:519
friend Matrix< U > operator*(const Matrix< U > &a, const Matrix< U > &b)
Matrix multiplication: C_ij = sum_k A_ik*B_kj.
Matrix< T > conj() const
Returns conjugate of matrix.
Definition Matrix.ipp:146
View< T > row_view(std::size_t row)
Returns a mutable 'View' of a row.
Definition Matrix.hpp:339
Matrix< T > & operator+=(const Matrix< T > &rhs)
In-place elementwise addition; dimensions must match.
Definition Matrix.ipp:201
friend Matrix< T > operator*(const T x, Matrix< T > rhs)
Scalar multiply: returns x * M.
Definition Matrix.hpp:500
auto begin()
iterators for underlying std::vector (entire data)
Definition Matrix.hpp:327
T & at(std::size_t row_i, std::size_t col_j)
at(i,j): element access with range checking, mutable
Definition Matrix.hpp:303
auto complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition Matrix.ipp:188
auto imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:177
auto as_gsl_view()
Returns a GSL matrix view for use with GSL functions (no copy).
Definition Matrix.ipp:434
Matrix< T > & conj_in_place()
Conjugates matrix, in place.
Definition Matrix.ipp:157
T * operator[](std::size_t i)
[] index access (no range checking). [i] returns pointer to ith row, mutable
Definition Matrix.hpp:298
Matrix(std::size_t dimension)
Initialise a blank square matrix dimension*dimension, filled with 0.
Definition Matrix.hpp:229
Matrix()
Default initialiser.
Definition Matrix.hpp:217
Matrix(std::size_t rows, std::size_t cols, std::initializer_list< T > l)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:244
friend Matrix< T > operator*(Matrix< T > lhs, const T x)
Scalar multiply: returns M * x.
Definition Matrix.hpp:504
T & operator()(std::size_t i, std::size_t j)
() index access with range checking, mutable
Definition Matrix.hpp:320
Matrix< T > & operator/=(const T x)
In-place scalar divide: M_ij /= x.
Definition Matrix.ipp:234
Matrix< T > transpose() const
Returns the transpose of the matrix.
Definition Matrix.ipp:94
Matrix< T > & zero()
Sets all elements to zero, in place.
Definition Matrix.ipp:137
Matrix< T > inverse() const
Returns inverse of the matrix; original is unchanged.
Definition Matrix.hpp:417
View< const T > row_view(std::size_t row) const
Returns an immutable 'View' of a row.
Definition Matrix.hpp:343
Matrix< T > & operator*=(const T x)
In-place scalar multiply: M_ij *= x.
Definition Matrix.ipp:224
bool empty() const
Bool - is empty? (same as size==0)
Definition Matrix.hpp:288
friend Matrix< T > operator-(Matrix< T > lhs, const Matrix< T > &rhs)
Elementwise subtraction: returns M - N.
Definition Matrix.hpp:495
std::size_t cols() const
Return columns [minor index size].
Definition Matrix.hpp:283
View< T > column_view(std::size_t col)
Returns a mutable 'View' of a column.
Definition Matrix.hpp:347
T determinant() const
Returns the determinant.
Definition Matrix.ipp:23
Matrix< T > & mult_elements_by(const Matrix< T > &a)
Elementwise multiply in place: M_ij *= a_ij.
Definition Matrix.ipp:268
const T * operator[](std::size_t i) const
[] index access (no range checking). [i] returns const pointer to ith row
Definition Matrix.hpp:300
friend Matrix< T > operator-(Matrix< T > M, T aI)
Returns M - aI (subtracts a from diagonal)
Definition Matrix.hpp:523
auto real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:166
Matrix(std::initializer_list< std::initializer_list< T > > ll)
Initialise a matrix from initialiser list. {{},{},{}}. Each row must be same length.
Definition Matrix.hpp:234
Matrix_view< T > matrix_view()
Returns a mutable view of the entire matrix (no ownership, no resize)
Definition Matrix.hpp:356
Matrix(std::size_t rows, std::size_t cols, std::vector< T > &&v)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:251
T operator()(std::size_t i, std::size_t j) const
() index access with range checking, const
Definition Matrix.hpp:322
Matrix(std::size_t rows, std::size_t cols)
Initialise a blank matrix rows*cols, filled with 0.
Definition Matrix.hpp:220
friend Matrix< T > mult_elements(Matrix< T > a, const Matrix< T > &b)
Returns new matrix C with C_ij = A_ij * B_ij (elementwise product).
Definition Matrix.hpp:473
Matrix(std::size_t rows, std::size_t cols, const T &value)
Initialise a matrix rows*cols, filled with 'value'.
Definition Matrix.hpp:224
void resize(std::size_t rows, std::size_t cols)
Resizes matrix to new dimension; all values reset to default.
Definition Matrix.hpp:265
View< const T > column_view(std::size_t col) const
Returns an immutable 'View' of a column.
Definition Matrix.hpp:351
Matrix< T > & operator-=(const Matrix< T > &rhs)
In-place elementwise subtraction; dimensions must match.
Definition Matrix.ipp:212
Non-owning strided view onto a 1D segment of an array.
Definition Matrix.hpp:69
T operator[](std::size_t i) const
[] index access (no range checking), const
Definition Matrix.hpp:85
T * data()
Raw pointer to first viewed element.
Definition Matrix.hpp:103
T operator()(std::size_t i) const
() index access with range checking, const
Definition Matrix.hpp:100
View(T *data, std::size_t start, std::size_t size, std::size_t stride)
Construct view over size elements starting at offset start, with stride.
Definition Matrix.hpp:76
T & operator[](std::size_t i)
[] index access (no range checking), mutable
Definition Matrix.hpp:83
std::size_t size() const
Number of elements in the view.
Definition Matrix.hpp:80
T at(std::size_t i) const
at(i): element access with range checking, const
Definition Matrix.hpp:93
T & at(std::size_t i)
at(i): element access with range checking, mutable
Definition Matrix.hpp:88
T & operator()(std::size_t i)
() index access with range checking, mutable
Definition Matrix.hpp:98
constexpr bool is_complex_v
Type trait: true if T is std::complex<U> for some U, false otherwise.
Definition AdamsMoulton.hpp:80
Linear algebra: matrices, vectors, views, and solvers.
Definition Matrix.hpp:53
void PENTA(const Matrix< T > &A, const Matrix< T > &B, const Matrix< T > &C, const Matrix< T > &D, const Matrix< T > &E, Matrix< T > *M)
5-matrix contraction for N*N matrices: M_ab = A_ai B_aj C_ij D_ib E_jb, without BLAS,...
Definition Matrix.ipp:386
void PENTA_GEMM(const Matrix< T > &A, const Matrix< T > &B, const Matrix< T > &C, const Matrix< T > &D, const Matrix< T > &E, Matrix< T > *M)
5-matrix contraction for N*N matrices: M_ab = A_ai B_aj C_ij D_ib E_jb, with BLAS
Definition Matrix.ipp:352
void GEMM(const Matrix< T > &a, const Matrix< T > &b, Matrix< T > *c, bool trans_A=false, bool trans_B=false)
Matrix multiplication C = op(A) * op(B) via CBLAS GEMM (row-major).
Definition Matrix.ipp:292
CBLAS_TRANSPOSE to_cblas_trans(bool trans)
Converts bool to CBLAS_TRANSPOSE enum (CblasTrans if true, CblasNoTrans if false)
Definition Matrix.hpp:542
constexpr auto myEps()
Default relative tolerance for equal(): 1e-6 for float, 1e-12 for double.
Definition Matrix.ipp:490
bool equal(const Matrix< T > &lhs, const Matrix< T > &rhs, T eps=myEps< T >())
Compares two matrices element-wise to within a relative tolerance.
Definition Matrix.ipp:505
Type trait: true iff T is std::complex<U> for some U.
Definition Matrix.hpp:19