8 #include "exceptions.h"
29 std::tuple<int, int> local2Global(
const int& k);
32 int global2Local(
const int& row,
const int& col);
53 const int& numBlasRows = 0,
const int& numBlasCols = 0,
54 const int& numBlocksRows = 0,
const int& numBlocksCols = 0);
115 const std::string &dataSetName) {}
129 const char& trans2 =
transN);
134 if(numRows_ != m1.
rows() || numCols_ != m1.
cols()) {
135 Error(
"Cannot add matrices of different sizes.");
137 for (
size_t s = 0; s <
size(); s++) mat[s] += m1.mat[s];
144 if(numRows_ != m1.
rows() || numCols_ != m1.
cols()) {
145 Error(
"Cannot subtract matrices of different sizes.");
147 for (
size_t s = 0; s <
size(); s++) mat[s] -= m1.mat[s];
154 for (
size_t s = 0; s <
size(); s++) mat[s] *= that;
161 for (
size_t s = 0; s <
size(); s++) mat[s] /= that;
180 bool checkNegativeEigenvalues =
true);
208 template <
typename T>
210 const int& numBlasRows,
const int& numBlasCols,
211 const int& numBlocksRows,
const int& numBlocksCols) {
213 (void) numBlocksRows;
214 (void) numBlocksCols;
220 numElements_ = numRows_ * numCols_;
221 mat =
new T[numRows_ * numCols_];
222 for (
size_t i = 0; i < numElements_; i++) mat[i] = 0;
223 assert(mat !=
nullptr);
227 template <
typename T>
236 template <
typename T>
238 numRows_ = that.numRows_;
239 numCols_ = that.numCols_;
240 numElements_ = that.numElements_;
241 if (mat !=
nullptr) {
245 mat =
new T[numElements_];
246 assert(mat !=
nullptr);
247 for (
size_t i = 0; i < numElements_; i++) {
248 mat[i] = that.mat[i];
252 template <
typename T>
255 numRows_ = that.numRows_;
256 numCols_ = that.numCols_;
257 numElements_ = that.numElements_;
259 if (mat !=
nullptr) {
262 mat =
new T[numElements_];
263 assert(mat !=
nullptr);
264 for (
size_t i = 0; i < numElements_; i++) {
265 mat[i] = that.mat[i];
272 template <
typename T>
274 if (mat !=
nullptr)
delete[] mat;
278 template <
typename T>
282 template <
typename T>
286 template <
typename T>
290 template <
typename T>
294 template <
typename T>
300 template <
typename T>
302 if(row >= numRows_ || col >= numCols_ || row < 0 || col < 0) {
303 DeveloperError(
"Tried to reference a SMatrix state out of bounds: "
304 + std::to_string(row) +
" " + std::to_string(col));
306 return mat[global2Local(row, col)];
309 template <
typename T>
311 if(row >= numRows_ || col >= numCols_ || row < 0 || col < 0) {
312 DeveloperError(
"Tried to reference a SMatrix state out of bounds: "
313 + std::to_string(row) +
" " + std::to_string(col));
315 return mat[global2Local(row, col)];
318 template <
typename T>
320 if(row >= numRows_ || col >= numCols_ || row < 0 || col < 0) {
321 DeveloperError(
"indicesAreLocal tried to reference a SMatrix state out of bounds: "
322 + std::to_string(row) +
" " + std::to_string(col));
327 template <
typename T>
331 if(numRows_ == 0)
Error(
"attempted to div by zero in l2g");
332 int j = k / numRows_;
333 int i = k - j * numRows_;
334 return std::make_tuple(i, j);
338 template <
typename T>
340 return numRows_ * col + row;
343 template <
typename T>
345 std::vector<std::tuple<int, int>> x;
346 for (
size_t k = 0; k < numElements_; k++) {
347 std::tuple<int, int> t = local2Global(k);
354 template <
typename T>
357 for (
int row = 0; row < numRows_; row++) {
358 for (
int col = 0; col < numCols_; col++) ret(row, col) = -(*this)(row, col);
364 template <
typename T>
366 if(numRows_ != numCols_) {
367 Error(
"Cannot build an identity matrix with non-square matrix");
369 for (
int row = 0; row < numRows_; row++) (*
this)(row, row) = (T)1.0;
377 template <
typename T>
380 for (
int row = 0; row < numRows_; row++) {
381 for (
int col = 0; col < numCols_; col++) {
382 sumSq += ((*this)(row, col) * (*
this)(row, col));
388 template <
typename T>
394 template <
typename T>
397 for (
size_t i = 0; i < numElements_; i++) {
398 scalar += (*(mat + i)) * (*(that.mat + i));
Header file for forward declaration of BLAS and LAPACK functions.
Object used to print an error message for developers, and stop the code.
Definition: exceptions.h:21
Object used to print an error message, and stop the code.
Definition: exceptions.h:10
Class for managing a (serial) matrix stored in memory.
Definition: SMatrix.h:20
std::tuple< std::vector< double >, SerialMatrix< T > > diagonalize()
Diagonalize a complex-hermitian / real-symmetric matrix.
SerialMatrix< T > operator/=(const T &that)
Matrix-scalar division.
Definition: SMatrix.h:160
~SerialMatrix()
Destructor, to delete raw buffer.
Definition: SMatrix.h:273
void symmetrize()
Symmetrize the matrix.
size_t size() const
Find global number of matrix elements.
Definition: SMatrix.h:295
SerialMatrix()
Default constructor.
Definition: SMatrix.h:228
SerialMatrix< T > & operator=(const SerialMatrix< T > &that)
Copy constructor.
Definition: SMatrix.h:253
SerialMatrix(const SerialMatrix< T > &that)
Copy constructor.
Definition: SMatrix.h:237
const T & operator()(const int &row, const int &col) const
Const get and set operator.
Definition: SMatrix.h:310
int localRows() const
Return local number of rows.
Definition: SMatrix.h:283
std::tuple< std::vector< double >, SerialMatrix< T > > diagonalize(int numEigenvalues, bool checkNegativeEigenvalues=true)
Diagonalize a complex-hermitian / real sym matrix for only some of its eigenvalues.
std::vector< std::tuple< int, int > > getAllLocalStates()
Find the global indices of the matrix elements that are stored locally by the current MPI process.
Definition: SMatrix.h:344
SerialMatrix(const int &numRows, const int &numCols, const int &numBlasRows=0, const int &numBlasCols=0, const int &numBlocksRows=0, const int &numBlocksCols=0)
Default SMatrix constructor.
Definition: SMatrix.h:209
void outputToHDF5(const std::string &outFileName, const std::string &dataSetName)
A dummy function not implemented, only for PMatrix currently.
Definition: SMatrix.h:114
static const char transC
Indicates that the matrix A is taken as its adjoint: transC(A) = A^+.
Definition: SMatrix.h:42
void enforcePositiveSemiDefinite()
A function to fix up a matrix with a bit of noise to be positive semi-definite, which currently does ...
Definition: SMatrix.h:108
double norm()
Computes the Frobenius norm of the matrix (or Euclidean norm, or L2 norm of the matrix).
Definition: SMatrix.h:378
void eye()
Sets this matrix as the identity.
Definition: SMatrix.h:365
SerialMatrix< T > operator-() const
Unary negation.
Definition: SMatrix.h:355
static const char transT
Indicates that the matrix A is taken as its transpose: transT(A) = A^T.
Definition: SMatrix.h:39
bool indicesAreLocal(const int &row, const int &col)
Returns true if the global indices (row,col) identify a matrix element stored by the MPI process.
Definition: SMatrix.h:319
double squaredNorm()
Computes the squared Frobenius norm of the matrix (or Euclidean norm, or L2 norm of the matrix)
Definition: SMatrix.h:389
T dot(const SerialMatrix< T > &that)
Computes a "scalar product" between two matrices A and B, defined as \sum_ij A_ij * B_ij.
Definition: SMatrix.h:395
int rows() const
Find global number of rows.
Definition: SMatrix.h:279
T & operator()(const int &row, const int &col)
Get and set operator.
Definition: SMatrix.h:301
int localCols() const
Return local number of rows.
Definition: SMatrix.h:291
SerialMatrix< T > operator-=(const SerialMatrix< T > &m1)
Matrix-matrix subtraction.
Definition: SMatrix.h:143
SerialMatrix< T > operator+=(const SerialMatrix< T > &m1)
Matrix-matrix addition.
Definition: SMatrix.h:133
SerialMatrix< T > operator*=(const T &that)
Matrix-scalar multiplication.
Definition: SMatrix.h:153
SerialMatrix< T > prod(const SerialMatrix< T > &that, const char &trans1=transN, const char &trans2=transN)
Matrix-matrix multiplication.
static const char transN
Indicates that the matrix A is not modified: transN(A) = A.
Definition: SMatrix.h:36
int cols() const
Find global number of columns.
Definition: SMatrix.h:287