34 if (row + nRows > M.
size1() || col + nCols > M.
size2()) {
35 throw std::runtime_error(
"Submatrix exceeds matrix size.");
94 throw std::out_of_range(
"ComplexMatrix indices are out of range.");
105 throw std::out_of_range(
"ComplexMatrix indices are out of range.");
125 const auto n =
d.size();
128 for (
size_t i = 0; i <
n; ++i) {
160 if (
m.size1() !=
size2()) {
161 throw std::invalid_argument(
"Matrix by matrix multiplication: matricies are of incompatible sizes.");
171 if (
m.size1() !=
size2()) {
172 throw std::invalid_argument(
"Matrix by matrix multiplication: matricies are of incompatible sizes.");
186 throw std::invalid_argument(
"System of linear equations: the matrix must be square.");
189 if (
rhs.size() !=
n) {
190 throw std::invalid_argument(
"System of linear equations: right-hand side vector has wrong size.");
193 throw std::invalid_argument(
"Matrix A is singular.");
196 Eigen::MatrixXcd b =
rhs.eigen();
197 Eigen::ColPivHouseholderQR<Eigen::MatrixXcd> dec(
eigen());
198 Eigen::VectorXcd res = dec.solve(b);
201 if (!
rhs.eigen().isApprox(
eigen() *
x.eigen())) {
202 throw std::runtime_error(
"Matrix Solution Error: solution does not exist.");
209 throw std::runtime_error(
"Matrix inverse: the matrix must be square.");
217 throw std::runtime_error(
"Matrix inverse: the matrix must be square.");
231 throw std::runtime_error(
"Matrix eigenSystem: the matrix must be square.");
235 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXcd> solver;
238 eigenValues = solver.eigenvalues();
239 eigenVectors = solver.eigenvectors();
246 throw std::out_of_range(
"EigenMatrix row index is out of range.");
257 throw std::out_of_range(
"ComplexMatrix column index is out of range.");
260 Eigen::VectorXcd col =
268 Eigen::MatrixXcd matrix = Eigen::MatrixXcd(
size1(),
size2());
269 for (
size_t col = 0; col <
size2(); ++col) {
270 auto col1 = indices[col];
271 for (
size_t row = 0; row <
size1(); ++row) {
272 matrix(row, col) =
m_matrix(row, col1);
281 const size_t n1 =
size1();
282 const size_t n2 =
size2();
284 std::vector<double> packed(2 * n1 * n2);
285 for (
size_t i = 0; i < n1; ++i) {
286 for (
size_t j = 0; j < n2; ++j) {
287 auto k = 2 * (i * n2 + j);
289 packed[k] =
value.real();
290 packed[k + 1] =
value.imag();
301 const size_t n1 =
size1();
302 const size_t n2 =
size2();
303 if (2 * n1 * n2 != packed.size()) {
304 throw std::runtime_error(
"Cannot unpack vector into ComplexMatrix: size mismatch.");
306 for (
size_t i = 0; i < n1; ++i) {
307 for (
size_t j = 0; j < n2; ++j) {
308 auto k = 2 * (i * n2 + j);
const std::vector< double > & rhs
Eigen::Map< const Eigen::VectorXcd, 0, Mantid::CurveFitting::dynamic_stride > complex_vector_map_type
Eigen::Map< const Eigen::MatrixXcd, 0, Mantid::CurveFitting::dynamic_stride > complex_matrix_map_type
double value
The value of the point.
A complex-valued matrix for linear algebra computations.
ComplexMatrix operator*(const EigenMatrix &m) const
Multiply this matrix by a matrix.
Eigen::MatrixXcd m_matrix
The pointer to the complex Eigen matrix.
void resize(const size_t nx, const size_t ny)
Resize the matrix.
void invert()
Invert this matrix.
size_t size2() const
Second size of the matrix.
void identity()
Set this matrix to identity matrix.
void zero()
Set all elements to zero.
ComplexMatrix()
Constructor.
size_t size1() const
First size of the matrix.
ComplexType det()
Calculate the determinant.
void sortColumns(const std::vector< size_t > &indices)
Sort columns in order defined by an index array.
Eigen::MatrixXcd & eigen()
Get the reference to the eigen matrix.
ComplexMatrix ctr() const
Get "conjugate transposed" matrix to be used in multiplications.
std::vector< double > packToStdVector() const
Pack the matrix into a single std vector of doubles (for passing in and out of algorithms)
ComplexMatrix & operator*=(const ComplexType &d)
Multiply this matrix by a number.
void solve(const ComplexVector &rhs, ComplexVector &x)
Solve system of linear equations M*x == rhs, M is this matrix This matrix is destroyed.
ComplexMatrix tr() const
Get "transposed" matrix to be used in multiplications.
bool isEmpty() const
Is matrix empty.
ComplexVector copyRow(const size_t i) const
Copy a row into a GSLVector.
void unpackFromStdVector(const std::vector< double > &v)
Unpack an std vector into this matrix.
ComplexVector copyColumn(const size_t i) const
Copy a column into a GSLVector.
ComplexMatrix & operator+=(const ComplexMatrix &M)
Add a matrix to this.
void eigenSystemHermitian(EigenVector &eigenValues, ComplexMatrix &eigenVectors)
Calculate the eigensystem of a Hermitian matrix.
ComplexType get(const size_t i, const size_t j) const
Get an element.
ComplexMatrix & operator-=(const ComplexMatrix &M)
Subtract a matrix from this.
ComplexMatrix & operator=(const ComplexMatrix &M)
Copy assignment operator.
void set(const size_t i, const size_t j, const ComplexType value)
Set an element.
ComplexType operator()(const size_t i, const size_t j) const
The "index" operator.
void diag(const ComplexVector &d)
Set the matrix to be diagonal.
A complex-valued vector for linear algebra computations.
A wrapper around Eigen::Matrix.
A wrapper around Eigen::Vector.
void resize(const size_t n)
Resize the vector.
std::complex< double > ComplexType
Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > dynamic_stride