17 #include <deal.II/base/vector_slice.h> 18 #include <deal.II/base/utilities.h> 19 #include <deal.II/lac/sparsity_pattern.h> 20 #include <deal.II/lac/sparsity_tools.h> 21 #include <deal.II/lac/full_matrix.h> 22 #include <deal.II/lac/dynamic_sparsity_pattern.h> 31 DEAL_II_NAMESPACE_OPEN
47 store_diagonal_first_in_row(false)
67 Assert (s.
rows == 0, ExcInvalidConstructorCall());
68 Assert (s.
cols == 0, ExcInvalidConstructorCall());
77 const unsigned int max_per_row)
93 const std::vector<unsigned int> &row_lengths)
101 reinit (m, n, row_lengths);
107 const unsigned int max_per_row)
114 reinit (m, m, max_per_row);
120 const std::vector<unsigned int> &row_lengths)
127 reinit (m, m, row_lengths);
133 const unsigned int max_per_row,
170 original_last_before_side_diagonals
171 = (row > extra_off_diagonals ?
174 row-extra_off_diagonals) :
178 original_first_after_side_diagonals
179 = (row <
rows-extra_off_diagonals-1 ?
180 std::upper_bound (original_row_start,
182 row+extra_off_diagonals) :
190 next_free_slot = std::copy (original_row_start,
191 original_last_before_side_diagonals,
195 for (
size_type i=1; i<=std::min(row,extra_off_diagonals);
196 ++i, ++next_free_slot)
197 *next_free_slot = row-i;
198 for (
size_type i=1; i<=std::min(extra_off_diagonals,
rows-row-1);
199 ++i, ++next_free_slot)
200 *next_free_slot = row+i;
203 next_free_slot = std::copy (original_first_after_side_diagonals,
211 ExcNotEnoughSpace (0,rowstart[row+1]-rowstart[row]));
231 Assert (s.
rows == 0, ExcInvalidConstructorCall());
232 Assert (s.
cols == 0, ExcInvalidConstructorCall());
236 Assert (
rows == 0, ExcInvalidConstructorCall());
237 Assert (
cols == 0, ExcInvalidConstructorCall());
247 const unsigned int max_per_row)
250 const std::vector<unsigned int> row_lengths (m, max_per_row);
251 reinit (m, n, row_lengths);
259 const VectorSlice<
const std::vector<unsigned int> > &row_lengths)
267 if ((m==0) || (n==0))
292 std::size_t vec_len = 0;
295 std::max(row_lengths[i], 1U) :
317 std::min (static_cast<size_type>(*std::max_element(row_lengths.begin(),
360 std::max(std::min(static_cast<size_type>(row_lengths[i-1]),n),
361 static_cast<size_type> (1U)) :
362 std::min(static_cast<size_type>(row_lengths[i-1]),n));
365 ((vec_len == 1) && (
rowstart[rows] == 0)),
397 const std::size_t nonzero_elements
400 std::bind2nd(std::not_equal_to<size_type>(),
invalid_entry));
426 ? tmp_entries.begin()+1
427 : tmp_entries.begin(),
432 new_colnums[next_free_entry++] = tmp_entries[j];
435 rowstart[line] = next_row_start;
436 next_row_start = next_free_entry;
442 (new_colnums[rowstart[line]] == line),
449 Assert ((rowstart[line] == next_row_start)
451 (std::find (&new_colnums[rowstart[line]+1],
452 &new_colnums[next_row_start],
453 new_colnums[rowstart[line]]) ==
454 &new_colnums[next_row_start]),
456 Assert ((rowstart[line] == next_row_start)
458 (std::adjacent_find(&new_colnums[rowstart[line]+1],
459 &new_colnums[next_row_start]) ==
460 &new_colnums[next_row_start]),
465 Assert (next_free_entry == nonzero_elements,
469 rowstart[
rows] = next_row_start;
483 template <
typename SparsityPatternType>
491 const bool do_diag_optimize = (dsp.n_rows() == dsp.n_cols());
492 std::vector<unsigned int> row_lengths (dsp.n_rows());
495 row_lengths[i] = dsp.row_length(i);
496 if (do_diag_optimize && !dsp.exists(i,i))
499 reinit (dsp.n_rows(), dsp.n_cols(), row_lengths);
505 for (
size_type row = 0; row<dsp.n_rows(); ++row)
508 typename SparsityPatternType::iterator col_num = dsp.begin (row),
509 end_row = dsp.end (row);
511 for (; col_num != end_row; ++col_num)
514 if ((col!=row) || !do_diag_optimize)
527 template <
typename number>
533 std::vector<unsigned int> entries_per_row (matrix.
m(), 0);
537 if (matrix(row,col) != 0)
538 ++entries_per_row[row];
539 if ((matrix.
m() == matrix.
n())
541 (matrix(row,row) == 0)
543 (matrix.
m() == matrix.
n()))
544 ++entries_per_row[row];
547 reinit (matrix.
m(), matrix.
n(), entries_per_row);
552 if (matrix(row,col) != 0)
563 const std::vector<unsigned int> &row_lengths)
565 reinit(m, n, make_slice(row_lengths));
641 = Utilities::lower_bound<const size_type *> (sorted_region_start,
644 if ((p != &
colnums[rowstart[i+1]]) && (*p == j))
675 Assert (
false, ExcNotEnoughSpace(i, rowstart[i+1]-rowstart[i]));
680 template <
typename ForwardIterator>
683 ForwardIterator
begin,
685 const bool indices_are_sorted)
687 if (indices_are_sorted ==
true)
691 ForwardIterator it =
begin;
692 bool has_larger_entries =
false;
700 has_larger_entries =
true;
703 if (has_larger_entries ==
false)
704 for ( ; it !=
end; ++it)
706 if (store_diagonal_first_in_row && *it == row)
708 Assert (k <= rowstart[row+1],
709 ExcNotEnoughSpace(row, rowstart[row+1]-rowstart[row]));
715 for (ForwardIterator p = begin; p !=
end; ++p)
722 for (ForwardIterator it = begin; it !=
end; ++it)
738 if (
colnums[k] == j)
return true;
755 if (
colnums[k] == j)
return k-rowstart[i];
762 std::pair<SparsityPattern::size_type, SparsityPattern::size_type>
782 return std::make_pair (row,col);
834 out <<
']' << std::endl;
855 out <<
colnums[j] <<
" " << -
static_cast<signed int>(i) << std::endl;
863 unsigned int m = this->
n_rows();
864 unsigned int n = this->
n_cols();
865 out <<
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 " << n+2
866 <<
" " << m+2 <<
" \">\n" 867 "<style type=\"text/css\" >\n" 874 " <rect width=\"" << n+2 <<
"\" height=\"" << m+2 <<
"\" fill=\"rgb(128, 128, 128)\"/>\n" 875 " <rect x=\"1\" y=\"1\" width=\"" << n <<
"\" height=\"" << m
876 <<
"\" fill=\"rgb(255, 255, 255)\"/>\n\n";
881 for (; it!=
end; ++it)
883 out <<
" <rect class=\"pixel\" x=\"" << it->
column()+1
884 <<
"\" y=\"" << it->
row()+1
885 <<
"\" width=\".9\" height=\".9\"/>\n";
887 out <<
"</svg>" << std::endl;
903 if (static_cast<size_type>(std::abs(static_cast<int>(i-
colnums[j]))) > b)
904 b = std::abs(static_cast<signed int>(i-
colnums[j]));
927 out.write (reinterpret_cast<const char *>(&
rowstart[0]),
929 - reinterpret_cast<const char *>(&
rowstart[0]));
931 out.write (reinterpret_cast<const char *>(&
colnums[0]),
933 - reinterpret_cast<const char *>(&
colnums[0]));
974 in.read (reinterpret_cast<char *>(&
rowstart[0]),
976 - reinterpret_cast<char *>(&
rowstart[0]));
981 in.read (reinterpret_cast<char *>(&
colnums[0]),
983 - reinterpret_cast<char *>(&
colnums[0]));
1001 template void SparsityPattern::copy_from<SparsityPattern> (
const SparsityPattern &);
1006 template void SparsityPattern::add_entries<const SparsityPattern::size_type *> (
const size_type ,
1010 #ifndef DEAL_II_VECTOR_ITERATOR_IS_POINTER 1011 template void SparsityPattern::add_entries<std::vector<SparsityPattern::size_type>::const_iterator>
1013 std::vector<size_type>::const_iterator,
1014 std::vector<size_type>::const_iterator,
1017 template void SparsityPattern::add_entries<std::vector<SparsityPattern::size_type>::iterator>
1019 std::vector<size_type>::iterator,
1020 std::vector<size_type>::iterator,
1023 DEAL_II_NAMESPACE_CLOSE
Iterator lower_bound(Iterator first, Iterator last, const T &val)
void block_write(std::ostream &out) const
const types::global_dof_index invalid_size_type
void add_entries(const size_type row, ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted=false)
#define AssertDimension(dim1, dim2)
size_type operator()(const size_type i, const size_type j) const
static const size_type invalid_entry
std::size_t memory_consumption() const
void print_gnuplot(std::ostream &out) const
bool exists(const size_type i, const size_type j) const
#define AssertThrow(cond, exc)
bool is_compressed() const
size_type bandwidth() const
void add(const size_type i, const size_type j)
#define Assert(cond, exc)
void print_svg(std::ostream &out) const
void print(std::ostream &out) const
SparsityPattern & operator=(const SparsityPattern &)
size_type max_entries_per_row() const
void copy_from(const size_type n_rows, const size_type n_cols, const ForwardIterator begin, const ForwardIterator end)
size_type n_nonzero_elements() const
void reinit(const size_type m, const size_type n, const unsigned int max_per_row)
unsigned int max_row_length
types::global_dof_index size_type
bool store_diagonal_first_in_row
void block_read(std::istream &in)
std::pair< size_type, size_type > matrix_position(const size_type global_index) const
unsigned int row_length(const size_type row) const
size_type row_position(const size_type i, const size_type j) const