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>
18struct is_complex : std::false_type {};
20struct is_complex<std::complex<T>> : std::true_type {};
38template <
typename T =
double>
44 std::vector<T> m_data{};
60 explicit Matrix(std::size_t dimension)
61 : m_rows(dimension), m_cols(dimension), m_data(dimension * dimension) {}
65 Matrix(std::initializer_list<std::initializer_list<T>> ll)
68 m_data.reserve(m_rows * m_cols);
70 m_data.insert(m_data.end(), l.begin(), l.end());
76 : m_rows(
rows), m_cols(
cols), m_data{l} {
77 assert(m_data.size() ==
rows *
cols &&
78 "initializer_list must be rows*cols");
83 : m_rows(
rows), m_cols(
cols), m_data{std::forward<std::vector<T>>(v)} {
84 assert(m_data.size() ==
rows *
cols &&
85 "initializer_list must be rows*cols");
89 : m_rows(
rows), m_cols(
cols), m_data{v} {
90 assert(m_data.size() ==
rows *
cols &&
91 "initializer_list must be rows*cols");
112 std::size_t
rows()
const {
return m_rows; }
114 std::size_t
cols()
const {
return m_cols; }
116 std::size_t
size()
const {
return m_data.size(); }
119 bool empty()
const {
return m_data.empty(); }
123 T *
data() {
return m_data.data(); }
125 const T *
data()
const {
return m_data.data(); }
130 const T *
operator[](std::size_t i)
const {
return &(m_data[i * m_cols]); }
132 T *
operator[](std::size_t i) {
return &(m_data[i * m_cols]); }
135 T &
at(std::size_t row_i, std::size_t col_j) {
136 assert(row_i < m_rows && col_j < m_cols);
137 return m_data[row_i * m_cols + col_j];
140 T
at(std::size_t row_i, std::size_t col_j)
const {
141 assert(row_i < m_rows && col_j < m_cols);
142 return m_data[row_i * m_cols + col_j];
146 const T &
atc(std::size_t row_i, std::size_t col_j)
const {
147 assert(row_i < m_rows && col_j < m_cols);
148 return m_data[row_i * m_cols + col_j];
159 auto begin() {
return m_data.begin(); }
160 auto cbegin()
const {
return m_data.cbegin(); }
161 auto end() {
return m_data.end(); }
162 auto cend()
const {
return m_data.cend(); }
167 return m_data.data() + long(
row * m_cols);
172 return View<T>(this->
data(), row * m_cols, m_cols, 1ul);
238 [[nodiscard]]
auto real()
const;
240 [[nodiscard]]
auto imag()
const;
242 [[nodiscard]]
auto complex()
const;
271 [[nodiscard]]
friend Matrix<T> operator-(Matrix<T> lhs,
272 const Matrix<T> &rhs) {
275 [[nodiscard]]
friend Matrix<T> operator*(
const T x, Matrix<T> rhs) {
278 [[nodiscard]]
friend Matrix<T> operator*(Matrix<T> lhs,
const T x) {
281 [[nodiscard]]
friend Matrix<T> operator/(Matrix<T> lhs,
const T x) {
289 Matrix<T> &operator-=(T aI);
291 [[nodiscard]]
friend Matrix<T> operator+(Matrix<T> M, T aI) {
294 [[nodiscard]]
friend Matrix<T> operator-(Matrix<T> M, T aI) {
300 template <
typename U>
304 template <
typename U>
305 friend std::ostream &operator<<(std::ostream &os,
const Matrix<U> &a);
317 bool trans_A =
false,
bool trans_B =
false);
372 PENTA_GEMM<T>(A, B, C, D, E, &M);
403template <
typename T,
bool PARALLEL = false>
404void PENTA(
const Matrix<T> &A,
const Matrix<T> &B,
const Matrix<T> &C,
405 const Matrix<T> &D,
const Matrix<T> &E, Matrix<T> *M);
409template <
typename T,
bool PARALLEL = false>
413 PENTA<T, PARALLEL>(A, B, C, D, E, &M);
422constexpr auto myEps();
426bool equal(
const Matrix<T> &lhs,
const Matrix<T> &rhs, T eps = myEps<T>());
Provides a non-owning view onto a Matrix (read/write, but no resize)
Definition Matrix.ipp:45
Matrix class; row-major.
Definition Matrix.hpp:39
T at(std::size_t row_i, std::size_t col_j) const
const () index access (with range checking). (i,j) ith row, jth col
Definition Matrix.hpp:140
const T * row(std::size_t row) const
Returns raw c pointer to start of a row.
Definition Matrix.hpp:166
Matrix< T > & make_identity()
Constructs a diagonal unit matrix (identity), in place; only for square.
Definition Matrix.ipp:201
Matrix< T > & invert_in_place()
Inverts the matrix, in place. Uses GSL; via LU decomposition. Only works for double/complex<double>.
Definition Matrix.ipp:135
const T & atc(std::size_t row_i, std::size_t col_j) const
const ref () index access (with range checking). (i,j) ith row, jth col
Definition Matrix.hpp:146
Matrix(std::size_t rows, std::size_t cols, const std::vector< T > &v)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:88
std::size_t size() const
Return rows*columns [total array size].
Definition Matrix.hpp:116
const T * data() const
As above, but const.
Definition Matrix.hpp:125
Matrix_view< const T > matrix_view() const
Returns an immutable view of the entire matrix (no ownership, no resize)
Definition Matrix.hpp:192
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:103
T * data()
Returns pointer to first element. Note: for std::complex<T>, this is a pointer to complex<T>,...
Definition Matrix.hpp:123
std::size_t rows() const
Return rows [major index size].
Definition Matrix.hpp:112
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:221
View< T > row_view(std::size_t row)
Returns a mutable 'View' of a row.
Definition Matrix.hpp:171
Matrix< T > & operator+=(const Matrix< T > &rhs)
Overload standard operators: do what expected.
Definition Matrix.ipp:276
auto begin()
iterators for underlying std::vector (entire data)
Definition Matrix.hpp:159
T & at(std::size_t row_i, std::size_t col_j)
() index access (with range checking). (i,j) returns ith row, jth col
Definition Matrix.hpp:135
auto complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition Matrix.ipp:263
auto imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:252
auto as_gsl_view()
Returns gsl_matrix_view (or _float_view, _complex_view, _complex_float_view). Call ....
Definition Matrix.ipp:536
Matrix< T > & conj_in_place()
Conjugates matrix, in place.
Definition Matrix.ipp:232
T * operator[](std::size_t i)
As above, but const.
Definition Matrix.hpp:132
Matrix(std::size_t dimension)
Initialise a blank square matrix dimension*dimension, filled with 0.
Definition Matrix.hpp:60
Matrix()
Default initialiser.
Definition Matrix.hpp:48
Matrix(std::size_t rows, std::size_t cols, std::initializer_list< T > l)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:75
T & operator()(std::size_t i, std::size_t j)
() index access (with range checking). (i,j) returns ith row, jth col
Definition Matrix.hpp:152
Matrix< T > transpose() const
Returns transpose of matrix.
Definition Matrix.ipp:169
Matrix< T > & zero()
Sets all elements to zero, in place.
Definition Matrix.ipp:212
Matrix< T > inverse() const
Returns inverse of the matrix. Leaves original matrix intact. Uses GSL; via LU decomposition....
Definition Matrix.ipp:162
View< const T > row_view(std::size_t row) const
Returns an immutable 'View' of a row.
Definition Matrix.hpp:175
bool empty() const
Bool - is empty? (same as size==0)
Definition Matrix.hpp:119
std::size_t cols() const
Return columns [minor index size].
Definition Matrix.hpp:114
View< T > column_view(std::size_t col)
Returns a mutable 'View' of a column.
Definition Matrix.hpp:179
T determinant() const
Returns the determinant. Uses GSL; via LU decomposition. Only works for double/complex<double>
Definition Matrix.ipp:107
Matrix< T > & mult_elements_by(const Matrix< T > &a)
Muplitplies all the elements by those of matrix a, in place: M_ij *= a_ij.
Definition Matrix.ipp:328
const T * operator[](std::size_t i) const
[] index access (with no range checking). [i][j] returns ith row, jth col
Definition Matrix.hpp:130
auto real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:241
Matrix(std::initializer_list< std::initializer_list< T > > ll)
Initialise a matrix from initialiser list. {{},{},{}}. Each row must be same length.
Definition Matrix.hpp:65
Matrix_view< T > matrix_view()
Returns a mutable view of the entire matrix (no ownership, no resize)
Definition Matrix.hpp:188
Matrix(std::size_t rows, std::size_t cols, std::vector< T > &&v)
Initialise a matrix from single initialiser list. {...}.
Definition Matrix.hpp:82
T operator()(std::size_t i, std::size_t j) const
As above, but const.
Definition Matrix.hpp:154
Matrix(std::size_t rows, std::size_t cols)
Initialise a blank matrix rows*cols, filled with 0.
Definition Matrix.hpp:51
friend Matrix< T > mult_elements(Matrix< T > a, const Matrix< T > &b)
Returns new matrix, C_ij = A_ij*B_ij.
Definition Matrix.hpp:252
Matrix(std::size_t rows, std::size_t cols, const T &value)
Initialise a matrix rows*cols, filled with 'value'.
Definition Matrix.hpp:55
void resize(std::size_t rows, std::size_t cols)
Resizes matrix to new dimension; all values reset to default.
Definition Matrix.hpp:96
View< const T > column_view(std::size_t col) const
Returns an immutable 'View' of a column.
Definition Matrix.hpp:183
Proved a "view" onto an array.
Definition Matrix.ipp:7
constexpr bool is_complex_v
User-defined type-trait: Checks whether T is a std::complex type.
Definition AdamsMoulton.hpp:109
Defines Matrix, Vector classes, and linear some algebra functions.
Definition Matrix.hpp:26
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:488
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:454
void GEMM(const Matrix< T > &a, const Matrix< T > &b, Matrix< T > *c, bool trans_A=false, bool trans_B=false)
Matrix multiplication C = A*B. C is overwritten with product, and must be correct size already.
Definition Matrix.ipp:394
bool equal(const Matrix< T > &lhs, const Matrix< T > &rhs, T eps=myEps< T >())
Compares two matrices; returns true iff all elements compare relatively to better than eps.
Definition Matrix.ipp:607