vector_matrix.h
Go to the documentation of this file.
1 // LIC// ====================================================================
2 // LIC// This file forms part of oomph-lib, the object-oriented,
3 // LIC// multi-physics finite-element library, available
4 // LIC// at http://www.oomph-lib.org.
5 // LIC//
6 // LIC// Copyright (C) 2006-2024 Matthias Heil and Andrew Hazel
7 // LIC//
8 // LIC// This library is free software; you can redistribute it and/or
9 // LIC// modify it under the terms of the GNU Lesser General Public
10 // LIC// License as published by the Free Software Foundation; either
11 // LIC// version 2.1 of the License, or (at your option) any later version.
12 // LIC//
13 // LIC// This library is distributed in the hope that it will be useful,
14 // LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // LIC// Lesser General Public License for more details.
17 // LIC//
18 // LIC// You should have received a copy of the GNU Lesser General Public
19 // LIC// License along with this library; if not, write to the Free Software
20 // LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 // LIC// 02110-1301 USA.
22 // LIC//
23 // LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24 // LIC//
25 // LIC//====================================================================
26 #ifndef OOMPH_VECTOR_MATRIX_HEADER
27 #define OOMPH_VECTOR_MATRIX_HEADER
28 
29 
30 // Config header generated by autoconfig
31 #ifdef HAVE_CONFIG_H
32 #include <oomph-lib-config.h>
33 #endif
34 
35 
36 #ifdef OOMPH_HAS_MPI
37 #include "mpi.h"
38 #endif
39 
40 // oomph-lib headers
41 #include "Vector.h"
42 #include "oomph_utilities.h"
43 
44 namespace oomph
45 {
46  //================================================================
47  /// VectorMatrix is a generalised, STL-map-based, matrix based on a Vector
48  /// of Vectors.
49  ///
50  /// Example of usage:
51  /// \code
52  ///
53  /// // Assume we have a Vector of pointers to objects:
54  /// VectorMatrix<int> vector_matrix(4,5,-1);
55  ///
56  /// // This is a 4 by 5 matrix with all entries set to -1.
57  ///
58  /// std::cout << vector_matrix[3][3] << " "
59  // << vector_matrix.nrow() << " "
60  // << vector_matrix.col() << std::endl;
61  ///
62  /// // Output:
63  /// // -1 4 5
64  ///
65  /// vector_matrix[3][3] = 42;
66  ///
67  /// std::cout << vector_matrix[3][3] << " "
68  /// << vector_matrix.nrow() << " "
69  /// << vector_matrix.col() << std::endl;
70  ///
71  /// // Output:
72  /// // 42 4 5
73  ///
74  /// \endcode
75  ///
76  //================================================================
77  template<class VALUE_TYPE>
79  {
80  public:
81  /// Default constructor - constructs an empty matrix.
83  {
84  Vector_matrix.resize(0);
85  }
86 
87  /// Constructor - constructs an n by m matrix with value val.
88  VectorMatrix(const unsigned& n, const unsigned& m, const VALUE_TYPE& val)
89  {
90  this->build_vectors_and_value(n, m, val);
91  }
92 
93  /// Constructor - constructs an n by m matrix, the value is defined
94  /// by the default initialisation of VALUE_TYPE.
95  VectorMatrix(const unsigned& n, const unsigned& m)
96  {
97  this->build_vectors(n, m);
98  }
99 
100  /// Default virtual destructor
101  virtual ~VectorMatrix()
102  {
103  this->clear();
104  }
105 
106  /// returns the number of rows. This is the outer Vector size.
107  const unsigned nrow() const
108  {
109 #ifdef PARANOID
110  // Inner vector size consistency check: All inner vectors must be the same
111  // size. Although the size of the inner vectors are not directly used to
112  // calculate the nrow(), we perform this check here in case the user has
113  // done something dodgy.
114 
115  const unsigned para_nrow = Vector_matrix.size();
116 
117  if (para_nrow > 0)
118  {
119  // There is at least one inner vector
120  unsigned inner_vector0_size = Vector_matrix[0].size();
121 
122  for (unsigned row_i = 0; row_i < para_nrow; row_i++)
123  {
124  unsigned current_inner_vector_size = Vector_matrix[row_i].size();
125  if (current_inner_vector_size != inner_vector0_size)
126  {
127  std::ostringstream err_msg;
128  err_msg << "The size of the inner vectors are not consistent.\n"
129  << "Vector_matrix[0].size() is " << inner_vector0_size
130  << "\n"
131  << "Vector_matrix[" << row_i << "] is "
132  << current_inner_vector_size << "\n";
133  throw OomphLibError(
134  err_msg.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
135  }
136  }
137  }
138 #endif
139 
140  return Vector_matrix.size();
141  }
142 
143  /// return the number of columns. This is the size of the first inner
144  /// vectors, or returns 0 if the outer vector is of size 0
145  /// (this->nrow() is 0).
146  const unsigned ncol() const
147  {
148 #ifdef PARANOID
149  // Inner vector size consistency check: All inner vectors must be the same
150  // size.
151 
152  const unsigned para_nrow = this->nrow();
153  if (para_nrow > 0)
154  {
155  // There is at least one inner vector
156  unsigned inner_vector0_size = Vector_matrix[0].size();
157 
158  for (unsigned row_i = 0; row_i < para_nrow; row_i++)
159  {
160  unsigned current_inner_vector_size = Vector_matrix[row_i].size();
161  if (current_inner_vector_size != inner_vector0_size)
162  {
163  std::ostringstream err_msg;
164  err_msg << "The size of the inner vectors are not consistent.\n"
165  << "Vector_matrix[0].size() is " << inner_vector0_size
166  << "\n"
167  << "Vector_matrix[" << row_i << "] is "
168  << current_inner_vector_size << "\n";
169  throw OomphLibError(
170  err_msg.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
171  }
172  }
173  }
174 #endif
175 
176  if (this->nrow() == 0)
177  {
178  return 0;
179  }
180  else
181  {
182  return Vector_matrix[0].size();
183  }
184  }
185 
186  /// [] access function to the i-th inner vector.
188  {
189  return Vector_matrix[i];
190  }
191 
192  /// [] access function to the i-th inner vector const version
193  const Vector<VALUE_TYPE>& operator[](const size_t i) const
194  {
195  return Vector_matrix[i];
196  }
197 
198 
199  /// Clears the outer vector. Calling Vector::clear() will invoke the
200  /// destructor of all the inner Vectors.
201  void clear()
202  {
203  Vector_matrix.clear();
204  }
205 
206  /// Resize the existing VectorMatrix.
207  /// WARNING: This invokes the resize function in std::vector, as such, only
208  /// new values are assigned, old values as kept.
209  /// e.g. if vec = [2,2,2], then vec.resize(5,3) gives
210  /// vec = [2, 2, 2, 3, 3].
211  void resize(const size_t& n, const size_t& m, VALUE_TYPE val = VALUE_TYPE())
212  {
213  Vector_matrix.resize(n);
214  for (unsigned i = 0; i < n; i++)
215  {
216  Vector_matrix[i].resize(m, val);
217  }
218  }
219 
220  /// Any elements held in the container before the call are destroyed
221  /// and replaced by newly constructed elements (no assignments of elements
222  /// take place).
223  /// This causes an automatic reallocation of the allocated storage space if
224  /// -and only if- the new vector size surpasses the current vector capacity.
225  /// This invokes std::assign on both the outer vector and the inner vectors.
226  void assign(const size_t& n, const size_t& m, const VALUE_TYPE& val)
227  {
228  Vector_matrix.assign(n, Vector<VALUE_TYPE>(m, val));
229  }
230 
231 
232  protected:
233  /// Builds an n by m VectorMatrix with default VALUE_TYPE.
234  void build_vectors(const unsigned& n, const unsigned& m)
235  {
236  Vector_matrix.resize(n, Vector<VALUE_TYPE>(m));
237  }
238 
239  /// Build an m by n VectorMatrix with VALUE_TYPE val.
240  void build_vectors_and_value(const unsigned& n,
241  const unsigned& m,
242  const VALUE_TYPE& val)
243  {
244  Vector_matrix.resize(n, Vector<VALUE_TYPE>(m, val));
245  }
246 
247  /// Here's the generalised matrix structure: A Vector of Vector to
248  /// templated by VALUE_TYPE.
250  };
251 
252 } // namespace oomph
253 
254 #endif
cstr elem_len * i
Definition: cfortran.h:603
An OomphLibError object which should be thrown when an run-time error is encountered....
VectorMatrix is a generalised, STL-map-based, matrix based on a Vector of Vectors.
Definition: vector_matrix.h:79
void build_vectors(const unsigned &n, const unsigned &m)
Builds an n by m VectorMatrix with default VALUE_TYPE.
void build_vectors_and_value(const unsigned &n, const unsigned &m, const VALUE_TYPE &val)
Build an m by n VectorMatrix with VALUE_TYPE val.
Vector< Vector< VALUE_TYPE > > Vector_matrix
Here's the generalised matrix structure: A Vector of Vector to templated by VALUE_TYPE.
void assign(const size_t &n, const size_t &m, const VALUE_TYPE &val)
Any elements held in the container before the call are destroyed and replaced by newly constructed el...
virtual ~VectorMatrix()
Default virtual destructor.
const unsigned ncol() const
return the number of columns. This is the size of the first inner vectors, or returns 0 if the outer ...
void resize(const size_t &n, const size_t &m, VALUE_TYPE val=VALUE_TYPE())
Resize the existing VectorMatrix. WARNING: This invokes the resize function in std::vector,...
const Vector< VALUE_TYPE > & operator[](const size_t i) const
[] access function to the i-th inner vector const version
const unsigned nrow() const
returns the number of rows. This is the outer Vector size.
VectorMatrix(const unsigned &n, const unsigned &m, const VALUE_TYPE &val)
Constructor - constructs an n by m matrix with value val.
Definition: vector_matrix.h:88
void clear()
Clears the outer vector. Calling Vector::clear() will invoke the destructor of all the inner Vectors.
VectorMatrix()
Default constructor - constructs an empty matrix.
Definition: vector_matrix.h:82
Vector< VALUE_TYPE > & operator[](const size_t i)
[] access function to the i-th inner vector.
VectorMatrix(const unsigned &n, const unsigned &m)
Constructor - constructs an n by m matrix, the value is defined by the default initialisation of VALU...
Definition: vector_matrix.h:95
A slight extension to the standard template vector class so that we can include "graceful" array rang...
Definition: Vector.h:58
//////////////////////////////////////////////////////////////////// ////////////////////////////////...