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 {};
77 View(T *
data, std::size_t start, std::size_t
size, std::size_t stride)
78 : m_size(
size), m_stride(stride), m_data(
data + long(start)) {}
81 std::size_t
size()
const {
return m_size; }
84 T &
operator[](std::size_t i) {
return m_data[i * m_stride]; }
86 T
operator[](std::size_t i)
const {
return m_data[i * m_stride]; }
89 T &
at(std::size_t i) {
91 return m_data[i * m_stride];
94 T
at(std::size_t i)
const {
96 return m_data[i * m_stride];
133 : m_rows(m.
rows()), m_cols(m.
cols()), m_data(m.
data()) {}
136 template <
typename U = T,
typename = std::enable_if_t<std::is_const_v<U>>>
138 : m_rows(m.
rows()), m_cols(m.
cols()), m_data(m.
data()) {}
141 std::size_t
rows()
const {
return m_rows; }
143 std::size_t
cols()
const {
return m_cols; }
145 std::size_t
size()
const {
return m_rows * m_cols; }
147 bool empty()
const {
return m_rows == 0 || m_cols == 0; }
152 const T *
data()
const {
return m_data; }
157 const T *
operator[](std::size_t i)
const {
return m_data + i * m_cols; }
160 T &
at(std::size_t i, std::size_t j) {
161 assert(i < m_rows && j < m_cols);
162 return m_data[i * m_cols + j];
165 T
at(std::size_t i, std::size_t j)
const {
166 assert(i < m_rows && j < m_cols);
167 return m_data[i * m_cols + j];
170 const T &
atc(std::size_t i, std::size_t j)
const {
171 assert(i < m_rows && j < m_cols);
172 return m_data[i * m_cols + j];
182 auto end() {
return m_data + m_rows * m_cols; }
183 auto cbegin()
const {
return m_data; }
184 auto cend()
const {
return m_data + m_rows * m_cols; }
208template <
typename T =
double>
214 std::vector<T> m_data{};
231 : m_rows(dimension), m_cols(dimension), m_data(dimension * dimension) {}
235 Matrix(std::initializer_list<std::initializer_list<T>> ll)
238 m_data.reserve(m_rows * m_cols);
240 m_data.insert(m_data.end(), l.begin(), l.end());
246 : m_rows(
rows), m_cols(
cols), m_data{l} {
247 assert(m_data.size() ==
rows *
cols &&
248 "initializer_list must be rows*cols");
253 : m_rows(
rows), m_cols(
cols), m_data{std::forward<std::vector<T>>(v)} {
254 assert(m_data.size() ==
rows *
cols &&
255 "initializer_list must be rows*cols");
259 : m_rows(
rows), m_cols(
cols), m_data{v} {
260 assert(m_data.size() ==
rows *
cols &&
261 "initializer_list must be rows*cols");
282 std::size_t
rows()
const {
return m_rows; }
284 std::size_t
cols()
const {
return m_cols; }
286 std::size_t
size()
const {
return m_data.size(); }
289 bool empty()
const {
return m_data.empty(); }
292 T *
data() {
return m_data.data(); }
294 const T *
data()
const {
return m_data.data(); }
299 T *
operator[](std::size_t i) {
return &(m_data[i * m_cols]); }
301 const T *
operator[](std::size_t i)
const {
return &(m_data[i * m_cols]); }
304 T &
at(std::size_t row_i, std::size_t col_j) {
305 assert(row_i < m_rows && col_j < m_cols);
306 return m_data[row_i * m_cols + col_j];
309 T
at(std::size_t row_i, std::size_t col_j)
const {
310 assert(row_i < m_rows && col_j < m_cols);
311 return m_data[row_i * m_cols + col_j];
315 const T &
atc(std::size_t row_i, std::size_t col_j)
const {
316 assert(row_i < m_rows && col_j < m_cols);
317 return m_data[row_i * m_cols + col_j];
328 auto begin() {
return m_data.begin(); }
329 auto cbegin()
const {
return m_data.cbegin(); }
330 auto end() {
return m_data.end(); }
331 auto cend()
const {
return m_data.cend(); }
336 return m_data.data() + long(
row * m_cols);
341 return View<T>(this->
data(), row * m_cols, m_cols, 1ul);
452 [[nodiscard]]
auto real()
const;
454 [[nodiscard]]
auto imag()
const;
456 [[nodiscard]]
auto complex()
const;
534 template <
typename U>
538 template <
typename U>
548 return trans ? CblasTrans : CblasNoTrans;
569void GEMM(
const Matrix<T> &a,
const Matrix<T> &b, Matrix<T> *c,
570 bool trans_A =
false,
bool trans_B =
false);
610void PENTA_GEMM(
const Matrix<T> &A,
const Matrix<T> &B,
const Matrix<T> &C,
611 const Matrix<T> &D,
const Matrix<T> &E, Matrix<T> *M);
625 PENTA_GEMM<T>(A, B, C, D, E, &M);
656template <
typename T,
bool PARALLEL = false>
657void PENTA(
const Matrix<T> &A,
const Matrix<T> &B,
const Matrix<T> &C,
658 const Matrix<T> &D,
const Matrix<T> &E, Matrix<T> *M);
662template <
typename T,
bool PARALLEL = false>
666 PENTA<T, PARALLEL>(A, B, C, D, E, &M);
676constexpr auto myEps();
693bool equal(
const Matrix<T> &lhs,
const Matrix<T> &rhs, T eps = myEps<T>());
Non-owning 2D view onto a Matrix.
Definition Matrix.hpp:121
std::size_t size() const
Return rows*columns [total array size].
Definition Matrix.hpp:145
T operator()(std::size_t i, std::size_t j) const
() index access with range checking, const
Definition Matrix.hpp:178
T * data()
Raw pointer to first element, mutable.
Definition Matrix.hpp:150
auto begin()
Iterators over flat data buffer.
Definition Matrix.hpp:181
T * operator[](std::size_t i)
[] index access (no range checking). [i] returns pointer to ith row, mutable
Definition Matrix.hpp:155
const T * operator[](std::size_t i) const
[] index access (no range checking). [i] returns const pointer to ith row
Definition Matrix.hpp:157
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:170
T & at(std::size_t i, std::size_t j)
at(i,j): element access with range checking, mutable
Definition Matrix.hpp:160
Matrix_view(T *data, std::size_t rows, std::size_t cols)
Construct from raw pointer and dimensions (no ownership)
Definition Matrix.hpp:128
T & operator()(std::size_t i, std::size_t j)
() index access with range checking, mutable
Definition Matrix.hpp:176
std::size_t rows() const
Return rows [major index size].
Definition Matrix.hpp:141
bool empty() const
Bool - is empty? (same as size==0)
Definition Matrix.hpp:147
Matrix_view(Matrix< std::remove_const_t< T > > &m)
Implicit conversion from mutable Matrix (works for both mutable and const view)
Definition Matrix.hpp:132
std::size_t cols() const
Return columns [minor index size].
Definition Matrix.hpp:143
T at(std::size_t i, std::size_t j) const
at(i,j): element access with range checking, const
Definition Matrix.hpp:165
const T * data() const
Raw pointer to first element, const.
Definition Matrix.hpp:152
Matrix_view(const Matrix< std::remove_const_t< T > > &m)
Implicit conversion from const Matrix (only for Matrix_view<const T>)
Definition Matrix.hpp:137
Row-major dense matrix with arithmetic and linear algebra support.
Definition Matrix.hpp:209
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:309
const T * row(std::size_t row) const
Returns raw c pointer to start of a row.
Definition Matrix.hpp:335
Matrix< T > & make_identity()
Constructs a diagonal unit matrix (identity), in place; only for square.
Definition Matrix.ipp:97
Matrix< T > & invert_in_place()
Inverts the matrix in place via LU decomposition (GSL).
Definition Matrix.ipp:37
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:315
friend Matrix< T > operator/(Matrix< T > lhs, const T x)
Scalar divide: returns M / x.
Definition Matrix.hpp:513
Matrix(std::size_t rows, std::size_t cols, const std::vector< T > &v)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:258
std::size_t size() const
Return rows*columns [total array size].
Definition Matrix.hpp:286
const T * data() const
Const pointer to first element; for std::complex<T> this is const complex<T>*, not const T*.
Definition Matrix.hpp:294
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:361
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:273
friend Matrix< T > operator+(Matrix< T > lhs, const Matrix< T > &rhs)
Elementwise addition: returns M + N.
Definition Matrix.hpp:495
T * data()
Pointer to first element; for std::complex<T> this is complex<T>*, not T*.
Definition Matrix.hpp:292
std::size_t rows() const
Return rows [major index size].
Definition Matrix.hpp:282
friend Matrix< T > operator+(Matrix< T > M, T aI)
Returns M + aI (adds a to diagonal)
Definition Matrix.hpp:524
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:117
View< T > row_view(std::size_t row)
Returns a mutable 'View' of a row.
Definition Matrix.hpp:340
Matrix< T > & operator+=(const Matrix< T > &rhs)
In-place elementwise addition; dimensions must match.
Definition Matrix.ipp:172
friend Matrix< T > operator*(const T x, Matrix< T > rhs)
Scalar multiply: returns x * M.
Definition Matrix.hpp:505
auto begin()
iterators for underlying std::vector (entire data)
Definition Matrix.hpp:328
T & at(std::size_t row_i, std::size_t col_j)
at(i,j): element access with range checking, mutable
Definition Matrix.hpp:304
auto complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition Matrix.ipp:159
auto imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:148
auto as_gsl_view()
Returns a GSL matrix view for use with GSL functions (no copy).
Definition Matrix.ipp:390
Matrix< T > & conj_in_place()
Conjugates matrix, in place.
Definition Matrix.ipp:128
T * operator[](std::size_t i)
[] index access (no range checking). [i] returns pointer to ith row, mutable
Definition Matrix.hpp:299
Matrix(std::size_t dimension)
Initialise a blank square matrix dimension*dimension, filled with 0.
Definition Matrix.hpp:230
Matrix()
Default initialiser.
Definition Matrix.hpp:218
Matrix(std::size_t rows, std::size_t cols, std::initializer_list< T > l)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:245
friend Matrix< T > operator*(Matrix< T > lhs, const T x)
Scalar multiply: returns M * x.
Definition Matrix.hpp:509
T & operator()(std::size_t i, std::size_t j)
() index access with range checking, mutable
Definition Matrix.hpp:321
Matrix< T > & operator/=(const T x)
In-place scalar divide: M_ij /= x.
Definition Matrix.ipp:194
Matrix< T > transpose() const
Returns the transpose of the matrix.
Definition Matrix.ipp:65
Matrix< T > & zero()
Sets all elements to zero, in place.
Definition Matrix.ipp:108
Matrix< T > inverse() const
Returns inverse of the matrix; original is unchanged.
Definition Matrix.hpp:422
View< const T > row_view(std::size_t row) const
Returns an immutable 'View' of a row.
Definition Matrix.hpp:344
Matrix< T > & operator*=(const T x)
In-place scalar multiply: M_ij *= x.
Definition Matrix.ipp:188
bool empty() const
Bool - is empty? (same as size==0)
Definition Matrix.hpp:289
friend Matrix< T > operator-(Matrix< T > lhs, const Matrix< T > &rhs)
Elementwise subtraction: returns M - N.
Definition Matrix.hpp:500
std::size_t cols() const
Return columns [minor index size].
Definition Matrix.hpp:284
View< T > column_view(std::size_t col)
Returns a mutable 'View' of a column.
Definition Matrix.hpp:348
T determinant() const
Returns the determinant via LU decomposition (GSL).
Definition Matrix.ipp:9
Matrix< T > & mult_elements_by(const Matrix< T > &a)
Elementwise multiply in place: M_ij *= a_ij.
Definition Matrix.ipp:224
const T * operator[](std::size_t i) const
[] index access (no range checking). [i] returns const pointer to ith row
Definition Matrix.hpp:301
friend Matrix< T > operator-(Matrix< T > M, T aI)
Returns M - aI (subtracts a from diagonal)
Definition Matrix.hpp:528
auto real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:137
Matrix(std::initializer_list< std::initializer_list< T > > ll)
Initialise a matrix from initialiser list. {{},{},{}}. Each row must be same length.
Definition Matrix.hpp:235
Matrix_view< T > matrix_view()
Returns a mutable view of the entire matrix (no ownership, no resize)
Definition Matrix.hpp:357
Matrix(std::size_t rows, std::size_t cols, std::vector< T > &&v)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:252
T operator()(std::size_t i, std::size_t j) const
() index access with range checking, const
Definition Matrix.hpp:323
Matrix(std::size_t rows, std::size_t cols)
Initialise a blank matrix rows*cols, filled with 0.
Definition Matrix.hpp:221
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:478
Matrix(std::size_t rows, std::size_t cols, const T &value)
Initialise a matrix rows*cols, filled with 'value'.
Definition Matrix.hpp:225
void resize(std::size_t rows, std::size_t cols)
Resizes matrix to new dimension; all values reset to default.
Definition Matrix.hpp:266
View< const T > column_view(std::size_t col) const
Returns an immutable 'View' of a column.
Definition Matrix.hpp:352
Matrix< T > & operator-=(const Matrix< T > &rhs)
In-place elementwise subtraction; dimensions must match.
Definition Matrix.ipp:180
Non-owning strided view onto a 1D segment of an array.
Definition Matrix.hpp:70
T operator[](std::size_t i) const
[] index access (no range checking), const
Definition Matrix.hpp:86
T * data()
Raw pointer to first viewed element.
Definition Matrix.hpp:104
T operator()(std::size_t i) const
() index access with range checking, const
Definition Matrix.hpp:101
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:77
T & operator[](std::size_t i)
[] index access (no range checking), mutable
Definition Matrix.hpp:84
std::size_t size() const
Number of elements in the view.
Definition Matrix.hpp:81
T at(std::size_t i) const
at(i): element access with range checking, const
Definition Matrix.hpp:94
T & at(std::size_t i)
at(i): element access with range checking, mutable
Definition Matrix.hpp:89
T & operator()(std::size_t i)
() index access with range checking, mutable
Definition Matrix.hpp:99
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:54
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:342
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:308
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:248
CBLAS_TRANSPOSE to_cblas_trans(bool trans)
Converts bool to CBLAS_TRANSPOSE enum (CblasTrans if true, CblasNoTrans if false)
Definition Matrix.hpp:547
constexpr auto myEps()
Default relative tolerance for equal(): 1e-6 for float, 1e-12 for double.
Definition Matrix.ipp:446
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:461
Type trait: true iff T is std::complex<U> for some U.
Definition Matrix.hpp:19