15 #ifndef included_Matrix_h
16 #define included_Matrix_h
57 bool randomized =
false);
82 bool copy_data =
true);
154 if (num_rows > INT_MAX / num_cols)
155 CAROM_ERROR(
"Matrix::setSize- new size exceeds maximum integer value!\n");
157 int new_size = num_rows*num_cols;
158 if (new_size > d_alloc_size) {
160 CAROM_ERROR(
"Can not reallocate externally owned storage.");
167 d_mat =
new double [new_size] {0.0};
168 d_alloc_size = new_size;
170 d_num_rows = num_rows;
171 d_num_cols = num_cols;
173 calculateNumDistributedRows();
185 return d_distributed;
213 if (!d_distributed) {
216 return d_num_distributed_rows;
240 std::unique_ptr<Matrix>
272 std::unique_ptr<Matrix>
274 const Matrix& other)
const
276 Matrix* result =
new Matrix(d_num_rows, other.d_num_cols, d_distributed);
277 mult(other, *result);
278 return std::unique_ptr<Matrix>(result);
316 std::unique_ptr<Vector>
318 const Vector& other)
const
321 mult(other, *result);
322 return std::unique_ptr<Vector>(result);
403 std::unique_ptr<Matrix>
405 const Matrix& other)
const
407 Matrix *result =
new Matrix(d_num_rows, d_num_cols, d_distributed);
409 return std::unique_ptr<Matrix>(result);
434 std::unique_ptr<Matrix>
437 Matrix *result =
new Matrix(d_num_rows, d_num_cols, d_distributed);
439 return std::unique_ptr<Matrix>(result);
490 std::unique_ptr<Matrix>
492 const Matrix& other)
const
494 Matrix* result =
new Matrix(d_num_cols, other.d_num_cols,
false);
496 return std::unique_ptr<Matrix>(result);
534 std::unique_ptr<Vector>
536 const Vector& other)
const
540 return std::unique_ptr<Vector>(result);
572 std::unique_ptr<Matrix>
575 Matrix* result =
new Matrix(d_num_rows, d_num_cols,
false);
577 return std::unique_ptr<Matrix>(result);
610 std::unique_ptr<Vector>
615 return std::unique_ptr<Vector>(result);
652 std::unique_ptr<Matrix>
660 void qr_factorize(std::vector<std::unique_ptr<Matrix>> & QR)
const;
679 int* row_pivot_owner,
680 int pivots_requested)
const;
697 orthogonalize(
bool double_pass =
false,
double zero_tol = 1.0e-15);
720 double zero_tol = 1.0e-15);
749 CAROM_ASSERT((0 <= row) && (row <
numRows()));
750 CAROM_ASSERT((0 <= col) && (col <
numColumns()));
751 return d_mat[row*d_num_cols+col];
771 CAROM_ASSERT((0 <= row) && (row <
numRows()));
772 CAROM_ASSERT((0 <= col) && (col <
numColumns()));
773 return d_mat[row*d_num_cols+col];
788 return item(row, col);
805 return item(row, col);
814 void print(
const char * prefix)
const;
822 void write(
const std::string& base_file_name)
const;
830 void read(
const std::string& base_file_name);
839 void local_read(
const std::string& base_file_name,
int rank);
877 calculateNumDistributedRows();
894 qrcp_pivots_transpose_serial(
int* row_pivot,
895 int* row_pivot_owner,
896 int pivots_requested)
const;
913 qrcp_pivots_transpose_distributed(
int* row_pivot,
914 int* row_pivot_owner,
915 int pivots_requested)
const;
918 qrcp_pivots_transpose_distributed_scalapack(
int* row_pivot,
919 int* row_pivot_owner,
920 int pivots_requested)
const;
938 qrcp_pivots_transpose_distributed_elemental(
int* row_pivot,
939 int* row_pivot_owner,
940 int pivots_requested)
const;
958 qrcp_pivots_transpose_distributed_elemental_balanced
959 (
int* row_pivot,
int* row_pivot_owner,
int pivots_requested)
const;
977 qrcp_pivots_transpose_distributed_elemental_unbalanced
978 (
int* row_pivot,
int* row_pivot_owner,
int pivots_requested)
const;
991 void qr_factorize(
bool computeR, std::vector<Matrix*> & QR)
const;
1006 int d_num_distributed_rows;
1136 std::vector<std::complex<double>>
eigs;
1208 std::unique_ptr<Matrix> SpaceTimeProduct(
const CAROM::Matrix & As,
1211 const std::vector<double> *tscale=NULL,
1212 const bool At0at0=
false,
const bool Bt0at0=
false,
1213 const bool lagB=
false,
const bool skip0=
false);
void local_read(const std::string &base_file_name, int rank)
read a single rank of a distributed Matrix into (a) HDF file(s).
void setSize(int num_rows, int num_cols)
Sets the number of rows and columns of the matrix and reallocates storage if needed....
std::unique_ptr< Vector > mult(const Vector &other) const
Multiplies this Matrix with other and returns the product.
double * getData() const
Get the matrix data as a pointer.
bool balanced() const
Returns true if rows of matrix are load-balanced.
const double & operator()(int row, int col) const
Const Matrix member access.
void transposePseudoinverse()
Computes the transposePseudoinverse of this.
bool distributed() const
Returns true if the Matrix is distributed.
void rescale_rows_max()
Rescale every matrix row by its maximum absolute value.
Matrix & operator=(const Matrix &rhs)
Assignment operator.
void write(const std::string &base_file_name) const
write Matrix into (a) HDF file(s).
int numColumns() const
Returns the number of columns in the Matrix. This method will return the same value from each process...
std::unique_ptr< Vector > transposeMult(const Vector &other) const
Multiplies the transpose of this Matrix with other and returns the product.
void distribute(const int &local_num_rows)
Distribute this matrix rows among MPI processes, based on the specified local number of rows....
void gather()
Gather all the distributed rows among MPI processes. This becomes not distributed after this function...
void orthogonalize_last(int ncols=-1, bool double_pass=false, double zero_tol=1.0e-15)
Orthonormalizes the matrix's last column, assuming the previous columns are already orthonormal.
std::unique_ptr< Matrix > qr_factorize() const
Computes and returns the Q of the QR factorization of this.
Matrix & operator+=(const Matrix &rhs)
Addition operator.
int numDistributedRows() const
Returns the number of rows of the Matrix across all processors.
Matrix & operator-=(const Matrix &rhs)
Subtraction operator.
std::unique_ptr< Vector > getColumn(int column) const
Returns a deep copy of a column of the matrix.
void orthogonalize(bool double_pass=false, double zero_tol=1.0e-15)
Orthonormalizes the matrix.
std::unique_ptr< Matrix > inverse() const
Computes and returns the inverse of this.
void multPlus(Vector &a, const Vector &b, double c) const
Computes a += this*b*c.
double & item(int row, int col)
Non-const Matrix member access. Matrix data is stored in row-major format.
void print(const char *prefix) const
print Matrix into (a) ascii file(s).
std::unique_ptr< Matrix > getFirstNColumns(int n) const
Get the first N columns of a matrix.
std::unique_ptr< Matrix > elementwise_mult(const Matrix &other) const
Multiplies two matrices element-wise.
void pointwise_mult(int this_row, const Vector &other, Vector &result) const
Multiplies a specified row of this Matrix with other pointwise.
const double & item(int row, int col) const
Const Matrix member access. Matrix data is stored in row-major format.
int numRows() const
Returns the number of rows of the Matrix on this processor.
void read(const std::string &base_file_name)
read Matrix into (a) HDF file(s).
std::unique_ptr< Matrix > mult(const Matrix &other) const
Multiplies this Matrix with other and returns the product.
void transpose()
Replaces this Matrix with its transpose (in place), in the serial square case only.
std::unique_ptr< Matrix > elementwise_square() const
Square every element in the matrix.
std::unique_ptr< Matrix > transposeMult(const Matrix &other) const
Multiplies the transpose of this Matrix with other and returns the product.
void rescale_cols_max()
Rescale every matrix column by its maximum absolute value.
void qrcp_pivots_transpose(int *row_pivot, int *row_pivot_owner, int pivots_requested) const
Compute the leading numColumns() column pivots from a QR decomposition with column pivots (QRCP) of t...
ComplexEigenPair NonSymmetricRightEigenSolve(const Matrix &A)
Computes the eigenvectors/eigenvalues of an NxN real nonsymmetric matrix.
Matrix IdentityMatrixFactory(const Vector &v)
Factory function to make an identity matrix with rows distributed in the same fashion as its Vector a...
Matrix outerProduct(const Vector &v, const Vector &w)
Computes the outer product of two Vectors, v and w.
void SerialSVD(Matrix *A, Matrix *U, Vector *S, Matrix *V)
Computes the SVD of a undistributed matrix in serial.
Matrix DiagonalMatrixFactory(const Vector &v)
Factory function to make a diagonal matrix with nonzero entries as in its Vector argument....
EigenPair SymmetricRightEigenSolve(const Matrix &A)
Computes the eigenvectors/eigenvalues of an NxN real symmetric matrix.
Matrix * ev_real
The real component of the eigenvectors in matrix form.
std::vector< std::complex< double > > eigs
The corresponding complex eigenvalues.
Matrix * ev_imaginary
The imaginary component of the eigenvectors in matrix form.
std::vector< double > eigs
The corresponding eigenvalues.
Matrix * ev
The eigenvectors in matrix form.
Matrix * U
The left singular vectors.
Vector * S
The singular values.
Matrix * V
The right singular vectors.