IOLink  IOL_v1.1.0_release
Matrix.h
1 #pragma once
2 
3 #include <array>
4 #include <cstdint>
5 #include <initializer_list>
6 
7 #include <iolink/IOLinkAPI.h>
8 
9 namespace iolink
10 {
11 
17 template <typename ValueType, size_t N>
18 class Matrix
19 {
20 public:
24  static Matrix identity();
25 
31  static Matrix uniform(ValueType value);
32 
37  Matrix();
38 
54  Matrix(const ValueType* values);
55 
73  Matrix(const ValueType* values, bool isRowMajor);
74 
92  explicit Matrix(std::initializer_list<ValueType> init);
93 
94  bool operator==(const Matrix& other) const;
95  bool operator!=(const Matrix& other) const;
96 
103  ValueType at(size_t row, size_t column) const;
104 
111  ValueType operator()(size_t row, size_t column) const;
112 
120  void setAt(size_t row, size_t column, ValueType value);
121 
125  void transposeInPlace();
126 
130  Matrix transpose() const;
131 
150  ValueType* data();
151 
155  const ValueType* data() const;
156 
160  std::string toString() const;
161 
162  // ==================== //
163  // Arithmetic operators //
164  // ==================== //
165 
166  Matrix operator-() const;
167 
168  Matrix& operator+=(const Matrix& m);
169  Matrix& operator-=(const Matrix& m);
170 
171  inline Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; }
172  inline Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; }
173 
174  Matrix& operator*=(ValueType value);
175  Matrix& operator/=(ValueType value);
176 
177  inline Matrix operator*(ValueType v) const { return Matrix(*this) *= v; }
178  inline Matrix operator/(ValueType v) const { return Matrix(*this) /= v; }
179 
180 private:
181  std::array<ValueType, N * N> m_data;
182 };
183 
187 template <typename T, size_t N>
188 inline std::ostream&
189 operator<<(std::ostream& os, const Matrix<T, N>& m)
190 {
191  os << m.toString();
192  return os;
193 }
194 
195 //=======================//
196 // Template declarations //
197 //=======================//
198 
199 extern template class IOLINK_API_IMPORT Matrix<float, 3>;
200 extern template class IOLINK_API_IMPORT Matrix<double, 3>;
201 
202 extern template class IOLINK_API_IMPORT Matrix<float, 4>;
203 extern template class IOLINK_API_IMPORT Matrix<double, 4>;
204 
205 //======================//
206 // Aliases declarations //
207 //======================//
208 
211 
214 
215 } // end namespace iolink