IOLink  IOL_v1.6.1_release
Matrix.h
1 #pragma once
2 
3 #include <array>
4 #include <cstdint>
5 #include <initializer_list>
6 #include <ostream>
7 #include <string>
8 
9 #include <iolink/IOLinkAPI.h>
10 
11 namespace iolink
12 {
13 
19 template <typename ValueType, size_t N>
20 class Matrix
21 {
22 public:
26  static Matrix identity();
27 
33  static Matrix uniform(ValueType value);
34 
39  Matrix();
40 
56  Matrix(const ValueType* values);
57 
75  Matrix(const ValueType* values, bool isRowMajor);
76 
94  explicit Matrix(std::initializer_list<ValueType> init);
95 
96  bool operator==(const Matrix& other) const;
97  bool operator!=(const Matrix& other) const;
98 
105  ValueType at(size_t row, size_t column) const;
106 
113  ValueType operator()(size_t row, size_t column) const;
114 
122  void setAt(size_t row, size_t column, ValueType value);
123 
127  void transposeInPlace();
128 
132  Matrix transpose() const;
133 
152  ValueType* data();
153 
157  const ValueType* data() const;
158 
162  std::string toString() const;
163 
164  // ==================== //
165  // Arithmetic operators //
166  // ==================== //
167 
168  Matrix operator-() const;
169 
170  Matrix& operator+=(const Matrix& m);
171  Matrix& operator-=(const Matrix& m);
172 
173  inline Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; }
174  inline Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; }
175 
176  Matrix& operator*=(ValueType value);
177  Matrix& operator/=(ValueType value);
178 
179  inline Matrix operator*(ValueType v) const { return Matrix(*this) *= v; }
180  inline Matrix operator/(ValueType v) const { return Matrix(*this) /= v; }
181 
182 private:
183  std::array<ValueType, N * N> m_data;
184 };
185 
189 template <typename T, size_t N>
190 inline std::ostream&
191 operator<<(std::ostream& os, const Matrix<T, N>& m)
192 {
193  os << m.toString();
194  return os;
195 }
196 
197 //=======================//
198 // Template declarations //
199 //=======================//
200 
201 extern template class IOLINK_API_IMPORT Matrix<float, 3>;
202 extern template class IOLINK_API_IMPORT Matrix<double, 3>;
203 
204 extern template class IOLINK_API_IMPORT Matrix<float, 4>;
205 extern template class IOLINK_API_IMPORT Matrix<double, 4>;
206 
207 //======================//
208 // Aliases declarations //
209 //======================//
210 
213 
216 
217 } // end namespace iolink