HermesCommon  3.0
matrix.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_MATRIX_H
23 #define __HERMES_COMMON_MATRIX_H
24 
25 #include "algebra_utilities.h"
26 #include "algebra_mixins.h"
27 #include "mixins.h"
28 
29 namespace Hermes
30 {
32  namespace Algebra
33  {
35  template<typename Scalar>
36  class HERMES_API Matrix : public Hermes::Mixins::Loggable, public Algebra::Mixins::MatrixRhsImportExport < Scalar >
37  {
38  public:
41  Matrix(unsigned int size = 0);
42  virtual ~Matrix() {};
43 
45  virtual void alloc() = 0;
46 
48  virtual void free() = 0;
49 
54  virtual Scalar get(unsigned int m, unsigned int n) const = 0;
55 
57  virtual void zero() = 0;
58 
64  virtual void set_row_zero(unsigned int n);
65 
71  virtual void add(unsigned int m, unsigned int n, Scalar v) = 0;
72 
80  virtual void add(unsigned int m, unsigned int n, Scalar *mat, int *rows, int *cols, const int size);
81 
83  virtual void add_to_diagonal(Scalar v);
84 
86  virtual void multiply_with_vector(Scalar* vector_in, Scalar*& vector_out, bool vector_out_initialized = false) const;
87 
89  virtual void multiply_with_Scalar(Scalar value);
90 
93  virtual unsigned int get_size() const;
94 
95  protected:
97  unsigned int size;
98  };
99 
101  template<typename Scalar>
102  class HERMES_API SparseMatrix : public Matrix < Scalar > {
103  public:
104  SparseMatrix();
107  SparseMatrix(unsigned int size);
108  virtual ~SparseMatrix();
109 
113  virtual void prealloc(unsigned int n);
114 
116  virtual void free();
117 
122  virtual void pre_add_ij(unsigned int row, unsigned int col);
123 
125  virtual void finish();
126 
129  virtual void add_sparse_matrix(SparseMatrix<Scalar>* mat);
130 
135  virtual void add_sparse_to_diagonal_blocks(int num_stages, SparseMatrix<Scalar>* mat);
136 
141  virtual void add_as_block(unsigned int i, unsigned int j, SparseMatrix<Scalar>* mat);
142 
147  virtual int get_num_row_entries(unsigned int row) const;
148 
156  virtual void extract_row_copy(unsigned int row, unsigned int len,
157  unsigned int &n_entries, double *vals, unsigned int *idxs) const;
158 
163  virtual int get_num_col_entries(unsigned int col) const;
164 
172  virtual void extract_col_copy(unsigned int col, unsigned int len,
173  unsigned int &n_entries, double *vals, unsigned int *idxs) const;
174 
176  virtual SparseMatrix<Scalar>* duplicate() const;
177 
179  virtual double get_fill_in() const = 0;
180 
183  virtual unsigned int get_nnz() const;
184 
185  protected:
188  static const int PAGE_SIZE = 100;
189 
191  struct Page {
192  Page(bool dyn_stored_ = false) : dyn_stored(dyn_stored_), next(nullptr), count(0) {
193  };
195  unsigned char count;
197  int idx[PAGE_SIZE];
202  };
203 
206  Page **next_pages;
207 
214  int sort_and_store_indices(Page *page, int *buffer, int *max);
217  int get_num_indices();
218  };
219 
222  template<typename Scalar> HERMES_API
223  SparseMatrix<Scalar>* create_matrix(bool use_direct_solver = false);
224  }
225 }
226 #endif
General (abstract) matrix representation in Hermes.
Definition: matrix.h:36
General namespace for the Hermes library.
bool dyn_stored
this page is stored in the dynamically allocated part.
Definition: matrix.h:201
HERMES_API SparseMatrix< Scalar > * create_matrix(bool use_direct_solver=false)
Function returning a matrix according to the users's choice.
Definition: matrix.cpp:298
unsigned int size
matrix size
Definition: matrix.h:97
General (abstract) sparse matrix representation in Hermes.
Utilities for all Algebra code.
Mixins classes for algebraic purposes.
Structure for storing indices in sparse matrix.
Definition: matrix.h:191
Mix-in classes for one functionality, for various classes to be derived from.
Page * pages
array of pages with indices array. Each field of arra contains pages for one column ...
Definition: matrix.h:205
Page * next
pointer to next page
Definition: matrix.h:199
Class the output of which is loggable, i.e. that uses functionality of info(), warn() Contains the cl...
Definition: mixins.h:62