cmake introduction
A short example project for the CMake build system
matrix.h
Go to the documentation of this file.
1 #ifndef __cxx_test_project_matrix_h__
2 #define __cxx_test_project_matrix_h__
3 
4 #include <memory>
5 #include <cassert>
6 #include <iomanip>
7 #include <cstddef>
8 #include <cmath>
9 
10 namespace core
11 {
12 
13 /**
14  @brief A dense quadratic matrix of arbitrary size.
15 
16  The template is typically a double.
17 */
18 template<typename Scalar>
19 class Matrix
20 {
21 public:
22  /// @brief constructor of an @a _size by @a _size matrix
23  /// @param _size number of rows/columns of square matrix.
24  Matrix(int _size)
25  : size(_size)
26  {
27  values = new Scalar [size*size]();
28  }
29 
30  /// @brief destructor, delete memory created during construction
31  virtual ~Matrix()
32  {
33  if (values != nullptr)
34  delete[] values;
35  }
36 
37  /// @brief access and change individual elements
38  /// @details The parameters @a i and @a j must be within \f$[1,\_size]\f$,
39  /// otherwise the program terminates with an error.
40  Scalar& operator()(int i, int j)
41  {
42  assert((1 <= i) && (i <= size));
43  assert((1 <= j) && (j <= size));
44  return *(values + size*(i - 1) + j - 1); }
45 
46  /// @brief access individual elements
47  /// @warning no checks are done for @a i and @a j.
48  Scalar operator()(int i, int j) const
49  { return *(values + size*(i - 1) + j - 1); }
50 
51  /// @brief get the number of rows/columns
52  int get_size () const
53  { return size; }
54 
55  /// @brief get the array of elements of the matrix
56  /// @note be careful with this. Do not delete it!
57  /// @note use saver const version, if possible
58  Scalar* get_values()
59  { return values; }
60 
61  /// @brief get the array of elements of the matrix
62  const Scalar* get_values() const
63  { return values; }
64 
65  /** @brief print the matrix in some nice form
66  *
67  * @param[in,out] os stream to which the matrix is written
68  *
69  * @todo more documentation, please!
70  */
71  template <typename OUTPUT_STREAM>
72  void print_formatted (OUTPUT_STREAM &os,
73  const unsigned int precision = 3,
74  const bool scientific = true,
75  const unsigned int width_ = 0,
76  const char *zero_string = "*",
77  const double denominator = 1.,
78  const double threshold = 0.) const
79  {
80  unsigned int width = width_;
81  os << "(" << size << "x" << size <<") Matrix" << std::endl;
82  // set output format, but store old
83  // state
84  std::ios::fmtflags old_flags = os.flags();
85  unsigned int old_precision = os.precision (precision);
86 
87  if (scientific) {
88  os.setf (std::ios::scientific, std::ios::floatfield);
89  if (!width)
90  width = precision+7;
91  }
92  else {
93  os.setf (std::ios::fixed, std::ios::floatfield);
94  if (!width)
95  width = precision+2;
96  }
97 
98  for (int i1 = 1 ; i1 <= size; i1++) {
99  for (int i2 = 1; i2 <= size; i2++) {
100  if (std::fabs((*this)(i1,i2)) > threshold)
101  os << std::setw(width)
102  << (*this)(i1,i2) * Scalar(denominator) << ' ';
103  else
104  os << std::setw(width) << zero_string << ' ';
105  }
106  if (i1 != size) os << std::endl;
107  }
108 
109  os << std::endl;
110  // reset output format
111  os.flags (old_flags);
112  os.precision(old_precision);
113  }
114 
115 private:
116  int size; ///< the number of rows/columns
117  Scalar* values; ///< the entries of the matrix (row-wise)
118 };
119 
120 }
121 
122 #endif
int get_size() const
get the number of rows/columns
Definition: matrix.h:52
Namespace for core components.
Definition: core.h:11
Scalar * get_values()
get the array of elements of the matrix
Definition: matrix.h:58
Scalar & operator()(int i, int j)
access and change individual elements
Definition: matrix.h:40
const Scalar * get_values() const
get the array of elements of the matrix
Definition: matrix.h:62
virtual ~Matrix()
destructor, delete memory created during construction
Definition: matrix.h:31
Matrix(int _size)
constructor of an _size by _size matrix
Definition: matrix.h:24
void print_formatted(OUTPUT_STREAM &os, const unsigned int precision=3, const bool scientific=true, const unsigned int width_=0, const char *zero_string="*", const double denominator=1., const double threshold=0.) const
print the matrix in some nice form
Definition: matrix.h:72
int size
the number of rows/columns
Definition: matrix.h:116
Scalar * values
the entries of the matrix (row-wise)
Definition: matrix.h:117
A dense quadratic matrix of arbitrary size.
Definition: matrix.h:19
Scalar operator()(int i, int j) const
access individual elements
Definition: matrix.h:48