8using lapack_complex =
long double;
9void dgetrf_(
int *m,
int *n,
double *a,
int *lda,
int *ipiv,
int *info);
10void dgetri_(
int *n,
double *a,
int *lda,
int *ipiv,
double *work,
int *lwork,
12void zgetrf_(
int *m,
int *n, lapack_complex *a,
int *lda,
int *ipiv,
int *info);
13void zgetri_(
int *n, lapack_complex *a,
int *lda,
int *ipiv,
14 lapack_complex *work,
int *lwork,
int *info);
24 static_assert(std::is_same_v<T, double> ||
25 std::is_same_v<T, std::complex<double>>,
26 "Determinant only works for double");
28 assert(rows() == cols() &&
"Determinant only defined for square matrix");
32 auto gsl_view = LU.as_gsl_view();
33 gsl_permutation *permutn = gsl_permutation_alloc(rows());
34 if constexpr (std::is_same_v<T, double>) {
35 gsl_linalg_LU_decomp(&gsl_view.matrix, permutn, &sLU);
36 gsl_permutation_free(permutn);
37 return gsl_linalg_LU_det(&gsl_view.matrix, sLU);
38 }
else if constexpr (std::is_same_v<T, std::complex<double>>) {
39 gsl_linalg_complex_LU_decomp(&gsl_view.matrix, permutn, &sLU);
40 gsl_permutation_free(permutn);
41 const auto gsl_cmplx = gsl_linalg_complex_LU_det(&gsl_view.matrix, sLU);
43 return {GSL_REAL(gsl_cmplx), GSL_IMAG(gsl_cmplx)};
56 std::is_same_v<T, double> || std::is_same_v<T, std::complex<double>>,
57 "invert only works for Matrix<double> or Matrix<complex<double>>");
59 assert(rows() == cols() &&
"Inverse only defined for square matrix");
60 int dim = int(m_rows);
62 std::vector<int> ipiv(m_rows);
63 if constexpr (std::is_same_v<T, double>) {
64 dgetrf_(&dim, &dim, data(), &dim, ipiv.data(), &info);
65 assert(info == 0 &&
"LU factorisation failed (singular matrix?)");
69 dgetri_(&dim, data(), &dim, ipiv.data(), &wkopt, &lwork, &info);
70 lwork = std::max(1,
int(wkopt));
71 std::vector<double> work(
static_cast<std::size_t
>(lwork));
72 dgetri_(&dim, data(), &dim, ipiv.data(), work.data(), &lwork, &info);
73 assert(info == 0 &&
"LU inversion failed (singular matrix?)");
74 }
else if constexpr (std::is_same_v<T, std::complex<double>>) {
75 auto *a_ptr =
reinterpret_cast<lapack_complex *
>(data());
76 zgetrf_(&dim, &dim, a_ptr, &dim, ipiv.data(), &info);
77 assert(info == 0 &&
"LU factorisation failed (singular matrix?)");
80 std::complex<double> wkopt{0.0, 0.0};
81 zgetri_(&dim, a_ptr, &dim, ipiv.data(),
82 reinterpret_cast<lapack_complex *
>(&wkopt), &lwork, &info);
83 lwork = std::max(1,
int(wkopt.real()));
84 std::vector<std::complex<double>> work(
static_cast<std::size_t
>(lwork));
85 zgetri_(&dim, a_ptr, &dim, ipiv.data(),
86 reinterpret_cast<lapack_complex *
>(work.data()), &lwork, &info);
87 assert(info == 0 &&
"LU inversion failed (singular matrix?)");
96 if constexpr (std::is_same_v<T, double>) {
98 const auto this_gsl = as_gsl_view();
99 gsl_matrix_transpose_memcpy(&Tr_gsl.matrix, &this_gsl.matrix);
100 }
else if constexpr (std::is_same_v<T, float>) {
102 const auto this_gsl = as_gsl_view();
103 gsl_matrix_float_transpose_memcpy(&Tr_gsl.matrix, &this_gsl.matrix);
104 }
else if constexpr (std::is_same_v<T, std::complex<double>>) {
106 const auto this_gsl = as_gsl_view();
107 gsl_matrix_complex_transpose_memcpy(&Tr_gsl.matrix, &this_gsl.matrix);
108 }
else if constexpr (std::is_same_v<T, std::complex<float>>) {
110 const auto this_gsl = as_gsl_view();
111 gsl_matrix_complex_float_transpose_memcpy(&Tr_gsl.matrix, &this_gsl.matrix);
114 for (
auto i = 0ul; i < Tr.
rows(); ++i) {
115 for (
auto j = 0ul; j < Tr.
cols(); ++j) {
116 Tr[i][j] = (*this)[j][i];
127 assert(m_rows == m_cols &&
"Can only call make_identity() for square matrix");
128 for (
auto i = 0ul; i < m_rows; ++i) {
129 for (
auto j = 0ul; j < m_cols; ++j) {
130 at(i, j) = i == j ? T(1) : T(0);
138 for (std::size_t i = 0; i < size(); ++i) {
147 static_assert(is_complex_v<T>,
"conj() only available for complex Matrix");
148 std::vector<T> conj_data;
149 conj_data.reserve(m_data.size());
150 for (std::size_t i = 0; i < m_data.size(); ++i) {
151 conj_data.push_back(std::conj(m_data[i]));
153 return Matrix<T>{m_rows, m_cols, std::move(conj_data)};
158 static_assert(is_complex_v<T>,
"conj() only available for complex Matrix");
159 for (std::size_t i = 0; i < m_data.size(); ++i) {
160 m_data[i] = std::conj(m_data[i]);
167 static_assert(is_complex_v<T>,
"real() only available for complex Matrix");
168 std::vector<typename T::value_type> real_data;
169 real_data.reserve(m_data.size());
170 for (std::size_t i = 0; i < m_data.size(); ++i) {
171 real_data.push_back(std::real(m_data[i]));
178 static_assert(is_complex_v<T>,
"imag() only available for complex Matrix");
179 std::vector<typename T::value_type> imag_data;
180 imag_data.reserve(m_data.size());
181 for (std::size_t i = 0; i < m_data.size(); ++i) {
182 imag_data.push_back(std::imag(m_data[i]));
189 static_assert(!is_complex_v<T>,
"complex() only available for real Matrix");
191 std::vector<std::complex<T>> new_data;
192 new_data.reserve(m_data.size());
193 for (std::size_t i = 0; i < m_data.size(); ++i) {
194 new_data.push_back(m_data[i]);
202 assert(rows() == rhs.
rows() && cols() == rhs.
cols() &&
203 "Matrices must have same dimensions for addition");
205#pragma omp parallel for
206 for (
auto i = 0ul; i < m_data.size(); ++i) {
207 m_data[i] += rhs.m_data[i];
213 assert(rows() == rhs.
rows() && cols() == rhs.
cols() &&
214 "Matrices must have same dimensions for subtraction");
217#pragma omp parallel for
218 for (
auto i = 0ul; i < m_data.size(); ++i) {
219 m_data[i] -= rhs.m_data[i];
227#pragma omp parallel for
228 for (
auto i = 0ul; i < m_data.size(); ++i) {
237#pragma omp parallel for
238 for (
auto i = 0ul; i < m_data.size(); ++i) {
249 assert(m_rows == m_cols &&
"Can only call M+a for square matrix");
250 for (
auto i = 0ul; i < m_rows; ++i) {
259 assert(m_rows == m_cols &&
"Can only call M-a for square matrix");
260 for (
auto i = 0ul; i < m_rows; ++i) {
269 assert(rows() == a.
rows() && cols() == a.
cols() &&
270 "Matrices must have same dimensions for mult_elements_by");
271 for (
auto i = 0ul; i < m_data.size(); ++i) {
272 m_data[i] *= a.m_data[i];
282 "Matrices a and b must have correct dimension for multiplication");
285 GEMM(a, b, &product);
302 const int A_rows =
static_cast<int>(trans_A ? a.
cols() : a.
rows());
303 const int A_cols =
static_cast<int>(trans_A ? a.
rows() : a.
cols());
304 const int B_rows =
static_cast<int>(trans_B ? b.
cols() : b.
rows());
305 const int B_cols =
static_cast<int>(trans_B ? b.
rows() : b.
cols());
309 const int M = A_rows;
310 const int N = B_cols;
311 const int K = A_cols;
313 assert(A_cols == B_rows &&
"op(A) cols must equal op(B) rows");
314 assert(
static_cast<int>(c->rows()) == M &&
static_cast<int>(c->cols()) == N &&
315 "Output matrix c must be sized MxN");
320 const int lda =
static_cast<int>(a.
cols());
321 const int ldb =
static_cast<int>(b.
cols());
322 const int ldc =
static_cast<int>(c->cols());
324 if constexpr (std::is_same_v<T, double>) {
325 cblas_dgemm(CblasRowMajor, ta, tb, M, N, K, 1.0, a.
data(), lda, b.
data(),
326 ldb, 0.0, c->data(), ldc);
328 }
else if constexpr (std::is_same_v<T, float>) {
329 cblas_sgemm(CblasRowMajor, ta, tb, M, N, K, 1.0f, a.
data(), lda, b.
data(),
330 ldb, 0.0f, c->data(), ldc);
332 }
else if constexpr (std::is_same_v<T, std::complex<double>>) {
333 const std::complex<double> alpha{1.0, 0.0};
334 const std::complex<double> beta{0.0, 0.0};
335 cblas_zgemm(CblasRowMajor, ta, tb, M, N, K, &alpha, a.
data(), lda, b.
data(),
336 ldb, &beta, c->data(), ldc);
338 }
else if constexpr (std::is_same_v<T, std::complex<float>>) {
339 const std::complex<float> alpha{1.0f, 0.0f};
340 const std::complex<float> beta{0.0f, 0.0f};
341 cblas_cgemm(CblasRowMajor, ta, tb, M, N, K, &alpha, a.
data(), lda, b.
data(),
342 ldb, &beta, c->data(), ldc);
345 static_assert(!
sizeof(T),
"GEMM: unsupported scalar type");
355 const auto N = A.rows();
356 assert(A.cols() == A.rows() &&
"Must be square");
368 for (std::size_t i = 0; i < N; ++i) {
369 for (std::size_t j = 0; j < N; ++j) {
370 const auto cij = C[i][j];
371 for (std::size_t b = 0; b < N; ++b) {
372 X[j][b] = cij * E[j][b];
376 for (std::size_t a = 0; a < N; ++a) {
377 for (std::size_t b = 0; b < N; ++b) {
378 M[a][b] += A[a][i] * Y[a][b] * D[i][b];
385template <
typename T,
bool PARALLEL>
389 const auto N = A.rows();
390 assert(A.cols() == A.rows() &&
"Must be square");
395 if constexpr (PARALLEL) {
397#pragma omp parallel for collapse(2)
398 for (std::size_t a = 0; a < N; ++a) {
399 for (std::size_t b = 0; b < N; ++b) {
400 const T *Ba = &B[a][0];
402 for (std::size_t i = 0; i < N; ++i) {
403 const auto AaiDib = A[a][i] * D[i][b];
404 const T *Ci = &C[i][0];
405 for (std::size_t j = 0; j < N; ++j) {
406 Mab += AaiDib * Ba[j] * Ci[j] * E[j][b];
415 for (std::size_t a = 0; a < N; ++a) {
416 const T *Ba = &
B[a][0];
417 for (std::size_t b = 0; b < N; ++b) {
419 for (std::size_t i = 0; i < N; ++i) {
420 const auto AaiDib =
A[a][i] * D[i][b];
421 const T *Ci = &C[i][0];
422 for (std::size_t j = 0; j < N; ++j) {
423 Mab += AaiDib * Ba[j] * Ci[j] * E[j][b];
435 if constexpr (std::is_same_v<T, double>) {
436 return gsl_matrix_view_array(m_data.data(), m_rows, m_cols);
437 }
else if constexpr (std::is_same_v<T, float>) {
438 return gsl_matrix_float_view_array(m_data.data(), m_rows, m_cols);
439 }
else if constexpr (std::is_same_v<T, std::complex<double>>) {
441 return gsl_matrix_complex_view_array(
442 reinterpret_cast<double *
>(m_data.data()), m_rows, m_cols);
443 }
else if constexpr (std::is_same_v<T, std::complex<float>>) {
444 return gsl_matrix_complex_float_view_array(
445 reinterpret_cast<float *
>(m_data.data()), m_rows, m_cols);
447 assert(
false &&
"as_gsl_view() only available for double/float (or complex "
454 if constexpr (std::is_same_v<T, double>) {
455 return gsl_matrix_const_view_array(m_data.data(), m_rows, m_cols);
456 }
else if constexpr (std::is_same_v<T, float>) {
457 return gsl_matrix_float_const_view_array(m_data.data(), m_rows, m_cols);
458 }
else if constexpr (std::is_same_v<T, std::complex<double>>) {
459 return gsl_matrix_complex_const_view_array(
460 reinterpret_cast<const double *
>(m_data.data()), m_rows, m_cols);
461 }
else if constexpr (std::is_same_v<T, std::complex<float>>) {
462 return gsl_matrix_complex_float_const_view_array(
463 reinterpret_cast<const float *
>(m_data.data()), m_rows, m_cols);
465 assert(
false &&
"as_gsl_view() only for available double/float (or complex "
472std::ostream &operator<<(std::ostream &os,
const Matrix<T> &a) {
473 for (
auto i = 0ul; i < a.rows(); ++i) {
474 for (
auto j = 0ul; j < a.cols(); ++j) {
475 os << a(i, j) <<
" ";
491 if constexpr (std::is_same_v<T, float> ||
492 std::is_same_v<T, std::complex<float>>) {
494 }
else if constexpr (std::is_same_v<T, double> ||
495 std::is_same_v<T, std::complex<double>>) {
510 for (
auto i = 0ul; i < lhs.
rows(); ++i) {
511 for (
auto j = 0ul; j < lhs.
cols(); ++j) {
513 if (std::abs(lhs(i, j) - rhs(i, j)) >
514 std::abs(eps * (lhs(i, j) + rhs(i, j))))
Row-major dense matrix with arithmetic and linear algebra support.
Definition Matrix.hpp:208
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
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
Matrix< T > conj() const
Returns conjugate of matrix.
Definition Matrix.ipp:146
Matrix< T > & operator+=(const Matrix< T > &rhs)
In-place elementwise addition; dimensions must match.
Definition Matrix.ipp:201
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
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 > & operator*=(const T x)
In-place scalar multiply: M_ij *= x.
Definition Matrix.ipp:224
std::size_t cols() const
Return columns [minor index size].
Definition Matrix.hpp:283
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
auto real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:166
Matrix< T > & operator-=(const Matrix< T > &rhs)
In-place elementwise subtraction; dimensions must match.
Definition Matrix.ipp:212
double B(double Z, int l=0)
Bl(Z) fitting function [PRA 93, 052509 (2016)].
Definition FGRadPot.cpp:132
double A(double Z, int l=0)
Al(Z) fitting function [PRA 93, 052509 (2016)].
Definition FGRadPot.cpp:121
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