22#ifndef general_matrix_h
23#define general_matrix_h
45 static constexpr bool isVector {
false};
47 using value_type = AT;
63 Iterator(std::conditional_t<Const, const Mat, Mat> &mat_,
int row_,
int col_) : mat(mat_), row(row_), col(col_) {}
64 bool operator!=(
const Iterator& x)
const {
return row!=x.row || col!=x.col; }
65 Iterator& operator++() { ++row;
if(row>=mat.m) { row=0; ++col; }
return *
this; }
66 std::conditional_t<Const, const AT, AT>& operator*() {
return mat.e(row,col); }
67 std::conditional_t<Const, const AT, AT>& operator->() {
return mat.e(row,col); }
68 const AT& operator*()
const {
return mat.e(row,col); }
69 const AT& operator->()
const {
return mat.e(row,col); }
71 std::conditional_t<Const, const Mat, Mat> &mat;
86 const AT* elePtr(
int i,
int j)
const {
90 AT* elePtr(
int i,
int j) {
102 explicit Matrix() : memory(), ele(nullptr) { }
110 explicit Matrix(
int m_,
int n_,
Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
111 explicit Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=AT()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init0(a); }
112 explicit Matrix(
int m_,
int n_, Eye ini,
const AT &a=1) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init(ini,a); }
128 template<
class Row,
class Col>
138 template<
class Type,
class Row,
class Col>
150 explicit Matrix(
int m_,
int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
165 Matrix(
const std::string &strs);
172 using iterator = Iterator<false>;
173 using const_iterator = Iterator<true>;
174 iterator begin() {
return iterator(*
this,0,0); }
175 iterator end() {
return iterator(*
this,0,n); }
176 const_iterator begin()
const {
return const_iterator(*
this,0,0); }
177 const_iterator end()
const {
return const_iterator(*
this,0,n); }
179 Matrix<General,Ref,Ref,AT>& resize(
int m_,
int n_, Noinit) {
180 m = m_; n = n_; lda = m;
182 ele = (AT*)memory.get();
186 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(m,n,Noinit()).init0(a); }
188 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).init(ini,a); }
197 FMATVEC_ASSERT(m == A.
rows(), AT);
198 FMATVEC_ASSERT(n == A.
cols(), AT);
208 template<
class Type,
class Row,
class Col>
210 FMATVEC_ASSERT(m == A.
rows(), AT);
211 FMATVEC_ASSERT(n == A.
cols(), AT);
236 template<
class Type,
class Row,
class Col>
243 operator Matrix<General,Ref,Ref,AT2>()
const {
244 Matrix<General,Ref,Ref,AT2> ret(
rows(),
cols());
245 for(
size_t r=0; r<
rows(); ++r)
246 for(
size_t c=0; c<
cols(); ++c)
247 ret(r,c) = (*this)(r,c);
261 FMATVEC_ASSERT(i>=0, AT);
262 FMATVEC_ASSERT(j>=0, AT);
263 FMATVEC_ASSERT(i<m, AT);
264 FMATVEC_ASSERT(j<n, AT);
274 FMATVEC_ASSERT(i>=0, AT);
275 FMATVEC_ASSERT(j>=0, AT);
276 FMATVEC_ASSERT(i<m, AT);
277 FMATVEC_ASSERT(j<n, AT);
282 AT& e(
int i,
int j) {
286 const AT& e(
int i,
int j)
const {
339 return CblasColMajor;
396 inline Matrix<General,Ref,Ref,AT>& init(Eye,
const AT &val=1);
397 inline Matrix<General,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
398 inline Matrix<General,Ref,Ref,AT>& init0(
const AT &val=AT());
399 inline Matrix<General,Ref,Ref,AT>& init0(Init,
const AT &a=AT()) {
return init0(a); }
405 explicit inline operator std::vector<std::vector<AT>>()
const;
412 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
430 const Matrix<General,Ref,Ref,AT> T()
const;
432 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
434 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
436 template<
class Type,
class Row,
class Col>
inline void set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A);
438 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
440 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
442 template<
class Type,
class Row,
class Col>
inline void add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A);
444 template<
class Type,
class Row,
class Col>
inline void sub(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A);
446 inline const Matrix<General,Ref,Ref,AT>
operator()(
const Indices &I,
const Indices &J)
const;
448 template<
class Type,
class Row,
class Col>
inline void set(
const Indices &I,
const Indices &J,
const Matrix<Type,Row,Col,AT> &A);
450 template<
class Row>
inline void set(
const Indices &I,
int j,
const Vector<Row,AT> &x);
452 inline void ref(Matrix<General,Ref,Ref,AT> &A,
const Range<Var,Var> &I,
const Range<Var,Var> &J);
454 inline void ref(Matrix<Symmetric,Ref,Ref,AT> &A,
const Range<Var,Var> &I,
const Range<Var,Var> &J);
467 std::istringstream iss(strs);
473 throw std::runtime_error(
"Input not fully read.");
478 inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::init0(
const AT &val) {
479 for(
int i=0; i<m*n; i++) ele[i]=val;
485 for(
int i=0; i<
rows(); i++)
486 for(
int j=0; j<
cols(); j++)
493 for(
int i=0; i<m; i++)
494 for(
int j=0; j<n; j++)
495 e(i,j) = (i==j) ? val : 0;
501 FMATVEC_ASSERT(I.
end()<m, AT);
502 FMATVEC_ASSERT(J.
end()<n, AT);
505 for(
int i=0; i<A.
rows(); i++)
506 for(
int j=0; j<A.
cols(); j++)
514 FMATVEC_ASSERT(I.
end()<m, AT);
517 for(
int i=0; i<A.rows(); i++)
518 for(
int j=0; j<A.cols(); j++)
527 FMATVEC_ASSERT(i>=0, AT);
528 FMATVEC_ASSERT(i<m, AT);
532 for(
int j=0; j<n; j++)
541 FMATVEC_ASSERT(j>=0, AT);
542 FMATVEC_ASSERT(j<n, AT);
546 for(
int i=0; i<m; i++)
554 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
555 for(
int r=0; r<rows(); r++) {
556 for(
int c=0; c<cols(); c++)
563 inline Matrix<General,Ref,Ref,AT>::Matrix(
const std::vector<std::vector<AT>> &m) : memory(m.size()>0?m.size()*(m.empty()?0:m[0].size()):0), ele((AT*)memory.get()), m(static_cast<int>(m.size())), n(static_cast<int>(m.size()>0?(m.empty()?0:m[0].size()):0)), lda(static_cast<int>(m.size())) {
564 for(
int r=0; r<
rows(); r++) {
565 if(
static_cast<int>(m[r].size())!=
cols())
566 throw std::runtime_error(
"The rows of the input have different length.");
567 for(
int c=0; c<
cols(); c++)
574 template <
class AT>
template <
class Type,
class Row,
class Col>
576 for(
int i=0; i<m; i++)
577 for(
int j=0; j<n; j++)
583 inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::copy(
const Matrix<Sparse,Ref,Ref,AT> &A) {
584 for(
int i=0; i<m; i++) {
585 for(
int j=0; j<n; j++)
587 for(
int j=A.Ip()[i]; j<A.Ip()[i+1]; j++)
588 e(i,A.Jp()[j]) = A()[j];
594 inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::copy(
const Matrix<SymmetricSparse,Ref,Ref,AT> &A) {
595 for(
int i=0; i<m; i++) {
596 for(
int j=0; j<n; j++)
598 e(i,A.Jp()[A.Ip()[i]]) = A()[A.Ip()[i]];
600 for(
int i=0; i<m; i++) {
601 for(
int j=A.Ip()[i]+1; j<A.Ip()[i+1]; j++) {
602 e(i,A.Jp()[j]) = A()[j];
603 e(A.Jp()[j],i) = A()[j];
610 inline const Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::T()
const {
611 Matrix<General,Ref,Ref,AT> A(n,m,NONINIT);
612 for(
int i=0; i<n; i++)
613 for(
int j=0; j<m; j++)
618 template <
class AT>
template <
class Row>
619 inline void Matrix<General,Ref,Ref,AT>::set(
int j,
const Vector<Row,AT> &x) {
620 FMATVEC_ASSERT(j<
cols(), AT);
621 FMATVEC_ASSERT(
rows()==x.size(), AT);
622 for(
int i=0; i<
rows(); i++)
626 template <
class AT>
template <
class Col>
627 inline void Matrix<General,Ref,Ref,AT>::set(
int i,
const RowVector<Col,AT> &x) {
628 FMATVEC_ASSERT(i<
rows(), AT);
629 FMATVEC_ASSERT(
cols()==x.size(), AT);
630 for(
int j=0; j<
cols(); j++)
634 template <
class AT>
template<
class Type,
class Row,
class Col>
635 inline void Matrix<General,Ref,Ref,AT>::set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
637 FMATVEC_ASSERT(I.end()<
rows(), AT);
638 FMATVEC_ASSERT(J.end()<
cols(), AT);
639 FMATVEC_ASSERT(I.size()==A.rows(), AT);
640 FMATVEC_ASSERT(J.size()==A.cols(), AT);
642 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
643 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
647 template <
class AT>
template <
class Row>
648 inline void Matrix<General,Ref,Ref,AT>::add(
int j,
const Vector<Row,AT> &x) {
649 FMATVEC_ASSERT(j<
cols(), AT);
650 FMATVEC_ASSERT(
rows()==x.size(), AT);
651 for(
int i=0; i<
rows(); i++)
655 template <
class AT>
template <
class Col>
656 inline void Matrix<General,Ref,Ref,AT>::add(
int i,
const RowVector<Col,AT> &x) {
657 FMATVEC_ASSERT(i<
rows(), AT);
658 FMATVEC_ASSERT(
cols()==x.size(), AT);
659 for(
int j=0; j<
cols(); j++)
663 template <
class AT>
template<
class Type,
class Row,
class Col>
664 inline void Matrix<General,Ref,Ref,AT>::add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
666 FMATVEC_ASSERT(I.end()<
rows(), AT);
667 FMATVEC_ASSERT(J.end()<
cols(), AT);
668 FMATVEC_ASSERT(I.size()==A.rows(), AT);
669 FMATVEC_ASSERT(J.size()==A.cols(), AT);
671 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
672 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
673 e(i,j) += A.e(ii,jj);
676 template <
class AT>
template<
class Type,
class Row,
class Col>
677 inline void Matrix<General,Ref,Ref,AT>::sub(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
679 FMATVEC_ASSERT(I.end()<
rows(), AT);
680 FMATVEC_ASSERT(J.end()<
cols(), AT);
681 FMATVEC_ASSERT(I.size()==A.rows(), AT);
682 FMATVEC_ASSERT(J.size()==A.cols(), AT);
684 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
685 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
686 e(i,j) -= A.e(ii,jj);
691 FMATVEC_ASSERT(I.max()<
rows(), AT);
692 FMATVEC_ASSERT(J.max()<
cols(), AT);
694 Matrix<General,Ref,Ref,AT> A(I.size(),J.size(),NONINIT);
696 for(
int i=0; i<A.rows(); i++)
697 for(
int j=0; j<A.cols(); j++)
698 A.e(i,j) = e(I[i],J[j]);
703 template <
class AT>
template <
class Type,
class Row,
class Col>
704 inline void Matrix<General,Ref,Ref,AT>::set(
const Indices &I,
const Indices &J,
const Matrix<Type,Row,Col,AT> &A) {
705 FMATVEC_ASSERT(I.max()<
rows(), AT);
706 FMATVEC_ASSERT(J.max()<
cols(), AT);
707 FMATVEC_ASSERT(I.size()==A.rows(), AT);
708 FMATVEC_ASSERT(J.size()==A.cols(), AT);
709 for(
int i=0; i<I.size(); i++)
710 for(
int j=0; j<J.size(); j++)
711 e(I[i],J[j]) = A.e(i,j);
714 template <
class AT>
template<
class Row>
715 inline void Matrix<General,Ref,Ref,AT>::set(
const Indices &I,
int j,
const Vector<Row,AT> &x) {
716 FMATVEC_ASSERT(I.max()<
rows(), AT);
717 FMATVEC_ASSERT(j<
cols(), AT);
718 FMATVEC_ASSERT(I.size()==x.size(), AT);
719 for(
int i=0; i<I.size(); i++)
724 inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A,
const Range<Var,Var> &I,
const Range<Var,Var> &J) {
725 FMATVEC_ASSERT(I.end()<A.rows(), AT);
726 FMATVEC_ASSERT(J.end()<A.cols(), AT);
730 ele = A.elePtr(I.start(),J.start());
735 inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A,
const Range<Var,Var> &I,
const Range<Var,Var> &J) {
736 FMATVEC_ASSERT(I.end()<A.size(), AT);
737 FMATVEC_ASSERT(J.end()<A.size(), AT);
738 FMATVEC_ASSERT(I.start()>=J.start(), AT);
739 FMATVEC_ASSERT(J.end()<=I.start(), AT);
743 ele = A.elePtr(I.start(),J.start());
750 for (
int i = 0; i < m; i++) {
751 for (
int j = 0; j < n; j++) {
752 if (fabs(e(i, j)) > 1e-16 || i==j) {
Shape class for general matrices.
Definition: types.h:116
This is a matrix class for general matrices.
Definition: general_matrix.h:42
Matrix< General, Ref, Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: general_matrix.h:221
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:260
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:273
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:301
CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:338
Matrix()
Standard constructor.
Definition: general_matrix.h:102
int ldim() const
Leading dimension.
Definition: general_matrix.h:319
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:139
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:150
int cols() const
Number of columns.
Definition: general_matrix.h:313
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< General, Ref, Ref, AT > &A)
Assignment operator.
Definition: general_matrix.h:196
~Matrix()=default
Destructor.
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: general_matrix.h:209
int rows() const
Number of rows.
Definition: general_matrix.h:307
AT * operator()()
Pointer operator.
Definition: general_matrix.h:295
CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:327
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:119
int nonZeroElements() const
Cast to AT.
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:129
This is a matrix class for sparse quadratic matrices.
Definition: sparse_matrix.h:44
This is a matrix class for sparse quadratic matrices.
Definition: symmetric_sparse_matrix.h:44
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:52
int rows() const
Number of rows.
int cols() const
Number of columns.
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
This is an index class for creating submatrices.
Definition: range.h:44
int end() const
Last element.
Definition: range.h:91
int size() const
Size.
Definition: range.h:97
int start() const
First element.
Definition: range.h:85
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
Shape class for symmetric matrices.
Definition: types.h:132
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
Namespace fmatvec.
Definition: _memory.cc:28
bool operator!=(const Matrix< Type1, Row1, Col1, AT > &A, const Matrix< Type2, Row2, Col2, AT > &B)
Matrix-matrix comparison.
Definition: linear_algebra.h:1935