HermesCommon  3.0
vector.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_VECTOR_H
23 #define __HERMES_COMMON_VECTOR_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 Vector : public Hermes::Mixins::Loggable, public Algebra::Mixins::MatrixRhsImportExport < Scalar >
37  {
38  public:
40  Vector();
43  Vector(unsigned int size);
44  virtual ~Vector() { }
45 
49  virtual void alloc(unsigned int ndofs) = 0;
51  virtual void free() = 0;
53  virtual void finish() { }
54 
58  virtual Scalar get(unsigned int idx) const = 0;
59 
61  virtual Vector<Scalar>* duplicate() const = 0;
62 
65  virtual void extract(Scalar *v) const = 0;
66 
68  virtual void zero() = 0;
69 
71  virtual Vector<Scalar>* change_sign() = 0;
72 
77  virtual void set(unsigned int idx, Scalar y) = 0;
78 
83  virtual void add(unsigned int idx, Scalar y) = 0;
84 
86  virtual Vector<Scalar>* set_vector(Vector<Scalar>* vec);
88  virtual Vector<Scalar>* set_vector(Scalar* vec);
89 
91  virtual Vector<Scalar>* add_vector(Vector<Scalar>* vec);
93  virtual Vector<Scalar>* add_vector(Scalar* vec);
94 
96  virtual Vector<Scalar>* subtract_vector(Vector<Scalar>* vec);
98  virtual Vector<Scalar>* subtract_vector(Scalar* vec);
99 
105  virtual void add(unsigned int n, unsigned int *idx, Scalar *y) = 0;
106 
108  unsigned int get_size() const { return this->size; }
109  protected:
111  unsigned int size;
112  };
113 
115  template <typename Scalar>
116  class HERMES_API SimpleVector : public Vector < Scalar >
117  {
118  public:
119  SimpleVector();
120  SimpleVector(unsigned int size);
121  virtual ~SimpleVector();
122 
123  virtual void alloc(unsigned int ndofs);
124  virtual void free();
125  virtual Scalar get(unsigned int idx) const;
126  virtual void extract(Scalar *v) const;
127  virtual void zero();
128  virtual Vector<Scalar>* change_sign();
129 
131  Vector<Scalar>* duplicate() const;
132 
133  virtual void set(unsigned int idx, Scalar y);
134  virtual void add(unsigned int idx, Scalar y);
135  virtual void add(unsigned int n, unsigned int *idx, Scalar *y);
136  virtual Vector<Scalar>* add_vector(Vector<Scalar>* vec);
137  virtual Vector<Scalar>* add_vector(Scalar* vec);
138  virtual Vector<Scalar>* subtract_vector(Vector<Scalar>* vec);
139  virtual Vector<Scalar>* subtract_vector(Scalar* vec);
140  virtual Vector<Scalar>* set_vector(Vector<Scalar>* vec);
141  virtual Vector<Scalar>* set_vector(Scalar* vec);
142 
145  virtual void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format = "%lf");
146  virtual void import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt);
147 
149  Scalar *v;
150  };
151 
154  template<typename Scalar> HERMES_API
155  Vector<Scalar>* create_vector(bool use_direct_solver = false);
156  }
157 
158  template<typename Scalar>
159  double get_l2_norm(Algebra::Vector<Scalar>* vec)
160  {
161  Scalar val = 0;
162  for (unsigned int i = 0; i < vec->get_size(); i++)
163  {
164  Scalar inc = vec->get(i);
165  val = val + inc*conj(inc);
166  }
167  return sqrt(std::abs(val));
168  };
169 
170  template<typename Scalar>
171  double get_l2_norm(Scalar* vec, int count)
172  {
173  Scalar val = 0;
174  for (unsigned int i = 0; i < count; i++)
175  {
176  Scalar inc = vec[i];
177  val = val + inc*conj(inc);
178  }
179  return sqrt(std::abs(val));
180  }
181 }
182 #endif
General namespace for the Hermes library.
General (abstract) vector representation in Hermes.
virtual Scalar get(unsigned int idx) const =0
MatrixExportFormat
Format of file matrix and vector output.
Utilities for all Algebra code.
Vector used with MUMPS solver.
Definition: vector.h:116
virtual void finish()
finish the assembly of the vector
Definition: vector.h:53
HERMES_API Vector< Scalar > * create_vector(bool use_direct_solver=false)
Function returning a vector according to the users's choice.
Definition: vector.cpp:530
Mixins classes for algebraic purposes.
void change_sign(T **matrix, unsigned int m, unsigned int n)
Changes the sign of a matrix.
unsigned int get_size() const
Get vector length.
Definition: vector.h:108
Mix-in classes for one functionality, for various classes to be derived from.
Scalar * v
Raw data.
Definition: vector.h:149
unsigned int size
size of vector
Definition: vector.h:111
Class the output of which is loggable, i.e. that uses functionality of info(), warn() Contains the cl...
Definition: mixins.h:62