KASKADE 7 development version
triplet.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2024 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef TRIPLET_HH
14#define TRIPLET_HH
15
16#include <algorithm>
17#include <cmath>
18#include <fstream>
19#include <iostream>
20#include <vector>
21#include <memory>
22#include <utility>
23
24#include "dune/common/fmatrix.hh"
25#include "dune/istl/bcrsmatrix.hh"
26
27#include "linalg/crsutil.hh"
29
30namespace Kaskade
31{
33 // forward declaration
34 template <class Entry, class Index>
35 class NumaBCRSMatrix;
37
55 template<class Scalar,class SparseIndexInt=int>
57 {
58 public:
60 typedef Scalar value_type;
61 typedef typename std::vector<Scalar>::iterator iterator;
62 typedef typename std::vector<Scalar>::const_iterator const_iterator;
63 typedef typename std::vector<SparseIndexInt>::iterator index_iterator;
64 typedef typename std::vector<SparseIndexInt>::const_iterator const_index_iterator;
65
68
70 explicit MatrixAsTriplet(size_t s) : ridx(s), cidx(s), data(s){}
71
73 template <int n, int m, class Allocator>
74 explicit MatrixAsTriplet(Dune::BCRSMatrix<Dune::FieldMatrix<Scalar,n,m>,Allocator> const& other)
75 {
76 fillFromBCRSInterface<n,m>(other);
77 }
78
80 template <int n, int m, class Index>
82 {
83 fillFromBCRSInterface<n,m>(other);
84 }
85
94 MatrixAsTriplet(std::vector<SparseIndexInt>&& row, std::vector<SparseIndexInt>&& col,
95 std::vector<Scalar>&& value)
96 {
97 assert(row.size()==col.size() && row.size()==value.size());
98 assert(*std::min_element(row.begin(),row.end()) >=0);
99 assert(*std::min_element(col.begin(),col.end()) >=0);
100
101 ridx = std::move(row);
102 cidx = std::move(col);
103 data = std::move(value);
104 }
105
107 MatrixAsTriplet(SparseIndexInt nrows, SparseIndexInt ncols, Scalar const& value)
108 {
109 if(value != 0.0)
110 {
112 resize(0);
113 for(size_t i=0; i<nrows; ++i)
114 for(size_t k=0; k<ncols; ++k)
115 {
116 ridx.push_back(i);
117 cidx.push_back(k);
118 data.push_back(value);
119 }
120 }
121 }
122
124 MatrixAsTriplet(SparseIndexInt nrows, SparseIndexInt ncols, SparseIndexInt shift, Scalar const& value)
125 {
126 if(value != 0.0)
127 {
128 resize(0);
129 for(size_t i=0; i<ncols; ++i)
130 {
131 size_t rowidx=i+shift;
132 if(rowidx >= 0 && rowidx < nrows)
133 {
134 ridx.push_back(rowidx);
135 cidx.push_back(i);
136 data.push_back(value);
137 }
138 }
139 }
140 }
141
143 MatrixAsTriplet(SparseIndexInt nrows, SparseIndexInt ncols, SparseIndexInt shift, std::vector<Scalar> const& value)
144 {
145 resize(0);
146 for(int i=0; i<ncols; ++i)
147 {
148 int rowidx=i+shift;
149 if(rowidx >= 0 && rowidx < nrows)
150 {
151 ridx.push_back(rowidx);
152 cidx.push_back(i);
153 data.push_back(value[i]);
154 }
155 }
156 }
157
159 MatrixAsTriplet(MatrixAsTriplet const& other) = default;
160
163
167 template <class OtherScalar, class OtherIndex>
169 : ridx(A.ridx.size())
170 , cidx(A.cidx.size())
171 , data(A.data.size())
172 {
173 std::copy(A.ridx.begin(),A.ridx.end(),ridx.begin());
174 std::copy(A.cidx.begin(),A.cidx.end(),cidx.begin());
175 std::copy(A.data.begin(),A.data.end(),data.begin());
176 }
177
178
182 void clear()
183 {
184 cidx.clear();
185 ridx.clear();
186 data.clear();
187 }
188
189
194 {
195 SparseIndexInt r0 = *std::min_element(ridx.begin(),ridx.end());
196 SparseIndexInt c0 = *std::min_element(cidx.begin(),cidx.end());
197 if(r0!=0 || c0!=0)
198 shiftIndices(-r0,-c0);
199 }
200
202 SparseIndexInt nrows() const
203 {
204 if(ridx.size()==0) return 0;
205 return *std::max_element(ridx.begin(),ridx.end())+1;
206 }
207
212 SparseIndexInt N() const { return nrows(); }
213
215 SparseIndexInt ncols() const
216 {
217 if(cidx.size()==0) return 0;
218 return *std::max_element(cidx.begin(),cidx.end())+1;
219 }
220
225 SparseIndexInt M() const { return ncols(); }
226
234 void resize(size_t s)
235 {
236 ridx.resize(s);
237 cidx.resize(s);
238 data.resize(s);
239 }
240
242 void reserve(size_t s)
243 {
244 ridx.reserve(s);
245 cidx.reserve(s);
246 data.reserve(s);
247 }
248
254 size_t nnz() const { return ridx.size(); }
255
257
261 template <class X, class Y>
262 void axpy(Y& out, X const& in, Scalar alpha=1.0,int nvectors=1) const
263 {
264 assert(ridx.size()==cidx.size() && ridx.size()==data.size());
265 assert(in.size() >= ncols()*nvectors);
266 assert(out.size() >= nrows()*nvectors);
267 SparseIndexInt cols=ncols();
268 SparseIndexInt rows=nrows();
269 for(int j=0; j<nvectors; ++j)
270 for(size_t i=0; i<ridx.size(); ++i)
271 {
272 assert(ridx[i]+j*rows < out.size());
273 assert(cidx[i]+j*cols < in.size());
274 out[ridx[i]+j*rows] += alpha*in[cidx[i]+j*cols]*data[i];
275 }
276 }
277
279
282 template <class X, class Y>
283 void atxpy(Y& y, X const& x, Scalar alpha=1.0) const
284 {
285 assert(ridx.size()==cidx.size() && ridx.size()==data.size());
286 assert(y.size() == ncols());
287 assert(x.size() == nrows());
288 for(size_t i=0; i<data.size(); ++i) y[cidx[i]] += alpha*data[i]*x[ridx[i]];
289 }
290
292
295 template <class X, class Y>
296 void usmtv(Scalar const alpha, X const& x, Y& y) const
297 {
298 atxpy(y,x,alpha);
299 }
300
301
303 template <class X, class Y>
304 void usmv(Scalar const alpha, X const& x, Y& y) const
305 {
306 axpy(y, x, alpha);
307 }
308
309
311 template <class X, class Y>
312 void ax(Y& out, X const& in) const
313 {
314 assert(ridx.size()==cidx.size() && ridx.size()==data.size());
315 assert(in.size() >= ncols());
316 out.resize(0);
317 out.resize(nrows(),0.0);
318 for(size_t i=0; i<ridx.size(); ++i)
319 {
320 assert(ridx[i] < out.size());
321 assert(cidx[i] < in.size());
322 out[ridx[i]] += in[cidx[i]]*data[i];
323 }
324 }
325
327 template <class X, class Y>
328 void mv(X const& x, Y& y) const
329 {
330 axpy(y,x);
331 }
332
334 void shiftIndices(SparseIndexInt row, SparseIndexInt col)
335 {
336 assert(ridx.size()==cidx.size() && ridx.size()==data.size());
337 for(size_t i=0; i< ridx.size(); ++i)
338 {
339 ridx[i] += row;
340 cidx[i] += col;
341 assert(ridx[i] >= 0 && cidx[i] >= 0);
342 }
343 }
344
349 void addEntry(SparseIndexInt row, SparseIndexInt col, Scalar const& value)
350 {
351 ridx.push_back(row);
352 cidx.push_back(col);
353 data.push_back(value);
354 }
355
358 {
359 if(ridx.size()==0)
360 {
361 ridx=m.ridx;
362 cidx=m.cidx;
363 data=m.data;
364 return *this;
365 }
366 ridx.insert(ridx.end(),m.ridx.begin(),m.ridx.end());
367 cidx.insert(cidx.end(),m.cidx.begin(),m.cidx.end());
368 data.insert(data.end(),m.data.begin(),m.data.end());
369
370 // conversion to compresses row and back to triplet to remove multiple entries
371
372 std::vector<SparseIndexInt> Ap, Ai;
373 std::vector<Scalar> Az;
374 umfpack_triplet_to_col(nrows(), ncols(), ridx, cidx, data, Ap, Ai, Az);
375 {
376 std::vector<SparseIndexInt> nullInt1, nullInt2;
377 std::vector<Scalar> nullDouble;
378 cidx.swap(nullInt1);
379 ridx.swap(nullInt2);
380 data.swap(nullDouble);
381 }
383 data.insert(data.begin(),&Az[0],&Az[Ap.back()]);
384 ridx.insert(ridx.begin(),&Ai[0],&Ai[Ap.back()]);
385
386 return *this;
387 }
388
390 {
391 if(ridx.size()==0)
392 {
393 ridx=m.ridx;
394 cidx=m.cidx;
395 data=m.data;
396 return *this;
397 }
398 int s=data.size();
399 ridx.insert(ridx.end(),m.ridx.begin(),m.ridx.end());
400 cidx.insert(cidx.end(),m.cidx.begin(),m.cidx.end());
401 data.insert(data.end(),m.data.begin(),m.data.end());
402
403 for(int i=s; i < data.size(); ++i)
404 data[i] *= -1.0;
405 // conversion to compresses row and back to triplet to remove multiple entries
406
407 std::vector<SparseIndexInt> Ap, Ai;
408 std::vector<Scalar> Az;
409 umfpack_triplet_to_col(nrows(), ncols(), ridx, cidx, data, Ap, Ai, Az);
410 {
411 std::vector<SparseIndexInt> nullInt1, nullInt2;
412 std::vector<Scalar> nullDouble;
413 cidx.swap(nullInt1);
414 ridx.swap(nullInt2);
415 data.swap(nullDouble);
416 }
418 data.insert(data.begin(),&Az[0],&Az[Ap.back()]);
419 ridx.insert(ridx.begin(),&Ai[0],&Ai[Ap.back()]);
420
421 return *this;
422 }
423
424
426 MatrixAsTriplet& operator*=(Scalar const& scalar)
427 {
428 for(auto& v: data)
429 v *= scalar;
430
431 return *this;
432 }
433
435 MatrixAsTriplet& operator/=(Scalar const& scalar)
436 {
437 return (*this) *= 1/scalar;
438 }
439
442
445
446 void scaleRows(std::vector<Scalar>const& scaling)
447 {
448 for(size_t i=0; i<ridx.size();++i)
449 data[i] *= scaling[ridx[i]];
450 }
451
454 {
455 // simple: just exchange row and column indices.
456 std::swap(ridx,cidx);
457 return *this;
458 }
459
462 {
463 size_t count(0);
464 for(size_t i=0; i<ridx.size(); ++i)
465 {
466 if(ridx[i] <= cidx[i]) count++;
467 }
468 std::vector<SparseIndexInt> r2,c2;
469 std::vector<Scalar> d2;
470 r2.reserve(count);
471 c2.reserve(count);
472 d2.reserve(count);
473 for(size_t i=0; i<ridx.size(); ++i)
474 {
475 if(ridx[i] <= cidx[i])
476 {
477 r2.push_back(ridx[i]);
478 c2.push_back(cidx[i]);
479 d2.push_back(data[i]);
480 }
481 }
482 std::swap(r2,ridx);
483 std::swap(c2,cidx);
484 std::swap(d2,data);
485 }
486
490 std::ostream& print(std::ostream& out = std::cout, double threshold=0.0) const
491 {
492 out << "[" << std::endl;
493 for(size_t i=0; i<ridx.size(); ++i) if(std::fabs(data[i]) > threshold)
494 out << ridx[i] << "," << cidx[i] << "," << data[i] << ";" << std::endl;
495 out << "]" << std::endl;
496 return out;
497 }
498
500 void toColumns(std::vector<std::vector<Scalar> >& colsvector) const
501 {
502 SparseIndexInt rows=nrows();
503 colsvector.resize(ncols());
504 for(size_t i=0; i<colsvector.size();++i)
505 {
506 colsvector[i].resize(0);
507 colsvector[i].resize(rows,0.0);
508 }
509 for(size_t i=0; i<data.size(); ++i)
510 colsvector[cidx[i]][ridx[i]]=data[i];
511 }
512
513 void toRows(std::vector<std::vector<Scalar> >& rows) const
514 {
515 SparseIndexInt cols = ncols();
516 rows.resize(nrows());
517 std::for_each(rows.begin(),rows.end(),
518 [&cols](std::vector<Scalar>& row)
519 {
520 row.resize(0);
521 row.resize(cols,0.0);
522 });
523 for(size_t i=0; i<data.size(); ++i) rows[ridx[i]][cidx[i]] = data[i];
524 }
525
526 void toVector(std::vector<Scalar>& colsvector)
527 {
528 SparseIndexInt rows=nrows();
529 colsvector.resize(0);
530 colsvector.resize(nrows()*ncols(),0.0);
531 for(size_t i=0; i<data.size(); ++i)
532 colsvector[cidx[i]*rows+ridx[i]]=data[i];
533 }
535
536 void addColumn(std::vector<Scalar>& colsvector, size_t position)
537 {
538 for(size_t i=0; i<colsvector.size();++i)
539 {
540 ridx.push_back(i);
541 cidx.push_back(position);
542 data.push_back(colsvector[i]);
543 }
544 }
545
546
548 template<class Mat>
549 void addToMatrix(Mat& mat)
550 {
551 for(size_t i=0; i<data.size(); ++i)
552 mat[ridx[i]][cidx[i]] += data[i];
553 }
554
555 void erase(SparseIndexInt i)
556 {
557 ridx.erase(std::begin(ridx) + i);
558 cidx.erase(std::begin(cidx) + i);
559 data.erase(std::begin(data) + i);
560 }
561
563 {
564 SparseIndexInt noRows = nrows(), nnz = data.size();
565 std::vector<Scalar> diagonalEntries(noRows,0);
566
567 // sum up row entries
568 for(SparseIndexInt row=0; row<noRows; ++row)
569 {
570 SparseIndexInt i=0;
571 while(i<data.size())
572 {
573 if(ridx[i] == row) diagonalEntries[row] += data[i];
574 else ++i;
575 }
576 }
577
578 assert(nrows()==0 && ncols()==0); // matrix should be empty now
579
581 for(SparseIndexInt i=0; i<noRows; ++i) result.addEntry(i,i,diagonalEntries[i]);
582 return result;
583 }
584
586 iterator begin(){ return data.begin(); }
587
589 const_iterator begin() const { return data.begin(); }
590
592 iterator end() { return data.end(); }
593
595 const_iterator end() const { return data.end(); }
596
598 size_t size() const { return data.size(); }
599
601 void toFile(const char* filename, unsigned int precision=10) const
602 {
603 std::ofstream myfile;
604 myfile.precision(precision);
605 myfile.open(filename);
606 for(size_t i=0; i<data.size(); ++i)
607 myfile << ridx[i] << " " << cidx[i] << " " << data[i] << std::endl;
608 myfile.close();
609 }
610
612 template <int bcrsN=1>
613 std::unique_ptr<Dune::BCRSMatrix<Dune::FieldMatrix<Scalar,bcrsN,bcrsN>>> toBCRS() const
614 {
615 typedef Dune::BCRSMatrix<Dune::FieldMatrix<Scalar,bcrsN,bcrsN> > BCRSMat;
616
617 // get column indices for each row
618 SparseIndexInt noRows = nrows()/bcrsN,
619 noCols = ncols()/bcrsN;
620 std::vector<std::vector<SparseIndexInt> > bcrsids(noRows);
621 getBCRSIndices<bcrsN>(noRows, ridx, cidx, bcrsids);
622
623 SparseIndexInt nonz = 0;
624 for (auto const& s : bcrsids)
625 nonz += s.size();
626 std::unique_ptr<BCRSMat> result(new BCRSMat(noRows,noCols,nonz,BCRSMat::row_wise));
627
628 // create sparsity pattern
629 for (typename BCRSMat::CreateIterator row=result->createbegin(); row!=result->createend(); ++row)
630 for(SparseIndexInt col = 0; col < bcrsids[row.index()].size(); ++col) row.insert(bcrsids[row.index()][col]);
631
632 // fill matrix
633 for (SparseIndexInt i=0; i<data.size(); ++i)
634 {
635 size_t blockRow = ridx[i]/bcrsN, blockCol = cidx[i]/bcrsN;
636 (*result)[blockRow][blockCol][ridx[i]-bcrsN*blockRow][cidx[i]-bcrsN*blockCol] = data[i];
637 }
638
639 return result;
640 }
641
642 bool isSymmetric() const
643 {
644 for(size_t i=0; i<data.size(); ++i)
645 {
646 // check if symmetric entry exists
647 auto tmp = findEntry(cidx[i],ridx[i]);
648 if(tmp.second == false || std::fabs(data[i]-data[tmp.first]) > 1e-6)
649 return false;
650 }
651
652 return true;
653 }
654
658 std::pair<size_t,bool> findEntry(SparseIndexInt row, SparseIndexInt col) const
659 {
660 for(size_t i=0; i<data.size(); ++i)
661 if(ridx[i]==row && cidx[i]==col)
662 return std::make_pair(i,true);
663
664 return std::make_pair(0,false);
665 }
666
667
669 std::vector<SparseIndexInt> ridx;
671 std::vector<SparseIndexInt> cidx;
673 std::vector<Scalar> data;
674
675 private:
676 template <int bcrsN, class SparseInt>
677 void getBCRSIndices(SparseInt nrows, std::vector<SparseInt> const& ridx, std::vector<SparseInt> const& cidx, std::vector<std::vector<SparseInt> >& rowColumnIndices) const
678 {
679 rowColumnIndices.clear();
680 rowColumnIndices.resize(nrows);
681 for(SparseInt i=0; i<ridx.size(); ++i) rowColumnIndices[ridx[i]/bcrsN].push_back(cidx[i]/bcrsN);
682
683 for(std::vector<SparseInt>& s : rowColumnIndices)
684 {
685 std::sort(s.begin(),s.end());
686 auto last = std::unique(s.begin(),s.end());
687 s.erase(last,s.end());
688 }
689 }
690
691 template <int n, int m, class Matrix>
692 void fillFromBCRSInterface(Matrix const& other)
693 {
694 reserve(other.nonzeroes()*n*m);
695
696 auto rend = other.end();
697 for (auto r=other.begin(); r!=rend; ++r) // iterate over rows
698 {
699 SparseIndexInt ri = r.index() * n; // block row start index in matrix with scalar entries
700 auto cend = r->end();
701 for (auto c=r->begin(); c!=cend; ++c) // iterate over columns
702 {
703 SparseIndexInt ci = c.index() * m; // block col start index in matrix with scalar entries
704 auto const& entry = *c;
705
706 for(size_t k=0; k<n; ++k) // iterate over block entries
707 for(size_t l=0; l<m; ++l)
708 {
709 ridx.push_back(ri+k);
710 cidx.push_back(ci+l);
711 data.push_back(entry[k][l]);
712 }
713 }
714 }
715 }
716
717 template <class Iterator>
718 struct Read
719 {
720 Read(Iterator& in_): in(in_) {}
721
722 template <class Element>
723 void operator()(Element& e) const { in = vectorFromSequence(e,in); }
724
725 private:
726 Iterator& in;
727 };
728
729 template <class Iterator>
730 struct Write
731 {
732 Write(Iterator& out_): out(out_) {}
733
734 template <class Element>
735 void operator()(Element const& e) const { out = vectorToSequence(e,out); }
736
737 private:
738 Iterator& out;
739 };
740 };
741
746 template<class Scalar>
747 void deleteLowerTriangle(std::vector<int>& ridx, std::vector<int>& cidx, std::vector<Scalar>& data)
748 {
749 size_t count(0);
750 for(size_t i=0; i<ridx.size(); ++i)
751 if(ridx[i] <= cidx[i])
752 count++;
753
754 std::vector<int> r2,c2;
755 std::vector<Scalar> d2;
756 r2.reserve(count);
757 c2.reserve(count);
758 d2.reserve(count);
759
760 for(size_t i=0; i<ridx.size(); ++i)
761 if(ridx[i] <= cidx[i]) {
762 r2.push_back(ridx[i]);
763 c2.push_back(cidx[i]);
764 d2.push_back(data[i]);
765 }
766
767 std::swap(r2,ridx);
768 std::swap(c2,cidx);
769 std::swap(d2,data);
770 }
771
772 template <typename Scalar, typename SparseIndexInt>
773 std::ostream& operator<<(std::ostream &s, MatrixAsTriplet<Scalar,SparseIndexInt> const& mat)
774 {
775 return mat.print(s,0);
776 }
777
778 // ----------------------------------------------------------------------------------------------
779 // ----------------------------------------------------------------------------------------------
780
781
782} // end of namespace Kaskade
783
784#endif
void atxpy(Y &y, X const &x, Scalar alpha=1.0) const
Matrix-vector multiplication.
Definition: triplet.hh:283
size_t nnz() const
Definition: triplet.hh:254
void setStartToZero()
Shift the row and column indices such that is (structurally) nonzero.
Definition: triplet.hh:193
void toFile(const char *filename, unsigned int precision=10) const
write to text file
Definition: triplet.hh:601
void addEntry(SparseIndexInt row, SparseIndexInt col, Scalar const &value)
Adds the given value to the specified matrix entry. The entry is created in case it has been structur...
Definition: triplet.hh:349
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
Definition: triplet.hh:202
SparseIndexInt ncols() const
Returns number of cols (computes them, if not known)
Definition: triplet.hh:215
void reserve(size_t s)
Reserve memory.
Definition: triplet.hh:242
MatrixAsTriplet(MatrixAsTriplet const &other)=default
Copy constructor.
MatrixAsTriplet & operator=(MatrixAsTriplet &&other)=default
Move assignment.
void deleteLowerTriangle()
Deletes all sub-diagonal entries.
Definition: triplet.hh:461
std::vector< SparseIndexInt > ridx
row indices
Definition: triplet.hh:669
MatrixAsTriplet & operator*=(Scalar const &scalar)
Scaling.
Definition: triplet.hh:426
const_iterator end() const
get const iterator on end of data field
Definition: triplet.hh:595
const_iterator begin() const
get const iterator on data field
Definition: triplet.hh:589
MatrixAsTriplet(SparseIndexInt nrows, SparseIndexInt ncols, SparseIndexInt shift, std::vector< Scalar > const &value)
Constructs a diagonal matrix of size (nrows,ncols). The diagonal is shifted down shift rows (if shift...
Definition: triplet.hh:143
SparseIndexInt N() const
Returns number of rows (computes them, if not known) This is a Dune ISTL compatibility method.
Definition: triplet.hh:212
size_t size() const
get number of entries in sparse matrix
Definition: triplet.hh:598
std::vector< SparseIndexInt > cidx
column indices
Definition: triplet.hh:671
void toVector(std::vector< Scalar > &colsvector)
Definition: triplet.hh:526
void erase(SparseIndexInt i)
Definition: triplet.hh:555
MatrixAsTriplet(Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, n, m >, Allocator > const &other)
Constructor from a Dune::BCRSMatrix.
Definition: triplet.hh:74
MatrixAsTriplet(SparseIndexInt nrows, SparseIndexInt ncols, Scalar const &value)
Constructs a matrix of size (nrows,ncols), filled with value. If value==0.0, then it is an empty matr...
Definition: triplet.hh:107
bool isSymmetric() const
Definition: triplet.hh:642
void mv(X const &x, Y &y) const
matrix-vector multiplication
Definition: triplet.hh:328
MatrixAsTriplet & operator/=(Scalar const &scalar)
Scaling.
Definition: triplet.hh:435
void clear()
Deletes the data, leaving an empty matrix of size 0 x 0.
Definition: triplet.hh:182
std::unique_ptr< Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, bcrsN, bcrsN > > > toBCRS() const
copy to Dune::BCRSMatrix
Definition: triplet.hh:613
MatrixAsTriplet()
Constructor.
Definition: triplet.hh:67
MatrixAsTriplet & operator-=(MatrixAsTriplet const &m)
Definition: triplet.hh:389
MatrixAsTriplet(SparseIndexInt nrows, SparseIndexInt ncols, SparseIndexInt shift, Scalar const &value)
Constructs a diagonal matrix of size (nrows,ncols). The diagonal is shifted down shift rows (if shift...
Definition: triplet.hh:124
void shiftIndices(SparseIndexInt row, SparseIndexInt col)
Shifts matrix indices. Can be used together with += to concatenate sparese matrices.
Definition: triplet.hh:334
MatrixAsTriplet(std::vector< SparseIndexInt > &&row, std::vector< SparseIndexInt > &&col, std::vector< Scalar > &&value)
Move constructor taking raw data.
Definition: triplet.hh:94
SparseIndexInt M() const
Returns number of columns (computes them, if not known) This is a Dune ISTL compatibility method.
Definition: triplet.hh:225
std::ostream & print(std::ostream &out=std::cout, double threshold=0.0) const
Output as Matlab source code.
Definition: triplet.hh:490
MatrixAsTriplet(size_t s)
Constructor that allocates memory directly.
Definition: triplet.hh:70
MatrixAsTriplet(NumaBCRSMatrix< Dune::FieldMatrix< Scalar, n, m >, Index > const &other)
Constructor from a NumaBCRSMatrix.
Definition: triplet.hh:81
iterator end()
get iterator on end of data field
Definition: triplet.hh:592
void scaleRows(std::vector< Scalar >const &scaling)
Definition: triplet.hh:446
std::pair< size_t, bool > findEntry(SparseIndexInt row, SparseIndexInt col) const
Definition: triplet.hh:658
MatrixAsTriplet & transpose()
Transposition.
Definition: triplet.hh:453
void addColumn(std::vector< Scalar > &colsvector, size_t position)
add a column
Definition: triplet.hh:536
MatrixAsTriplet lumped()
Definition: triplet.hh:562
std::vector< Scalar > data
data
Definition: triplet.hh:673
void usmtv(Scalar const alpha, X const &x, Y &y) const
Matrix-vector multiplication.
Definition: triplet.hh:296
std::vector< SparseIndexInt >::const_iterator const_index_iterator
Definition: triplet.hh:64
iterator begin()
get iterator on data field
Definition: triplet.hh:586
void usmv(Scalar const alpha, X const &x, Y &y) const
scaled matrix-vector multiplication
Definition: triplet.hh:304
MatrixAsTriplet & operator=(MatrixAsTriplet const &m)=default
Assignment.
void toColumns(std::vector< std::vector< Scalar > > &colsvector) const
transform into a set of column vectors
Definition: triplet.hh:500
void axpy(Y &out, X const &in, Scalar alpha=1.0, int nvectors=1) const
Matrix-vector multiplication: out += alpha * (*this) * in.
Definition: triplet.hh:262
Scalar value_type
STL-compliant typedefs.
Definition: triplet.hh:60
MatrixAsTriplet & operator+=(MatrixAsTriplet const &m)
Matrix addition: (*this) += m (works also for matrices with non-matching sparsity pattern)
Definition: triplet.hh:357
void ax(Y &out, X const &in) const
Matrix-vector multiplication: out = (*this) * in.
Definition: triplet.hh:312
void addToMatrix(Mat &mat)
add to a dense matrix
Definition: triplet.hh:549
void resize(size_t s)
Resizes the memory.
Definition: triplet.hh:234
void toRows(std::vector< std::vector< Scalar > > &rows) const
Definition: triplet.hh:513
MatrixAsTriplet(MatrixAsTriplet &&other)=default
Move constructor.
std::vector< SparseIndexInt >::iterator index_iterator
Definition: triplet.hh:63
std::vector< Scalar >::iterator iterator
Definition: triplet.hh:61
MatrixAsTriplet(MatrixAsTriplet< OtherScalar, OtherIndex > const &A)
Copy constructor from a triplet matrix with different entry and/or index types.
Definition: triplet.hh:168
std::vector< Scalar >::const_iterator const_iterator
Definition: triplet.hh:62
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition: fixdune.hh:716
void umfpack_triplet_to_col(int rows, int cols, std::vector< long > const &ridx, std::vector< long > const &cidx, std::vector< double > const &values, std::vector< long > &Ap, std::vector< long > &Ai, std::vector< double > &Az)
Triplet to compressed column storage conversion . This is just a frontent to DirectType::UMFPACK util...
void deleteLowerTriangle(std::vector< int > &ridx, std::vector< int > &cidx, std::vector< Scalar > &data)
removes subdiagonal entries from the matrix entries stored in elementary triplet format
Definition: triplet.hh:747
void umfpack_col_to_triplet(std::vector< long > const &Ap, std::vector< long > &Ti)
Compressed column storage to triplet storage conversion. This is just a frontent to DirectType::UMFPA...
Functional::Scalar d2(Assembler &assembler, Functional const &f, typename Functional::AnsatzVars::VariableSet const &x, typename Functional::Scalar increment=1e-12, typename Functional::Scalar tolerance=1e-6, bool toFile=false, std::string const &savefilename=std::string("d2error"))
std::ostream & operator<<(std::ostream &s, std::vector< Scalar > const &vec)
Definition: dune_bridge.hh:47
OutIter vectorToSequence(double x, OutIter iter)
Definition: crsutil.hh:30
InIter vectorFromSequence(FunctionSpaceElement< Space, m > &v, InIter in)