53 int& counter,
int columns,
int width)
58 if (counter%columns==0)
69 if (counter%columns==0)
75 for (
const auto& entry : v)
97 std::ios_base::fmtflags oldflags = s.
flags();
100 s.
setf(std::ios_base::scientific, std::ios_base::floatfield);
105 s << title <<
" [blocks=" << v.N() <<
",dimension=" << v.dim() <<
"]"
112 if (counter%columns!=0)
135 for (
int j=0; j<m; j++)
157 [[maybe_unused]]
int precision)
174 typename M::size_type J,
typename M::size_type therow,
175 int width,
int precision)
177 typename M::size_type i0=I;
178 for (
typename M::size_type i=0; i<A.
N(); i++)
183 typename M::size_type j0=J;
184 for (
typename M::size_type j=0; j<A.
M(); j++)
187 typename M::ConstColIterator it = A[i].find(j);
191 print_row(s,*it,i0,j0,therow,width,precision);
214 std::string rowtext,
int width=10,
int precision=2)
218 std::ios_base::fmtflags oldflags = s.
flags();
221 s.
setf(std::ios_base::scientific, std::ios_base::floatfield);
234 for (
typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
253 const B& innerMatrixElement,
257 s<<innerMatrixElement<<
" ";
260 template<
class B,
int n>
262 const ScaledIdentityMatrix<B,n> innerMatrixElement,
263 int innerrow,
int innercol)
265 if (innerrow == innercol)
266 s<<innerMatrixElement.scalar()<<
" ";
271 template<
class B,
int n,
int m>
273 const FieldMatrix<B,n,m> innerMatrixElement,
274 int innerrow,
int innercol)
276 s<<innerMatrixElement[innerrow][innercol]<<
" ";
301 template<
class A,
class InnerMatrixType>
305 int width=3,
int precision=2)
309 std::ios_base::fmtflags oldflags = s.
flags();
311 s.
setf(std::ios_base::scientific, std::ios_base::floatfield);
328 bool reachedEnd=
false;
331 for(
int innerrow=0; innerrow<n; ++innerrow) {
334 Col
col=row->begin();
335 for(;
col != row->end(); ++
col,++count) {
338 if(count>=skipcols+width)
341 if(count==skipcols) {
345 s << row.index()<<
": ";
348 s<<
col.index()<<
": |";
350 if(count==skipcols) {
351 for(
typename std::string::size_type i=0; i < rowtext.
length(); i++)
357 for(
int innercol=0; innercol < m; ++innercol) {
359 Impl::printInnerMatrixElement(s,*
col,innerrow,innercol);
364 if(innerrow==n-1 &&
col==row->end())
383 struct MatlabPODWriter
392 struct MatlabPODWriter<
std::complex<T> >
411 template <
class FieldType,
414 int rowOffset,
int colOffset,
418 s << rowOffset + 1 <<
" " << colOffset + 1 <<
" ";
419 MatlabPODWriter<FieldType>::write(value, s)<<
std::endl;
429 template <
class MatrixType,
432 int externalRowOffset,
int externalColOffset,
437 if (colOffset.
size() > 0)
440 for (
typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
441 colOffset[i+1] = colOffset[i] +
444 typename MatrixType::size_type rowOffset = 0;
447 for (
typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
449 auto cIt = matrix[rowIdx].begin();
450 auto cEndIt = matrix[rowIdx].end();
453 for (; cIt!=cEndIt; ++cIt)
455 externalRowOffset+rowOffset,
456 externalColOffset + colOffset[cIt.index()],
483 template <
class MatrixType>
485 const std::string& filename,
int outputPrecision = 18)
488 int oldPrecision = outStream.
precision();
502 for (
const auto& entry : v)
524 template <
class VectorType>
526 const std::string& filename,
int outputPrecision = 18)
529 int oldPrecision = outStream.
precision();
541 friend NullStream &operator<<(NullStream &dev0, Any &&) {
548 template <
class Stream,
class SVGMatrixOptions>
549 void writeSVGMatrixHeader(Stream &out,
const SVGMatrixOptions &opts,
551 auto [col_offset, row_offset] = offsets;
552 double width = opts.width;
553 double height = opts.height;
555 if (opts.width == 0 and opts.height == 0)
556 width = height = 500;
558 width = opts.height * (double(col_offset) / row_offset);
559 if (opts.height == 0)
560 height = opts.width * (double(row_offset) / col_offset);
563 double scale_width = width / col_offset;
564 double scale_height = height / row_offset;
567 out <<
"<svg xmlns='http://www.w3.org/2000/svg' width='" <<
std::ceil(width)
568 <<
"' height='" <<
std::ceil(height) <<
"' version='1.1'>\n"
570 << opts.style <<
"</style>\n"
571 <<
"<g transform='scale(" << scale_width <<
" " << scale_height
576 template <
class Stream,
class Mat,
class SVGMatrixOptions,
577 class RowPrefix,
class ColPrefix>
579 writeSVGMatrix(Stream &out,
const Mat &
mat, SVGMatrixOptions opts,
580 RowPrefix row_prefix, ColPrefix col_prefix) {
582 const auto& block_size = opts.block_size;
583 const auto& interspace = opts.interspace;
589 const bool write_header = opts.write_header;
590 opts.write_header =
false;
597 auto for_each_entry = [&
mat](
const auto &call_back) {
598 for (
auto row_it =
mat.begin(); row_it !=
mat.end(); ++row_it) {
599 for (
auto col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
600 call_back(row_it.index(), col_it.index(), *col_it);
609 row_prefix.push_back(0);
610 col_prefix.push_back(0);
613 if constexpr (Dune::blockLevel<typename Mat::block_type>() == 0) {
615 for_each_entry([&](
const auto &row,
const auto &
col,
const auto &val) {
617 std::size_t y_off = interspace + row * (interspace + block_size);
618 row_prefix.back() = row;
619 col_prefix.back() =
col;
620 opts.writeSVGBlock(ss, row_prefix, col_prefix, val,
621 {x_off, y_off, block_size, block_size});
623 col_offset += cols * (block_size + interspace);
624 row_offset += rows * (block_size + interspace);
631 for_each_entry([&](
const auto &row,
const auto &
col,
const auto &val) {
635 writeSVGMatrix(dev0, val, opts, row_prefix, col_prefix);
638 if (col_offsets[
col + 1] == null_offset)
639 col_offsets[
col + 1] = sub_size.first;
642 if (row_offsets[row + 1] == null_offset)
643 row_offsets[row + 1] = sub_size.second;
653 col_offsets[0] = interspace;
654 row_offsets[0] = interspace;
655 for (
std::size_t i = 1; i < col_offsets.size(); i++)
656 col_offsets[i] += col_offsets[i - 1] + interspace;
657 for (
std::size_t i = 1; i < row_offsets.size(); i++)
658 row_offsets[i] += row_offsets[i - 1] + interspace;
660 for_each_entry([&](
const auto &row,
const auto &
col,
const auto &val) {
663 col_offsets[
col + 1] - col_offsets[
col] - interspace;
665 row_offsets[row + 1] - row_offsets[row] - interspace;
666 row_prefix.back() = row;
667 col_prefix.back() =
col;
669 ss <<
"<svg x='" << col_offsets[
col] <<
"' y='" << row_offsets[row]
670 <<
"' width='" << width <<
"' height='" << height <<
"'>\n";
672 writeSVGMatrix(ss, val, opts, row_prefix, col_prefix);
675 col_offset = col_offsets.back();
676 row_offset = row_offsets.back();
682 writeSVGMatrixHeader(out, opts, {col_offset, row_offset});
684 col_prefix.pop_back();
685 row_prefix.pop_back();
687 opts.writeSVGBlock(out, row_prefix, col_prefix,
mat,
688 {0, 0, col_offset, row_offset});
693 out <<
"</g>\n</svg>\n";
696 return {col_offset, row_offset};
720 " fill: cornflowerblue;\n"
721 " fill-opacity: 0.4;\n"
722 " stroke-width: 2;\n"
724 " stroke-opacity: 0.5;\n"
726 " .matrix-block:hover {\n"
727 " fill: lightcoral;\n"
728 " fill-opacity: 0.4;\n"
729 " stroke-opacity: 1;\n"
750 template <
class RowPrefix,
class ColPrefix>
752 const ColPrefix & )
const {
754 return "matrix-block";
765 template <
class Stream,
class RowPrefix,
class ColPrefix,
class Block>
767 const ColPrefix &col_prefix,
768 const Block &block)
const {
769 if (this->write_block_title) {
771 assert(row_prefix.size() == col_prefix.size());
772 for (
std::size_t i = 0; i < row_prefix.size(); ++i)
773 out <<
"[" << row_prefix[i] <<
", "<< col_prefix[i] <<
"]";
774 if constexpr (Dune::blockLevel<Block>() == 0)
775 out <<
": " << block;
801 template <
class Stream,
class RowPrefix,
class ColPrefix,
class Block>
803 const RowPrefix &row_prefix,
804 const ColPrefix &col_prefix,
const Block block,
807 auto &[x_off, y_off, block_width, block_height] = svg_box;
811 out <<
"<rect class='" << block_class <<
"' x='" << x_off <<
"' y='"
812 << y_off <<
"' width='" << block_width <<
"' height='" << block_height <<
"'";
815 out <<
" style='fill-opacity: 1;fill:" <<
color_fill(
double(block)) <<
"'";
839 template <
class Mat,
class SVGOptions = DefaultSVGMatrixOptions>
844 Impl::writeSVGMatrix(out,
mat, opts, IndexPrefix{}, IndexPrefix{});
Some handy generic functions for ISTL matrices.
Implementation of the BCRSMatrix class.
Col col
Definition matrixmatrix.hh:351
Matrix & mat
Definition matrixmatrix.hh:347
void writeMatrixToMatlab(const MatrixType &matrix, const std::string &filename, int outputPrecision=18)
Writes sparse matrix in a Matlab-readable format.
Definition io.hh:484
void writeMatrixToMatlabHelper(const FieldType &value, int rowOffset, int colOffset, std::ostream &s)
Helper method for the writeMatrixToMatlab routine.
Definition io.hh:413
void print_row(std::ostream &s, const K &value, typename FieldMatrix< K, 1, 1 >::size_type I, typename FieldMatrix< K, 1, 1 >::size_type J, typename FieldMatrix< K, 1, 1 >::size_type therow, int width, int precision)
Print one row of a matrix, specialization for number types.
Definition io.hh:152
void printmatrix(std::ostream &s, const M &A, std::string title, std::string rowtext, int width=10, int precision=2)
Print a generic block matrix.
Definition io.hh:213
void printSparseMatrix(std::ostream &s, const BCRSMatrix< InnerMatrixType, A > &mat, std::string title, std::string rowtext, int width=3, int precision=2)
Prints a BCRSMatrix with fixed sized blocks.
Definition io.hh:302
void printvector(std::ostream &s, const V &v, std::string title, std::string rowtext, int columns=1, int width=10, int precision=2)
Print an ISTL vector.
Definition io.hh:89
void writeVectorToMatlabHelper(const V &v, std::ostream &stream)
Definition io.hh:497
void writeSVGMatrix(std::ostream &out, const Mat &mat, SVGOptions opts={})
Writes the visualization of matrix in the SVG format.
Definition io.hh:840
void writeVectorToMatlab(const VectorType &vector, const std::string &filename, int outputPrecision=18)
Writes vectors in a Matlab-readable format.
Definition io.hh:525
void recursive_printvector(std::ostream &s, const V &v, std::string rowtext, int &counter, int columns, int width)
Recursively print a vector.
Definition io.hh:52
void fill_row(std::ostream &s, int m, int width, int precision)
Print a row of zeros for a non-existing block.
Definition io.hh:133
static constexpr size_type M()
typename Base::size_type size_type
Definition matrixutils.hh:211
static auto coldim(const M &)
Definition matrixutils.hh:219
static auto rowdim(const M &)
Definition matrixutils.hh:214
A sparse block matrix with compressed row storage.
Definition bcrsmatrix.hh:468
Default options class to write SVG matrices.
Definition io.hh:707
std::string style
CSS style block to write in header.
Definition io.hh:719
std::size_t width
Final width size (pixels) of the SVG header. If 0, size is automatic.
Definition io.hh:713
std::function< std::string(const double &)> color_fill
Color fill for default options.
Definition io.hh:743
std::size_t block_size
size (pixels) of the deepst block/value of the matrix
Definition io.hh:709
void writeSVGBlock(Stream &out, const RowPrefix &row_prefix, const ColPrefix &col_prefix, const Block block, const std::array< std::size_t, 4 > &svg_box) const
Write an SVG object for a given block/value in the matrix.
Definition io.hh:802
void writeBlockTitle(Stream &out, const RowPrefix &row_prefix, const ColPrefix &col_prefix, const Block &block) const
Helper function writes a title for a given block and prefix.
Definition io.hh:766
std::size_t interspace
size (pixels) of the interspace between blocks
Definition io.hh:711
bool write_block_title
(Helper) Whether to write a title on the rectangle value
Definition io.hh:758
std::size_t height
Final height size (pixels) of the SVG header. If 0, size is automatic.
Definition io.hh:715
bool write_header
Whether to write the SVG header.
Definition io.hh:717
std::string blockStyleClass(const RowPrefix &, const ColPrefix &) const
Helper function that returns an style class for a given prefix.
Definition io.hh:751
ConstIterator class for sequential access.
Definition matrix.hh:404
A generic dynamic dense matrix.
Definition matrix.hh:561
RowIterator end()
Get iterator to one beyond last row.
Definition matrix.hh:616
RowIterator begin()
Get iterator to first row.
Definition matrix.hh:610
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition matrix.hh:589
size_type M() const
Return the number of columns.
Definition matrix.hh:696
size_type N() const
Return the number of rows.
Definition matrix.hh:691