Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
matrix.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef MTY_MATRIX_H_INCLUDED
24 #define MTY_MATRIX_H_INCLUDED
25 
26 #ifdef USE_BOOST
27 #include <boost/numeric/ublas/matrix.hpp>
28 #else
29 
30 #include <vector>
31 #include <iostream>
32 
33 namespace boost::numeric::ublas {
34 
35 template<class T>
36 class matrix {
37 
38 public:
39  matrix()
40  :m_size1(0), m_size2(0)
41  {}
42 
43  matrix(size_t s1, size_t s2)
44  :m_size1(s1), m_size2(s2),
45  data(std::vector<T>(s1*s2, 0))
46  {}
47 
48  matrix(matrix const &) = default;
49  matrix(matrix &&) = default;
50  matrix &operator=(matrix const &) = default;
51  matrix &operator=(matrix &&) = default;
52 
53  ~matrix() {}
54 
55  void resize(size_t s1, size_t s2) {
56  m_size1 = s1;
57  m_size2 = s2;
58  data = std::vector<T>(s1*s2, 0);
59  }
60 
61  size_t size1() const { return m_size1; }
62  size_t size2() const { return m_size2; }
63 
64  T const &operator()(size_t i, size_t j) const {
65  return data[i*m_size1 + j];
66  }
67  T &operator()(size_t i, size_t j) {
68  return data[i*m_size1 + j];
69  }
70 
71 protected:
72  size_t m_size1;
73  size_t m_size2;
74  std::vector<T> data;
75 };
76 
77 template<class T>
78 class identity_matrix: public matrix<T> {
79 
80 public:
81  identity_matrix(size_t s)
82  :matrix<T>(s, s)
83  {
84  for (size_t i = 0; i != this->m_size1; ++i)
85  (*this)(i, i) = 1;
86  }
87 
88  identity_matrix(identity_matrix<T> const &) = default;
89  identity_matrix(identity_matrix<T> &&) = default;
90  identity_matrix &operator=(identity_matrix<T> const &) = default;
91  identity_matrix &operator=(identity_matrix<T> &&) = default;
92 };
93 
94 template<class T>
95 std::ostream &operator<<(
96  std::ostream &out,
97  matrix<T> const &m
98  )
99 {
100  for (size_t i = 0; i < m.size1(); ++i) {
101  for (size_t j = 0; j != m.size2(); ++j) {
102  out << m(i, j) << ", ";
103  }
104  out << ",\n";
105  }
106  return out;
107 }
108 
109 } // End of namespace boost::numeric::ublas
110 #endif // #ifdef USE_BOOST
111 
112 #endif // #ifndef MTY_MATRIX_H_INCLUDED
std::ostream & operator<<(std::ostream &fout, csl::Type type)
Definition: matrix.h:36
Definition: matrix.h:33