HermesCommon  3.0
cs_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_CS_MATRIX_H
23 #define __HERMES_COMMON_CS_MATRIX_H
24 
25 #include "algebra/matrix.h"
26 
27 namespace Hermes
28 {
30  namespace Algebra
31  {
34  template <typename Scalar>
35  class HERMES_API CSMatrix : public SparseMatrix < Scalar >
36  {
37  public:
44  void create(unsigned int size, unsigned int nnz, int* ap, int* ai, Scalar* ax);
45 
47  static int find_position(int *Ai, int Alen, unsigned int idx);
48 
50  CSMatrix();
54  CSMatrix(unsigned int size);
55  virtual ~CSMatrix();
56 
58  void switch_orientation();
59 
62  virtual void add(unsigned int Ai_data_index, unsigned int Ai_index, Scalar v);
63 
66  virtual Scalar get(unsigned int Ai_data_index, unsigned int Ai_index) const;
67 
69  virtual void alloc();
70  // Allocate data storage.
71  virtual void alloc_data();
73  virtual void free();
75  virtual void zero();
77  virtual void set_row_zero(unsigned int n);
78 
82  void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format = "%lf", bool invert_storage = false);
83 
87  void import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt, bool invert_storage = false);
88 
90  virtual unsigned int get_nnz() const;
92  virtual double get_fill_in() const;
93 
95  void multiply_with_Scalar(Scalar value);
96 
99  int *get_Ap() const;
102  int *get_Ai() const;
105  Scalar *get_Ax() const;
106 
112  virtual void add_as_block(unsigned int i, unsigned int j, SparseMatrix<Scalar>* mat);
113 
114  protected:
117  Scalar *Ax;
119  int *Ai;
121  int *Ap;
123  unsigned int nnz;
124  template<typename T> friend SparseMatrix<T>* create_matrix();
125  };
126 
130  template <typename Scalar>
131  class HERMES_API CSCMatrix : public CSMatrix < Scalar >
132  {
133  public:
135  CSCMatrix();
136 
140  CSCMatrix(unsigned int size);
141 
142  virtual ~CSCMatrix();
143 
144  virtual Scalar get(unsigned int m, unsigned int n) const;
145 
146  virtual void add(unsigned int m, unsigned int n, Scalar v);
147 
148  void multiply_with_vector(Scalar* vector_in, Scalar*& vector_out, bool vector_out_initialized) const;
149 
150  virtual void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format = "%lf");
151  virtual void import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt);
152 
154  SparseMatrix<Scalar>* duplicate() const;
155  };
156 
160  template <typename Scalar>
161  class HERMES_API CSRMatrix : public CSMatrix < Scalar >
162  {
163  public:
165  CSRMatrix();
166 
170  CSRMatrix(unsigned int size);
171 
172  virtual ~CSRMatrix();
173 
174  virtual Scalar get(unsigned int m, unsigned int n) const;
175 
176  virtual void add(unsigned int m, unsigned int n, Scalar v);
177 
178  void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format = "%lf");
179  void import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt);
180 
182  SparseMatrix<Scalar>* duplicate() const;
183 
190  virtual void pre_add_ij(unsigned int row, unsigned int col);
191  };
192  }
193 }
194 #endif
unsigned int nnz
Number of non-zero entries ( = Ap[size]).
Definition: cs_matrix.h:123
General namespace for the Hermes library.
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
General CSR Matrix class. (can be used in umfpack, in that case use the CSCMatrix subclass...
Definition: cs_matrix.h:161
int * Ap
Index to Ax/Ai, where each column / row starts.
Definition: cs_matrix.h:121
General (abstract) sparse matrix representation in Hermes.
MatrixExportFormat
Format of file matrix and vector output.
General CSC Matrix class. (can be used in umfpack, in that case use the CSCMatrix subclass...
Definition: cs_matrix.h:131
Basic matrix classes and operations.
int * Ai
Row / Column indices of values in Ax.
Definition: cs_matrix.h:119
General CS Matrix class. Either row- or column- specific (see subclassses).
Definition: cs_matrix.h:35