19#ifndef GIMLI_SPARSEMATRIX__H
20#define GIMLI_SPARSEMATRIX__H
23#include "elementmatrix.h"
25#include "vectortemplates.h"
28#include "meshentities.h"
42#define SPARSE_NOT_VALID throwError(WHERE_AM_I + " no data/or sparsity pattern defined.");
45template<
class ValueType,
class IndexType,
class ContainerType >
class MatrixElement {
47 typedef std::pair< IndexType, IndexType > IndexPair;
48 typedef MatrixElement< ValueType, IndexType, ContainerType > & Reference;
50 MatrixElement(ContainerType & Cont, IndexType r, IndexType c)
51 : C(Cont), I(C.find(IndexPair(r, c))), row(r), column(c) {
62 Reference operator = (Reference rhs) {
64 return operator = (rhs.asValue());
75 ValueType asValue()
const {
76 if (I == C.end())
return ValueType(0);
else return (*I).second;
80 operator ValueType ()
const {
return asValue(); }
87 Reference operator = (
const ValueType &
x) {
89 if (
x != ValueType(0) || 1) {
95 assert(C.size() < C.max_size());
96 I = (C.insert(
typename ContainerType::value_type(IndexPair(row, column),
x))).first;
97 }
else (*I).second =
x;
119 Reference operator += (
const ValueType &
x) {
120 if (
x != ValueType(0) || 1 ) {
122 assert(C.size() < C.max_size());
123 I = (C.insert(
typename ContainerType::value_type(IndexPair(row, column),
x))).first;
124 }
else (*I).second +=
x;
129 Reference operator -= (
const ValueType &
x) {
130 if (
x != ValueType(0) || 1) {
132 assert(C.size() < C.max_size());
133 I = (C.insert(
typename ContainerType::value_type(IndexPair(row, column), -
x))).first;
134 }
else (*I).second -=
x;
141 typename ContainerType::iterator I;
142 IndexType row, column;
148template<
class ValueType,
class IndexType >
151 typedef std::pair< IndexType, IndexType > IndexPair;
152 typedef std::map< IndexPair, ValueType, std::less< IndexPair > > ContainerType;
153 typedef typename ContainerType::iterator iterator;
154 typedef typename ContainerType::const_iterator const_iterator;
163 :
MatrixBase(), rows_(0), cols_(0), stype_(0) {
164 this->
load(filename);
174 for (const_iterator it = S.begin(); it != S.end(); it ++){
175 this->setVal(it->first.first, it->first.second, it->second);
187 ASSERT_EQUAL(i.size(), j.size())
188 ASSERT_EQUAL(i.size(), v.size())
192 for (Index n = 0; n < i.size(); n ++ ) (*
this)[i[n]][j[n]] = v[n];
201 for (const_iterator it = S.begin(); it != S.end(); it ++){
202 this->setVal(it->first.first, it->first.second, it->second);
215 virtual uint
rtti()
const {
return GIMLI_SPARSE_MAP_MATRIX_RTTI; }
227 const RVector & vals) {
228 ASSERT_EQUAL(vals.size(),
rows.size())
229 ASSERT_EQUAL(vals.size(),
cols.size())
231 for (Index i = 0; i < vals.size(); i ++){
232 (*this)[
rows[i]][
cols[i]] += vals[i];
238 cols_ = 0; rows_ = 0; stype_ = 0;
241 void cleanRow(IndexType row){
242 ASSERT_RANGE(row, 0, this->
rows())
244 for (auto it = begin(); it != end();){
245 if (idx1(it) == row){
253 void cleanCol(IndexType col){
254 ASSERT_RANGE(col, 0, this->
cols())
255 for (auto it = begin(); it != end();){
256 if (idx2(it) == col){
265 inline int stype()
const {
return stype_;}
267 inline void setRows(IndexType r) { rows_ = r ; }
268 virtual IndexType
rows()
const {
return rows_; }
269 virtual IndexType nRows()
const {
return rows_; }
271 inline void setCols(IndexType c) { cols_ = c ; }
272 virtual IndexType
cols()
const {
return cols_; }
273 virtual IndexType nCols()
const {
return cols_; }
275 inline IndexType size()
const {
return C_.size(); }
276 inline IndexType max_size()
const {
return C_.max_size(); }
277 inline IndexType nVals()
const {
return C_.size(); }
279 inline iterator begin() {
return C_.begin(); }
280 inline iterator end() {
return C_.end(); }
282 inline const_iterator begin()
const {
return C_.begin(); }
283 inline const_iterator end()
const {
return C_.end(); }
286 void add(
const ElementMatrix < double > & A, ValueType scale=1.0);
287 void add(
const ElementMatrix < double > & A,
const Pos & scale);
290 void add(
const ElementMatrix < double > & A,
291 const Vector < ValueType > & scale);
293 void addToCol(Index
id,
const ElementMatrix < double > & A,
294 ValueType scale=1.0,
bool isDiag=
false);
295 void addToRow(Index
id,
const ElementMatrix < double > & A,
296 ValueType scale=1.0,
bool isDiag=
false);
298#define DEFINE_SPARSEMAPMATRIX_UNARY_MOD_OPERATOR__(OP) \
299 SparseMapMatrix< ValueType, IndexType > & operator OP##= (const ValueType & v){\
300 for (iterator it = begin(); it != end(); it ++) (*it).second OP##= v; \
304 DEFINE_SPARSEMAPMATRIX_UNARY_MOD_OPERATOR__(+)
305 DEFINE_SPARSEMAPMATRIX_UNARY_MOD_OPERATOR__(-)
306 DEFINE_SPARSEMAPMATRIX_UNARY_MOD_OPERATOR__(*)
307 DEFINE_SPARSEMAPMATRIX_UNARY_MOD_OPERATOR__(/)
309#undef DEFINE_SPARSEMMAPATRIX_UNARY_MOD_OPERATOR__
312 for (const_iterator it = A.begin(); it != A.end(); it ++){
313 this->addVal(it->first.first, it->first.second, it->second);
318 for (const_iterator it = A.begin(); it != A.end(); it ++){
319 this->addVal(it->first.first, it->first.second, -it->second);
325 void operator += (
const ElementMatrix < double > & A){
326 for (Index i = 0, imax = A.size(); i < imax; i++){
327 for (Index j = 0, jmax = A.size(); j < jmax; j++){
328 (*this)[A.idx(i)][A.idx(j)] += A.getVal(i, j);
335 Aux(IndexType r, IndexType maxs, ContainerType & Cont,
int stype)
336 : Row(r), maxColumns(maxs), C(Cont), stype_(
stype) { }
338 MatElement operator [] (IndexType c) {
340 if ((c < 0 || c >= maxColumns) || (stype_ < 0 && c < Row) || (stype_ > 0 && c > Row)) {
342 WHERE_AM_I +
" idx = " + str(c) +
", " + str(Row) +
" maxcol = "
343 + str(maxColumns) +
" stype: " + str(stype_));
345 return MatElement(C, Row, c);
348 IndexType Row, maxColumns;
353 Aux operator [] (IndexType r) {
354 if (r < 0 || r >= rows_){
356 WHERE_AM_I +
" idx = " + str(r) +
" maxrow = "
359 return Aux(r,
cols(), C_, stype_);
362 inline IndexType idx1(
const const_iterator & I)
const {
return (*I).first.first; }
363 inline IndexType idx2(
const const_iterator & I)
const {
return (*I).first.second; }
368 inline const ValueType & val(
const const_iterator & I)
const {
return (*I).second; }
370 inline ValueType & val(
const iterator & I) {
return (*I).second; }
372 inline Vector< ValueType > values()
const {
373 Vector< ValueType > ret(C_.size());
375 for (const_iterator it = this->begin(); it != this->end(); it ++, i ++){
381 inline ValueType getVal(IndexType i, IndexType j) {
return (*
this)[i][j]; }
383 inline void setVal(IndexType i, IndexType j,
const ValueType & val) {
384 if ((stype_ < 0 && i > j) || (stype_ > 0 && i < j))
return;
386 if (i >= rows_) rows_ = i+1;
387 if (j >= cols_) cols_ = j+1;
398 inline void addVal(IndexType i, IndexType j,
const ValueType & val) {
399 if ((stype_ < 0 && i > j) || (stype_ > 0 && i < j))
return;
400 if (i >= rows_) rows_ = i+1;
401 if (j >= cols_) cols_ = j+1;
402 (*this)[i][j] += val;
417 virtual Vector < ValueType >
mult(
const Vector < ValueType > & a)
const {
418 Vector < ValueType > ret(this->
rows(), 0.0);
420 ASSERT_EQUAL(this->
cols(), a.size())
423 for (const_iterator it = this->begin(); it != this->end(); it ++){
424 ret[it->first.first] += a[it->first.second] * it->second;
426 }
else if (stype_ == -1){
428 for (const_iterator it = this->begin(); it != this->end(); it ++){
429 IndexType I = it->first.first;
430 IndexType J = it->first.second;
432 ret[I] += a[J] * conj(it->second);
435 ret[J] += a[I] * it->second;
438 }
else if (stype_ == 1){
439 for (const_iterator it = this->begin(); it != this->end(); it ++){
440 IndexType I = it->first.first;
441 IndexType J = it->first.second;
443 ret[I] += a[J] * conj(it->second);
446 ret[J] += a[I] * it->second;
455 virtual Vector < ValueType >
transMult(
const Vector < ValueType > & a)
const {
456 Vector < ValueType > ret(this->
cols(), 0.0);
458 ASSERT_EQUAL(this->
rows(), a.size())
461 for (const_iterator it = this->begin(); it != this->end(); it ++){
462 ret[it->first.second] += a[it->first.first] * it->second;
464 }
else if (stype_ == -1){
466 }
else if (stype_ == 1){
472 virtual Vector < ValueType > col(
const Index i) {
473 Vector < ValueType > null(this->
cols(), 0.0);
476 return this->
mult(null);
479 virtual Vector < ValueType > row(
const Index i) {
480 Vector < ValueType > null(this->
rows(), 0.0);
486 void save(
const std::string & filename)
const {
487 std::fstream file; openOutFile(filename, &file);
489 for (const_iterator it = begin(); it != end(); it ++){
490 file << idx1(it) <<
" " << idx2(it) <<
" " << val(it) << std::endl;
496 void load(
const std::string & filename){
497 std::fstream file; openInFile(filename, &file);
498 std::vector < IndexType > vi, vj;
499 std::vector < ValueType > vval;
502 while (file >> i >> j >> val){
508 setRows(IndexType(max(vi) + 1));
509 setCols(IndexType(max(vj) + 1));
511 for (Index i = 0; i < vi.size(); i ++){
512 (*this)[vi[i]][vj[i]] = vval[i];
517 void importCol(
const std::string & filename,
double dropTol, Index colOffset){
520 FILE *file; file = fopen(filename.c_str(),
"r+b");
522 throwError(WHERE_AM_I +
" " + filename +
": " + strerror(errno));
528 ret = fread(&
rows,
sizeof(uint32), 1, file);
529 if (ret == 0) throwError(
"fail reading file " + filename);
531 ret = fread(&
cols,
sizeof(uint32), 1, file);
532 if (ret == 0) throwError(
"fail reading file " + filename);
534 for (uint i = 0; i <
rows; i ++){
535 for (uint j = 0; j <
cols; j ++){
536 ret = fread(&val,
sizeof(ValueType), 1, file);
537 if (ret == 0) throwError(
"fail reading file " + filename);
538 if (abs(val) > dropTol) this->setVal(i, j + colOffset, val);
545 void importCol(
const std::string & filename,
double dropTol=1e-3){
553 rows.resize(C_.size());
554 cols.resize(C_.size());
557 for (const_iterator it = this->begin(); it != this->end(); it ++, i ++){
566 IndexType rows_, cols_;
573template <
class ValueType,
class IndexType >
574void save(
const SparseMapMatrix< ValueType, IndexType > & S,
575 const std::string & fname){
579template <
class ValueType,
class IndexType >
581 const std::string & fname){
582 return S.load(fname);
585inline RVector operator * (
const RSparseMapMatrix & A,
const RVector & b){
589inline CVector operator * (
const CSparseMapMatrix & A,
const CVector & b){
593inline CVector operator * (
const CSparseMapMatrix & A,
const RVector & b){
594 return A.mult(toComplex(b));
597inline RVector transMult(
const RSparseMapMatrix & A,
const RVector & b){
598 return A.transMult(b);
601inline CVector transMult(
const CSparseMapMatrix & A,
const CVector & b){
602 return A.transMult(b);
605inline CVector transMult(
const CSparseMapMatrix & A,
const RVector & b){
606 return A.transMult(toComplex(b));
609inline RSparseMapMatrix real(
const CSparseMapMatrix & A){
610 RSparseMapMatrix R(A.rows(), A.cols());
611 for (CSparseMapMatrix::const_iterator it = A.begin(); it != A.end(); it ++){
612 R.setVal(it->first.first, it->first.second, it->second.real());
617inline RSparseMapMatrix imag(
const CSparseMapMatrix & A){
618 RSparseMapMatrix R(A.rows(), A.cols());
619 for (CSparseMapMatrix::const_iterator it = A.begin(); it != A.end(); it ++){
620 R.setVal(it->first.first, it->first.second, it->second.imag());
625inline RSparseMapMatrix operator + (
const RSparseMapMatrix & A,
const RSparseMapMatrix & B){
626 RSparseMapMatrix tmp(A);
630inline RSparseMapMatrix operator - (
const RSparseMapMatrix & A,
const RSparseMapMatrix & B){
631 RSparseMapMatrix tmp(A);
635#define DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(OP) \
636 inline RSparseMapMatrix operator OP (const RSparseMapMatrix & A, const double & v){\
637 return RSparseMapMatrix(A) OP##= v; \
640 DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(+)
641 DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(-)
642 DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(*)
643 DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(/)
645#undef DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__
647#define DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(OP) \
648 inline RSparseMapMatrix operator OP (const double & v, const RSparseMapMatrix & A){\
649 return RSparseMapMatrix(A) OP##= v; \
652 DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(+)
653 DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__(*)
655#undef DEFINE_SPARSEMAPMATRIX_EXPR_OPERATOR__
662 const Vec & l,
const Vec & r) {
664 if (S.
cols() != r.size())
665 throwLengthError(WHERE_AM_I +
" " + str(S.
cols())
666 +
" != " + str(r.size()));
667 if (S.
rows() != l.size())
668 throwLengthError(WHERE_AM_I +
" " + str(S.
rows())
669 +
" != " + str(l.size()));
672 S.val(it) *= l[S.idx1(it)] * r[S.idx2(it)];
684 if (S.
cols() != v.size())
685 throwLengthError(WHERE_AM_I +
" " + str(S.
cols())
686 +
" != " + str(v.size()));
687 if (S.
rows() != u.size())
688 throwLengthError(WHERE_AM_I +
" " + str(S.
rows())
689 +
" != " + str(u.size()));
692 S.val(it) += u[S.idx1(it)] * v[S.idx2(it)];
712 :
MatrixBase(), valid_(false), stype_(0), rows_(0), cols_(0){ }
717 colPtr_(S.vecColPtr()),
718 rowIdx_(S.vecRowIdx()),
719 vals_(S.vecVals()), valid_(true), stype_(0){
730 const IndexArray & rowIdx,
731 const Vector < ValueType > vals,
int stype=0)
733 colPtr_ = std::vector < int >(colPtr.size());
734 rowIdx_ = std::vector < int >(rowIdx.size());
735 for (Index i = 0; i < colPtr_.size(); i ++ ) colPtr_[i] = colPtr[i];
736 for (Index i = 0; i < rowIdx_.size(); i ++ ) rowIdx_[i] = rowIdx[i];
740 cols_ = max(rowIdx_) + 1;
741 rows_ = colPtr_.size() - 1;
745 const std::vector < int > & rowIdx,
746 const Vector < ValueType > vals,
int stype=0)
753 cols_ = max(rowIdx_) + 1;
754 rows_ = colPtr_.size() - 1;
763 colPtr_ = S.vecColPtr();
764 rowIdx_ = S.vecRowIdx();
779 #define DEFINE_SPARSEMATRIX_UNARY_MOD_OPERATOR__(OP, FUNCT) \
780 void FUNCT(int i, int j, ValueType val){ \
781 if ((stype_ < 0 && i > j) || (stype_ > 0 && i < j)) return; \
782 if (abs(val) > TOLERANCE || 1){ \
783 for (int k = colPtr_[i]; k < colPtr_[i + 1]; k ++){ \
784 if (rowIdx_[k] == j) { \
785 vals_[k] OP##= val; return; \
788 std::cerr << WHERE_AM_I << " pos " << i << " " << j << " is not part of the sparsity pattern " << std::endl; \
791 SparseMatrix< ValueType > & operator OP##= (const ValueType & v){\
796 DEFINE_SPARSEMATRIX_UNARY_MOD_OPERATOR__(+, addVal)
797 DEFINE_SPARSEMATRIX_UNARY_MOD_OPERATOR__(-, subVal)
798 DEFINE_SPARSEMATRIX_UNARY_MOD_OPERATOR__(*, mulVal)
799 DEFINE_SPARSEMATRIX_UNARY_MOD_OPERATOR__(/, divVal)
801 #undef DEFINE_SPARSEMATRIX_UNARY_MOD_OPERATOR__
804 vals_ += A.vecVals();
808 vals_ -= A.vecVals();
813 if (!valid_) SPARSE_NOT_VALID;
814 for (Index i = 0, imax = A.size(); i < imax; i++){
815 for (Index j = 0, jmax = A.size(); j < jmax; j++){
816 addVal(A.idx(i), A.idx(j), A.getVal(i, j));
822 virtual uint
rtti()
const {
return GIMLI_SPARSE_CRS_MATRIX_RTTI; }
825 virtual Vector < ValueType >
mult(
const Vector < ValueType > & a)
const {
826 if (a.size() < this->cols()){
827 throwLengthError(WHERE_AM_I +
" SparseMatrix size(): " + str(this->
cols()) +
" a.size(): " +
831 Vector < ValueType > ret(this->
rows(), 0.0);
835 for (Index i = 0; i < this->
rows(); i++){
837 for (
int j = this->vecColPtr()[i]; j < this->vecColPtr()[i + 1]; j ++){
838 ret[i] += a[this->vecRowIdx()[j]] * this->vecVals()[j];
841 }
else if (stype_ == -1){
843 for (Index i = 0; i < ret.size(); i++){
844 for (
int j = this->vecColPtr()[i]; j < this->vecColPtr()[i + 1]; j ++){
845 J = this->vecRowIdx()[j];
848 ret[i] += a[J] * conj(this->vecVals()[j]);
852 ret[J] += a[i] * this->vecVals()[j];
858 }
else if (stype_ == 1){
860 for (Index i = 0; i < ret.size(); i++){
861 for (
int j = this->vecColPtr()[i]; j < this->vecColPtr()[i + 1]; j ++){
862 J = this->vecRowIdx()[j];
865 ret[i] += a[J] * conj(this->vecVals()[j]);
869 ret[J] += a[i] * this->vecVals()[j];
878 virtual Vector < ValueType >
transMult(
const Vector < ValueType > & a)
const {
880 if (a.size() < this->rows()){
881 throwLengthError(WHERE_AM_I +
" SparseMatrix size(): " + str(this->
rows()) +
" a.size(): " +
885 Vector < ValueType > ret(this->
cols(), 0.0);
888 for (Index i = 0; i < this->
rows(); i++){
889 for (
int j = this->vecColPtr()[i]; j < this->vecColPtr()[i + 1]; j ++){
890 ret[this->vecRowIdx()[j]] += a[i] * this->vecVals()[j];
894 }
else if (stype_ == -1){
896 }
else if (stype_ == 1){
903 return add(A, ValueType(1.0));
905 void add(
const ElementMatrix< double > & A,
907 void add(
const ElementMatrix< double > & A,
909 void add(
const ElementMatrix< double > & A,
910 const Matrix < ValueType > & scale);
912 void clean(){
for (Index i = 0, imax = nVals(); i < imax; i++) vals_[i] = (ValueType)(0); }
923 void setVal(
int i,
int j, ValueType val){
924 if (abs(val) > TOLERANCE || 1){
925 for (
int k = colPtr_[i]; k < colPtr_[i + 1]; k ++){
926 if (rowIdx_[k] == j) {
927 vals_[k] = val;
return;
930 std::cerr << WHERE_AM_I <<
" pos " << i <<
" " << j <<
" is not part of the sparsity pattern " << std::endl;
937 ValueType
getVal(
int i,
int j,
bool warn=
true)
const {
938 for (
int k = colPtr_[i]; k < colPtr_[i + 1]; k ++){
939 if (rowIdx_[k] == j) {
943 if (warn) std::cerr << WHERE_AM_I <<
" pos " << i <<
" "
944 << j <<
" is not part of the sparsity pattern " << std::endl;
948 void cleanRow(
int row){
949 ASSERT_RANGE(row, 0, (
int)this->
rows())
950 for (
int col = colPtr_[row]; col < colPtr_[row + 1]; col ++){
951 vals_[col] = ValueType(0);
955 void cleanCol(
int col){
956 ASSERT_RANGE(col, 0, (
int)this->
cols())
957 for (
int i = 0; i < (
int)this->rowIdx_.size(); i++){
958 if (rowIdx_[i] == col) {
959 vals_[i] = ValueType(0);
966 void buildSparsityPattern(
const Mesh & mesh){
969 colPtr_.resize(mesh.nodeCount() + 1);
974 Index col = 0, row = 0;
983 std::vector < std::set< Index > > idxMap(mesh.nodeCount());
988 for (uint c = 0; c < mesh.cellCount(); c ++){
989 cell = &mesh.cell(c);
990 nc = cell->nodeCount();
992 for (uint i = 0; i < nc; i ++){
993 for (uint j = 0; j < nc; j ++){
994 row = cell->node(i).
id();
995 col = cell->node(j).
id();
997 idxMap[col].insert(row);
1004 for (std::vector < std::set< Index > >::iterator mIt = idxMap.begin();
1005 mIt != idxMap.end(); mIt++){
1007 nVals += (*mIt).size();
1013 rowIdx_.reserve(nVals);
1014 rowIdx_.resize(nVals);
1015 vals_.resize(nVals);
1020 for (std::vector < std::set< Index > >::iterator mIt = idxMap.begin(); mIt != idxMap.end(); mIt++){
1021 for (std::set< Index >::iterator sIt = (*mIt).begin(); sIt != (*mIt).end(); sIt++){
1022 rowIdx_[k] = (*sIt);
1023 vals_[k] = (ValueType)0.0;
1031 rows_ = colPtr_.size() - 1;
1032 cols_ = max(rowIdx_) + 1;
1036 void fillStiffnessMatrix(
const Mesh & mesh){
1037 RVector a(mesh.cellCount(), 1.0);
1038 fillStiffnessMatrix(mesh, a);
1041 void fillStiffnessMatrix(
const Mesh & mesh,
const RVector & a){
1043 buildSparsityPattern(mesh);
1044 ElementMatrix < double > A_l;
1046 for (uint i = 0; i < mesh.cellCount(); i ++){
1047 A_l.ux2uy2uz2(mesh.cell(i));
1048 A_l *= a[mesh.cell(i).id()];
1052 void fillMassMatrix(
const Mesh & mesh){
1053 RVector a(mesh.cellCount(), 1.0);
1054 fillMassMatrix(mesh, a);
1057 void fillMassMatrix(
const Mesh & mesh,
const RVector & a){
1059 buildSparsityPattern(mesh);
1060 ElementMatrix < double > A_l;
1062 for (uint i = 0; i < mesh.cellCount(); i ++){
1063 A_l.u2(mesh.cell(i));
1064 A_l *= a[mesh.cell(i).id()];
1070 inline int stype()
const {
return stype_;}
1072 inline int * colPtr() {
if (valid_)
return &colPtr_[0];
else SPARSE_NOT_VALID;
return 0; }
1073 inline const int & colPtr()
const {
if (valid_)
return colPtr_[0];
else SPARSE_NOT_VALID;
return colPtr_[0]; }
1074 inline const std::vector < int > & vecColPtr()
const {
return colPtr_; }
1076 inline int * rowIdx() {
if (valid_)
return &rowIdx_[0];
else SPARSE_NOT_VALID;
return 0; }
1077 inline const int & rowIdx()
const {
if (valid_)
return rowIdx_[0];
else SPARSE_NOT_VALID;
return rowIdx_[0]; }
1078 inline const std::vector < int > & vecRowIdx()
const {
return rowIdx_; }
1080 inline ValueType * vals() {
if (valid_)
return &vals_[0];
else SPARSE_NOT_VALID;
return 0; }
1083 inline const Vector < ValueType > & vecVals()
const {
return vals_; }
1084 inline Vector < ValueType > & vecVals() {
return vals_; }
1086 inline Index size()
const {
return rows(); }
1087 inline Index nVals()
const {
return vals_.size(); }
1088 inline Index
cols()
const {
return cols_; }
1089 inline Index
rows()
const {
return rows_; }
1090 inline Index nCols()
const {
return cols(); }
1091 inline Index nRows()
const {
return rows(); }
1093 void save(
const std::string & fileName)
const {
1094 if (!valid_) SPARSE_NOT_VALID;
1096 openOutFile(fileName, & file);
1098 file.setf(std::ios::scientific, std::ios::floatfield);
1101 for (Index i = 0; i < size(); i++){
1102 for (SIndex j = colPtr_[i]; j < colPtr_[i + 1]; j ++){
1103 file << i <<
"\t" << rowIdx_[j]
1104 <<
"\t" << vals_[j] << std::endl;
1110 bool valid()
const {
return valid_; }
1116 std::vector < int > colPtr_;
1117 std::vector < int > rowIdx_;
1118 Vector < ValueType > vals_;
1126template <
class ValueType >
1133template <
class ValueType >
1140template <
class ValueType >
1141SparseMatrix < ValueType > operator * (
const SparseMatrix < ValueType > & A,
1142 const ValueType & b){
1147template <
class ValueType >
1148SparseMatrix < ValueType > operator * (
const ValueType & b,
1149 const SparseMatrix < ValueType > & A){
1154inline RVector operator * (
const RSparseMatrix & A,
const RVector & b){
return A.mult(b);}
1155inline RVector transMult(
const RSparseMatrix & A,
const RVector & b){
return A.transMult(b);}
1157inline CVector operator * (
const CSparseMatrix & A,
const CVector & b){
return A.mult(b);}
1158inline CVector operator * (
const CSparseMatrix & A,
const RVector & b){
return A.mult(toComplex(b));}
1159inline CVector transMult(
const CSparseMatrix & A,
const CVector & b){
return A.transMult(b);}
1160inline CVector transMult(
const CSparseMatrix & A,
const RVector & b){
return A.transMult(toComplex(b));}
1162inline CSparseMatrix operator + (
const CSparseMatrix & A,
const RSparseMatrix & B){
1163 CSparseMatrix ret(A);
1164 ret.vecVals() += toComplex(B.vecVals());
1168inline RSparseMatrix real(
const CSparseMatrix & A){
1169 return RSparseMatrix(A.vecColPtr(), A.vecRowIdx(),
1170 real(A.vecVals()), A.stype());
1172inline RSparseMatrix imag(
const CSparseMatrix & A){
1173 return RSparseMatrix(A.vecColPtr(), A.vecRowIdx(),
1174 imag(A.vecVals()), A.stype());
1178template<
typename ValueType >
1180template<
typename ValueType >
1186template<
typename ValueType,
typename Index >
1188template<
typename ValueType,
typename Index >
1194 add(
const ElementMatrix < double > & A,
double scale);
1196 add(
const ElementMatrix < double > & A,
const Pos & scale);
1198 add(
const ElementMatrix < double > & A,
const RMatrix & scale);
1200 add(
const ElementMatrix < double > & A,
const Vector < double > & scale);
1203 add(
const ElementMatrix < double > & A, Complex scale);
1205 add(
const ElementMatrix < double > & A,
const Pos & scale);
1207 add(
const ElementMatrix < double > & A,
const Matrix < Complex > & scale);
1209 add(
const ElementMatrix < double > & A,
const Vector < Complex > & scale);
1212template <> DLLEXPORT
void SparseMapMatrix< double, Index >::
1213 addToCol(Index
id,
const ElementMatrix < double > & A,
double scale,
bool isDiag);
1214template <> DLLEXPORT
void SparseMapMatrix< double, Index >::
1215 addToRow(Index
id,
const ElementMatrix < double > & A,
double scale,
bool isDiag);
1216template <> DLLEXPORT
void SparseMapMatrix< Complex, Index >::
1217 addToCol(Index
id,
const ElementMatrix < double > & A, Complex scale,
bool isDiag);
1218template <> DLLEXPORT
void SparseMapMatrix< Complex, Index >::
1219 addToRow(Index
id,
const ElementMatrix < double > & A, Complex scale,
bool isDiag);
1224template <> DLLEXPORT
void SparseMatrix< double >::
1225 add(
const ElementMatrix < double > & A,
double scale);
1226template <> DLLEXPORT
void SparseMatrix< double >::
1227 add(
const ElementMatrix < double > & A,
const Pos & scale);
1228template <> DLLEXPORT
void SparseMatrix< double >::
1229 add(
const ElementMatrix < double > & A,
const RMatrix & scale);
1231template <> DLLEXPORT
void SparseMatrix< Complex >::
1232 add(
const ElementMatrix < double > & A, Complex scale);
1233template <> DLLEXPORT
void SparseMatrix< Complex >::
1234 add(
const ElementMatrix < double > & A,
const Pos & scale);
1235template <> DLLEXPORT
void SparseMatrix< Complex >::
1236 add(
const ElementMatrix < double > & A,
const CMatrix & scale);
int id() const
Definition baseentity.h:59
A abstract cell.
Definition meshentities.h:261
Interface class for matrices.
Definition matrix.h:137
MatrixBase(bool verbose=false)
Definition matrix.h:141
based on: Ulrich Breymann, Addison Wesley Longman 2000 , revised edition ISBN 0-201-67488-2,...
Definition sparsematrix.h:45
Simple row-based dense matrix based on Vector.
Definition matrix.h:324
3 dimensional vector
Definition pos.h:73
Definition sparsematrix.h:333
based on: Ulrich Breymann, Addison Wesley Longman 2000 , revised edition ISBN 0-201-67488-2,...
Definition sparsematrix.h:149
SparseMapMatrix(const IndexArray &i, const IndexArray &j, const RVector &v)
Definition sparsematrix.h:185
virtual Vector< ValueType > transMult(const Vector< ValueType > &a) const
Definition sparsematrix.h:455
void add(const ElementMatrix< double > &A, ValueType scale=1.0)
virtual IndexType rows() const
Definition sparsematrix.h:268
SparseMapMatrix(IndexType r=0, IndexType c=0, int stype=0)
Definition sparsematrix.h:158
void add(const ElementMatrix< double > &A, const Vector< ValueType > &scale)
void resize(Index rows, Index cols)
Definition sparsematrix.h:217
void add(const IndexArray &rows, const IndexArray &cols, const RVector &vals)
Definition sparsematrix.h:226
int stype() const
Definition sparsematrix.h:265
void fillArrays(Vector< ValueType > &vals, IndexArray &rows, IndexArray &cols)
Definition sparsematrix.h:551
void importCol(const std::string &filename, double dropTol, Index colOffset)
Definition sparsematrix.h:517
virtual Vector< ValueType > mult(const Vector< ValueType > &a) const
Definition sparsematrix.h:417
virtual IndexType cols() const
Definition sparsematrix.h:272
virtual void clear()
Definition sparsematrix.h:236
virtual uint rtti() const
Definition sparsematrix.h:215
void save(const std::string &filename) const
Definition sparsematrix.h:486
virtual ~SparseMatrix()
Definition sparsematrix.h:758
int stype() const
Definition sparsematrix.h:1070
virtual Vector< ValueType > mult(const Vector< ValueType > &a) const
Definition sparsematrix.h:825
void clear()
Definition sparsematrix.h:914
void copy_(const SparseMapMatrix< double, Index > &S)
Definition sparsematrix.h:1179
SparseMatrix(const SparseMatrix< ValueType > &S)
Definition sparsematrix.h:715
SparseMatrix()
Definition sparsematrix.h:711
void save(const std::string &fileName) const
Definition sparsematrix.h:1093
SparseMatrix(const SparseMapMatrix< ValueType, Index > &S)
Definition sparsematrix.h:725
SparseMatrix< ValueType > & operator=(const SparseMatrix< ValueType > &S)
Definition sparsematrix.h:761
virtual uint rtti() const
Definition sparsematrix.h:822
Index rows() const
Definition sparsematrix.h:1089
ValueType getVal(int i, int j, bool warn=true) const
Definition sparsematrix.h:937
void clean()
Definition sparsematrix.h:912
Index cols() const
Definition sparsematrix.h:1088
virtual Vector< ValueType > transMult(const Vector< ValueType > &a) const
Definition sparsematrix.h:878
Definition stopwatch.h:62
void resize(Index n, ValueType fill)
Definition vector.h:698
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
RVector x(const R3Vector &rv)
Definition pos.cpp:107
bool load(Matrix< ValueType > &A, const std::string &filename)
Definition matrix.h:828