1#ifndef CHI_MATH_DYNAMIC_MATRIX_H
2#define CHI_MATH_DYNAMIC_MATRIX_H
12 template<
class NumberFormat>
20template<
class NumberFormat>
23 typedef std::pair<size_t,size_t>
MatDim;
30 static_assert(std::is_floating_point<NumberFormat>::value,
31 "Only floating point number formats are "
32 "supported for DynamicMatrix." );
37 elements_(Nrows, std::vector<NumberFormat>(Ncols))
39 static_assert(std::is_floating_point<NumberFormat>::value,
40 "Only floating point number formats are "
41 "supported for DynamicMatrix." );
46 elements_(Nrows, std::vector<NumberFormat>(Ncols, value))
48 static_assert(std::is_floating_point<NumberFormat>::value,
49 "Only floating point number formats are "
50 "supported for DynamicMatrix." );
65 {
elements_ = std::move(other.elements_);}
86 DynamicMatrix(std::initializer_list<std::initializer_list<NumberFormat>> in)
122 void resize(
size_t Nrows,
size_t Ncols,
const NumberFormat& val)
127 row.resize(Ncols,val);
128 for (
auto& entry : row)
145 typename std::vector<std::vector<NumberFormat>>::iterator
148 typename std::vector<std::vector<NumberFormat>>::iterator
164 if ((a.first != b.first) or (a.second != b.second))
165 throw std::length_error(
"Mismatched square sizes of DynamicMatrix");
170 if (a.first != b.second)
171 throw std::length_error(
"Mismatched matrix A rows with matrix B cols"
172 " in DynamicMatrix");
183 for (
int i=0; i<dim.first; ++i)
184 for (
int j=0; j<dim.second;++j)
196 for (
int i=0; i<dim.first; ++i)
197 for (
int j=0; j<dim.second;++j)
211 for (
int i=0; i<dim.first; ++i)
212 for (
int j=0; j<dim.second;++j)
224 for (
int i=0; i<dim.first; ++i)
225 for (
int j=0; j<dim.second;++j)
238 for (
int i=0; i<dim.first; ++i)
239 for (
int j=0; j<dim.second;++j)
250 for (
int i=0; i<dim.first; ++i)
251 for (
int j=0; j<dim.second;++j)
265 unsigned int istar=0;
266 unsigned int jstar=0;
267 for (
unsigned int i=0; i<dimA.first; ++i)
269 for (
unsigned int j=0; j<dimA.first; ++j)
271 NumberFormat value = 0.0;
272 for (
unsigned int k=0; k<dimA.second; ++k)
275 newMatrix[istar][jstar] = value;
289 auto dimV = V.
size();
291 if (dimA.second != dimV)
292 throw std::length_error(
"Mismatched matrix vector sizes in"
293 " matrix-vector multiplication: DynamicMatrix");
297 for (
unsigned int i=0; i<dimA.first; ++i)
299 NumberFormat value = 0.0;
300 for (
unsigned int j=0; j<dimA.second; ++j)
316 for (
int i=0; i<dim.first; ++i)
317 for (
int j=0; j<dim.second;++j)
328 for (
int i=0; i<dim.first; ++i)
329 for (
int j=0; j<dim.second;++j)
348 auto dimV = V.
size();
350 if ((dimA.first != dimV) or (dimA.second != dimV))
352 throw std::length_error(
"Mismatched matrix vector sizes in"
353 " matrix-vector diagonal assignment: DynamicMatrix");
356 for (
int i=0; i<dimA.first; ++i)
365 for (
int i=0; i<dimA.first; ++i)
373 std::stringstream out;
375 for (
int i = 0; i<dim.first ; ++i)
377 for (
int j=0; j<(dim.second-1); ++j)
381 if (i<(dim.first -1)) out <<
"\n";
389template<
class NumberFormat>
395 for (
int i=0; i<dim.first; ++i)
396 for (
int j=0; j<dim.second;++j)
DynamicMatrix(DynamicMatrix &&other) noexcept
static void bounds_check_rows_cols(const MatDim a, const MatDim b)
DynamicMatrix operator-(const DynamicMatrix &rhs) const
DynamicMatrix & operator-=(const DynamicMatrix &rhs)
DynamicMatrix Inverse() const
DynamicMatrix & operator+=(const DynamicMatrix &rhs)
static void bounds_check_colsA_rowsB(const MatDim a, const MatDim b)
void SetDiagonal(DynamicVector< NumberFormat > &V)
DynamicMatrix & operator=(DynamicMatrix &&other) noexcept
std::vector< std::vector< NumberFormat > > elements_
DynamicMatrix operator*(const DynamicMatrix &rhs)
std::vector< NumberFormat > & at(size_t i)
bool empty() const noexcept
std::vector< std::vector< NumberFormat > >::iterator end()
DynamicMatrix & operator*=(const NumberFormat value)
std::vector< NumberFormat > * data()
DynamicMatrix operator*(const NumberFormat value) const
void resize(size_t Nrows, size_t Ncols)
DynamicMatrix & operator=(const DynamicMatrix &other)
std::vector< NumberFormat > & front()
DynamicMatrix operator/(const NumberFormat value) const
std::string PrintStr() const
DynamicMatrix operator+(const DynamicMatrix &rhs) const
MatDim Dimensions() const
void resize(size_t Nrows, size_t Ncols, const NumberFormat &val)
DynamicVector< NumberFormat > operator*(const DynamicVector< NumberFormat > &V)
DynamicMatrix(size_t Nrows, size_t Ncols)
DynamicMatrix(size_t Nrows, size_t Ncols, NumberFormat value)
void reserve(size_t Nrows)
std::vector< NumberFormat > & operator[](size_t i)
std::vector< std::vector< NumberFormat > >::iterator begin()
DynamicMatrix & operator=(std::initializer_list< std::initializer_list< NumberFormat > > in)
std::vector< NumberFormat > & back()
std::pair< size_t, size_t > MatDim
DynamicMatrix(std::initializer_list< std::initializer_list< NumberFormat > > in)
void push_back(const std::vector< NumberFormat > &val)
DynamicMatrix & operator/=(const NumberFormat value)
DynamicMatrix(const DynamicMatrix &other)
DynamicMatrix & operator=(const std::vector< std::vector< double > > &in)
void SetDiagonal(NumberFormat val)
DynamicMatrix(const std::vector< std::vector< double > > &in)
chi_math::DynamicMatrix< NumberFormat > operator*(const double value, chi_math::DynamicMatrix< NumberFormat > &that)
MatDbl InverseGEPivoting(const MatDbl &A)