libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNMatrix.h
Go to the documentation of this file.
1 /* Copyright 2014-2016 INSA-Lyon, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNMatrix.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNMATRIX_HEADER
23 #define CRNMATRIX_HEADER
24 
25 #include <CRNObject.h>
26 #include <CRNException.h>
27 #include <CRNMath/CRNMath.h>
28 #include <CRNString.h>
29 #include <vector>
30 
31 namespace crn
32 {
33  /****************************************************************************/
43  template<typename T> class Matrix: public Object
44  {
45  public:
52  Matrix(size_t nrow, size_t ncol, const T &value = T(0)) : rows(nrow), cols(ncol)
53  {
54  if ((rows == 0) || (cols == 0))
55  throw ExceptionDomain("Matrix::Matrix(size_t nrow, size_t ncol, double value): null or negative row or column dimensions");
56  datatype(rows * cols, value).swap(data);
57  }
58 
64  Matrix(const std::vector<std::vector<T>> &m)
65  {
66  if (m.empty())
67  throw ExceptionDimension("Matrix::Matrix(m): empty matrix.");
68  if (m.front().empty())
69  throw ExceptionDimension("Matrix::Matrix(m): empty matrix.");
70  auto r = m.size();
71  auto c = m.front().size();
72  for (const auto &line : m)
73  if (line.size() != c)
74  throw ExceptionInvalidArgument("Matrix::Matrix(m): the argument is not a matrix.");
75  rows = r;
76  cols = c;
77  datatype(rows * cols).swap(data);
78  for (size_t r = 0; r < rows; ++r)
79  std::copy(m[r].begin(), m[r].end(), data.begin() + r * cols);
80  }
81 
88  Matrix(const std::vector<T> &vect, Orientation ori = Orientation::VERTICAL)
89  {
90  if (vect.empty())
91  throw ExceptionDimension("Matrix::Matrix(vect, orientation): empty matrix.");
92  if (ori == Orientation::VERTICAL)
93  {
94  rows = vect.size();
95  cols = 1;
96  data = vect;
97  }
98  else if (ori == Orientation::HORIZONTAL)
99  {
100  rows = 1;
101  cols = vect.size();
102  data = vect;
103  }
104  else
105  throw ExceptionInvalidArgument("Matrix::Matrix(vect, orientation): invalid orientation.");
106  }
107 
114  Matrix(std::vector<T> &&vect, Orientation ori = Orientation::VERTICAL)
115  {
116  if (vect.empty())
117  throw ExceptionDimension("Matrix::Matrix(vect, orientation): empty matrix.");
118  if (ori == Orientation::VERTICAL)
119  {
120  rows = vect.size();
121  cols = 1;
122  data = std::move(vect);
123  }
124  else if (ori == Orientation::HORIZONTAL)
125  {
126  rows = 1;
127  cols = vect.size();
128  data = std::move(vect);
129  }
130  else
131  throw ExceptionInvalidArgument("Matrix::Matrix(vect, orientation): invalid orientation.");
132  }
133 
135  virtual ~Matrix() override = default;
136  Matrix(const Matrix &) = default;
137  template<typename Y> Matrix(const Matrix<Y> &m) : rows(m.GetRows()), cols(m.GetCols())
138  {
139  datatype(rows * cols).swap(data);
140  std::copy(m.Std().begin(), m.Std().end(), data.begin());
141  }
142  Matrix(Matrix &&) = default;
143  Matrix& operator=(const Matrix &) = default;
144  Matrix& operator=(Matrix &&) = default;
145 
146  inline void Swap(Matrix &m)
147  {
148  std::swap(rows, m.rows);
149  std::swap(cols, m.cols);
150  std::swap(data, m.data);
151  }
152 
157  inline size_t GetRows() const noexcept { return rows; }
158 
163  inline size_t GetCols() const noexcept { return cols; }
164 
165  inline const T& At(size_t pos) const noexcept { return data[pos]; }
166  inline T& At(size_t pos) noexcept { return data[pos]; }
167  inline const T& At(size_t r, size_t c) const noexcept { return data[r * cols + c]; }
168  inline T& At(size_t r, size_t c) noexcept { return data[r * cols + c]; }
169 
173  void SetAll(const T &v)
174  {
175  for (size_t r = 0; r < rows; ++r)
176  for (size_t c = 0; c < cols; ++c)
177  At(r, c) = v;
178  }
179 
182  void Negative()
183  {
184  for (size_t r = 0; r < rows; ++r)
185  for (size_t c = 0; c < cols; ++c)
186  At(r, c) = -At(r, c);
187  }
188 
196  inline void SetRow(size_t r, const std::vector<T> &row)
197  {
198  if (r > rows)
199  throw ExceptionDomain("Matrix::SetRow(): index out of range.");
200  if (row.size() != cols)
201  throw ExceptionDimension("Matrix::SetRow(): argument is not of the right size.");
202  for (size_t c = 0; c < cols; ++c)
203  At(r, c) = row[c];
204  }
205 
213  inline void SetColum(size_t c, const std::vector<T> &col)
214  {
215  if (c > cols)
216  throw ExceptionDomain("Matrix::SetColumn(): index out of range.");
217  if (col.size() != rows)
218  throw ExceptionDimension("Matrix::SetColumn(): argument is not of the right size.");
219  for (size_t r = 0; r < rows; ++r)
220  At(r, c) = col[r];
221  }
222 
230  inline void IncreaseElement(size_t r, size_t c, const T &delta)
231  {
232  if (!areValidIndexes(r, c))
233  throw ExceptionDomain("Matrix::IncreaseElement(): index out of range.");
234  At(r, c) += delta;
235  }
236 
242  void IncreaseRow(size_t r, const T &v)
243  {
244  if (r >= rows)
245  throw ExceptionDomain("Matrix::IncreaseRow(): column index out of range");
246  for (size_t c = 0; c < cols; ++c)
247  At(r, c) += v;
248  }
249 
255  void IncreaseColumn(size_t c, const T &v)
256  {
257  if (c >= cols)
258  throw ExceptionDomain("Matrix::IncreaseColumn(): column index out of range");
259  for (size_t r = 0; r < rows; ++r)
260  At(r, c) += v;
261  }
262 
264  {
265  if ((m.GetRows() != rows) || (m.GetCols() != cols))
266  throw ExceptionDimension("Matrix::+=(): incompatible dimensions");
267  for (auto i : Range(data))
268  At(i) += m.At(i);
269  return *this;
270  }
272  {
273  if ((m.GetRows() != rows) || (m.GetCols() != cols))
274  throw ExceptionDimension("Matrix::+=(): incompatible dimensions");
275  for (auto i : Range(data))
276  At(i) -= m.At(i);
277  return *this;
278  }
280  {
281  if (cols != m.GetRows())
282  throw ExceptionDimension("Matrix::*=(): incompatible dimensions");
283  auto p = datatype(rows * m.GetCols());
284  for (size_t r = 0; r < rows; r++)
285  for (size_t c = 0; c < m.GetCols(); c++)
286  for (size_t k = 0; k < cols; k++)
287  p[r * m.GetCols() + c] += At(r, k) * m[k][c];
288  cols = m.GetCols();
289  data.swap(p);
290  return *this;
291  }
292  bool operator==(const Matrix &m) const
293  {
294  if ((m.GetRows() != rows) || (m.GetCols() != cols))
295  return false;
296  return std::equal(data.begin(), data.end(), m.data.begin());
297  }
298  inline bool operator!=(const Matrix &m) const
299  { return !(*this == m); }
300 
306  Matrix& operator*=(double d) { for (auto &v : data) v = T(v * d); return *this; }
307 
313  Matrix& operator/=(double d) { for (auto &v : data) v = T(v / d); return *this; }
314 
320  void MultRow(size_t r, double v)
321  {
322  if (r >= rows)
323  throw ExceptionDomain("Matrix::ScaleRow(): row index out of range");
324  for (size_t c = 0; c < cols; ++c)
325  At(r, c) = T(v * At(r, c));
326  }
327 
333  void MultColumn(size_t c, double v)
334  {
335  if (c >= cols)
336  throw ExceptionDomain("Matrix::ScaleColumn(): column index out of range");
337  for (size_t r = 0; r < rows; ++r)
338  At(r, c) = T(v * At(r, c));
339  }
340 
343  {
344  auto means = std::vector<T>(cols, T(0));
345  for (size_t r = 0; r < rows; ++r)
346  for (size_t c = 0; c < cols; ++c)
347  means[c] += At(r, c);
348  for (size_t c = 0; c < cols; ++c)
349  means[c] = means[c] / T(rows);
350  for (size_t r = 0; r < rows; ++r)
351  for (size_t c = 0; c < cols; ++c)
352  At(r, c) -= means[c];
353  }
354 
358  void ReduceColumns(bool centered = true)
359  {
360  auto means = std::vector<T>(cols, T(0));
361  if (!centered)
362  {
363  for (size_t r = 0; r < rows; ++r)
364  for (size_t c = 0; c < cols; ++c)
365  means[c] += At(r, c);
366  for (size_t c = 0; c < cols; ++c)
367  means[c] = means[c] / T(rows);
368  }
369  auto devs = std::vector<T>(cols, T(0));
370  for (size_t r = 0; r < rows; ++r)
371  for (size_t c = 0; c < cols; ++c)
372  devs[c] += Sqr(At(r, c) - means[c]);
373  for (size_t c = 0; c < cols; ++c)
374  devs[c] = sqrt(devs[c] / T(rows));
375  for (size_t r = 0; r < rows; ++r)
376  for (size_t c = 0; c < cols; ++c)
377  At(r, c) /= devs[c];
378  }
379 
385  void SwapRows(size_t r1, size_t r2)
386  {
387  if ((r1 >= rows) || (r2 >= rows))
388  throw ExceptionDomain("Matrix::SwapRows(): index out of range");
389  if (r1 != r2)
390  for (size_t c = 0; c < cols; ++c)
391  std::swap(At(r1, c), At(r2, c));
392  }
393 
399  void SwapColumns(size_t c1, size_t c2)
400  {
401  if ((c1 >= cols) || (c2 >= cols))
402  throw ExceptionDomain("Matrix::SwapColumns(): index out of range");
403  if (c1 != c2)
404  for (size_t r = 0; r < rows; ++r)
405  std::swap(At(r, c1), At(r, c2));
406  }
407 
410  {
411  auto sum = T(0);
412  for (size_t r = 0; r < rows; ++r)
413  for (size_t c = 0; c < cols; ++c)
414  sum += Abs(At(r, c));
415  if (sum != T(0))
416  for (size_t r = 0; r < rows; ++r)
417  for (size_t c = 0; c < cols; ++c)
418  At(r, c) /= sum;
419  }
420 
423  {
424  auto sum = typename TypeInfo<T>::SumType(0);
425  for (size_t r = 0; r < rows; r++)
426  for (size_t c = 0; c < cols; c++)
427  sum += At(r, c);
428  return sum;
429  }
430 
432  T GetMin() const
433  {
434  return *std::min_element(data.begin(), data.end());
435  }
436 
438  T GetMax() const
439  {
440  return *std::max_element(data.begin(), data.end());
441  }
442 
444  std::pair<size_t, size_t> Argmin() const
445  {
446  auto pos = std::min_element(data.begin(), data.end()) - data.begin();
447  return std::make_pair(pos / cols, pos % cols);
448  }
449 
451  std::pair<size_t, size_t> Argmax() const
452  {
453  auto pos = std::max_element(data.begin(), data.end()) - data.begin();
454  return std::make_pair(pos / cols, pos % cols);
455  }
456 
461  {
462  auto out = Matrix(cols, rows);
463  for (size_t r = 0; r < rows; ++r)
464  for (size_t c = 0; c < cols; ++c)
465  out[c][r] = At(r, c);
466  return out;
467  }
468 
472  virtual Matrix& Transpose()
473  {
474  datatype newdata(cols * rows);
475  for (size_t r = 0; r < rows; ++r)
476  for (size_t c = 0; c < cols; ++c)
477  newdata[c * rows + r] = At(r, c);
478  data.swap(newdata);
479  std::swap(rows, cols);
480  return *this;
481  }
482 
489  Matrix MakeRowAsColumn(size_t r) const
490  {
491  if (r >= GetRows())
492  throw ExceptionDomain("Matrix::MakeRowAsColumn(): index out of range");
493  return Matrix(datatype(data.begin() + r * cols, data.begin() + (r + 1) * cols), Orientation::VERTICAL);
494  }
495 
501  Matrix MakeRow(size_t r) const
502  {
503  if (r >= GetRows())
504  throw ExceptionDomain("Matrix::MakeRow(): row index out of range");
505  return Matrix(datatype(data.begin() + r * cols, data.begin() + (r + 1) * cols), Orientation::VERTICAL);
506  }
507 
513  Matrix MakeColumn(size_t k) const
514  {
515  if (k >= cols)
516  throw ExceptionDomain("Matrix::MakeColumn(): column index out of range");
517  auto column = Matrix(rows, 1, 0.0);
518  for (size_t r = 0; r < rows; r++)
519  column.At(r, 0) = At(r, k);
520  return column;
521  }
522 
527  size_t ArgmaxInRow(size_t r) const
528  {
529  if (r >= rows)
530  throw ExceptionDomain("Matrix::ArgmaxInRow(): index out of range");
531  return std::max_element(data.begin() + r * cols, data.begin() + (r + 1) * cols) - (data.begin() + r * cols);
532  }
533 
538  size_t ArgmaxInColumn(size_t c) const
539  {
540  if (c >= cols)
541  throw ExceptionDomain("Matrix::ArgmaxInColumn(): index out of range");
542  auto max = At(0, c);
543  auto index = size_t(0);
544  for (size_t r = 0; r < rows; r++)
545  {
546  const auto &mrc = At(r, c);
547  if (mrc > max)
548  {
549  max = mrc;
550  index = r;
551  }
552  }
553  return index;
554  }
555 
561  size_t CountNullCellsInRow(size_t r) const
562  {
563  if (r >= rows)
564  throw ExceptionDomain("Matrix::CountNullCellsInRow(): row index out of range");
565  return std::count(data.begin() + r * cols, data.begin() + (r + 1) * cols, T(0));
566  }
567 
573  size_t CountNullCellsInColumn(size_t c) const
574  {
575  if (c >= rows)
576  throw ExceptionDomain("Matrix::CountNullCellsInColumn(): column index out of range");
577  auto count = size_t(0);
578  for (size_t r = 0; r < rows; ++r)
579  if (At(r, c) == T(0))
580  count += 1;
581  return count;
582  }
583 
585  size_t CountNullCells() const
586  {
587  return std::count(data.begin(), data.end(), T(0));
588  }
589 
592  {
593  auto m = Matrix(1, cols);
594  for (size_t r = 0; r < rows; ++r)
595  for (size_t c = 0; c < cols; ++c)
596  m.At(c) += At(r, c);
597  m *= 1.0 / double(rows);
598  return m;
599  }
600 
602  Matrix MakeColumnDeviations(const Matrix &means) const
603  {
604  auto m = Matrix(1, cols);
605  for (size_t r = 0; r < rows; ++r)
606  for (size_t c = 0; c < cols; ++c)
607  m.At(c) += Sqr(At(r, c) - means[c]);
608  m *= 1.0 / double(rows);
609  return m;
610  }
611 
615  inline Matrix MakeColumnDeviations(bool zero_means = false) const
616  {
617  if (zero_means)
618  return MakeColumnDeviations(Matrix{1, cols});
619  else
621  }
622 
625  {
626  auto cov = Matrix(cols, cols);
627  for (size_t i = 0; i < cols; ++i)
628  for (size_t j = i; j < cols; ++j)
629  {
630  auto product = T(0);
631  for (size_t k = 0; k < rows; ++k)
632  product += At(k, i) * At(k, j);
633  // Each pattern in supposed equiprobable
634  // and thus weighted with 1/nbPatterns.
635  product /= T(rows);
636  cov[i][j] = product;
637  if (i != j)
638  cov[j][i] = product;
639  }
640  return cov;
641  }
642 
648  inline T* operator[](size_t r) { return data.data() + r * cols; }
649 
655  inline const T* operator[](size_t r) const { return data.data() + r * cols; }
656 
658  inline const std::vector<T>& Std() const & noexcept { return data; }
660  inline std::vector<T> Std() && { return std::move(data); }
661 
663  template<typename IMG> IMG ToImage() const
664  {
665  auto img = IMG{cols, rows};
666  std::copy(data.begin(), data.end(), img.Begin());
667  return img;
668  }
669 
673  String ToString() const
674  {
675  String s;
676  for (size_t r = 0; r < rows; r++)
677  {
678  for (size_t c = 0; c < cols; c++)
679  {
680  if (c) s += U" ";
681  s += At(r, c);
682  }
683  s += "\n";
684  }
685  return s;
686  }
687 
688  protected:
690  inline bool areValidIndexes(size_t r, size_t c) const { return (r < rows) && (c < cols); }
691 
692  using datatype = std::vector<T>;
694  size_t rows, cols;
695  };
696 
697  template<typename T> Matrix<T> operator+(const Matrix<T> &m1, const Matrix<T> &m2)
698  { return Matrix<T>(m1) += m2; }
699  template<typename T> Matrix<T> operator+(Matrix<T> &&m1, const Matrix<T> &m2)
700  { return std::move(m1 += m2); }
701  template<typename T> Matrix<T> operator+(const Matrix<T> &m1, Matrix<T> &&m2)
702  { return std::move(m2 += m1); }
703 
704  template<typename T> Matrix<T> operator-(const Matrix<T> &m1, const Matrix<T> &m2)
705  { return Matrix<T>(m1) -= m2; }
706  template<typename T> Matrix<T> operator-(Matrix<T> &&m1, const Matrix<T> &m2)
707  { return std::move(m1 -= m2); }
708 
709  template<typename T> Matrix<T> operator*(const Matrix<T> &m, double d)
710  { return std::move(Matrix<T>(m) *= d); }
711  template<typename T> Matrix<T> operator*(Matrix<T> &&m, double d)
712  { return std::move(m *= d); }
713  template<typename T> Matrix<T> operator*(double d, const Matrix<T> &m)
714  { return Matrix<T>(m) *= d; }
715  template<typename T> Matrix<T> operator*(double d, Matrix<T> &&m)
716  { return std::move(m *= d); }
717 
718  template<typename T> Matrix<T> operator/(const Matrix<T> &m, double d)
719  { return std::move(Matrix<T>(m) /= d); }
720  template<typename T> Matrix<T> operator/(Matrix<T> &&m, double d)
721  { return std::move(m /= d); }
722  template<typename T> Matrix<T> operator/(double d, const Matrix<T> &m)
723  { return Matrix<T>(m) /= d; }
724  template<typename T> Matrix<T> operator/(double d, Matrix<T> &&m)
725  { return std::move(m /= d); }
726 
727 
728  template<typename T> Matrix<T> operator*(const Matrix<T> &m1, const Matrix<T> &m2)
729  { return Matrix<T>(m1) *= m2; }
730  template<typename T> Matrix<T> operator*(Matrix<T> &&m1, const Matrix<T> &m2)
731  { return std::move(m1 *= m2); }
732 
733  template<typename I> struct TypeInfo<Matrix<I>>
734  {
738  };
739 
740 }
741 
742 namespace std
743 {
744  template<typename T> void swap(crn::Matrix<T> &m1, crn::Matrix<T> &m2)
745  {
746  m1.Swap(m2);
747  }
748 }
749 #endif
750 
BoolNotBoolDummy operator-(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:124
std::vector< T > Std()&&
Definition: CRNMatrix.h:660
String ToString() const
Prints matrix into a string.
Definition: CRNMatrix.h:673
ScalarRange< T > Range(T b, T e)
Creates a range [[b, e[[.
Definition: CRNType.h:257
size_t GetRows() const noexcept
Returns the number of rows.
Definition: CRNMatrix.h:157
void SwapColumns(size_t c1, size_t c2)
Swap two columns in matrix.
Definition: CRNMatrix.h:399
void Negative()
Change sign for all elements.
Definition: CRNMatrix.h:182
void Swap(Matrix &m)
Definition: CRNMatrix.h:146
size_t GetCols() const noexcept
Returns the number of columns.
Definition: CRNMatrix.h:163
T & At(size_t pos) noexcept
Definition: CRNMatrix.h:166
void IncreaseRow(size_t r, const T &v)
Increase one row from matrix.
Definition: CRNMatrix.h:242
Matrix(std::vector< T > &&vect, Orientation ori=Orientation::VERTICAL)
Constructor from std::vector 1D vector.
Definition: CRNMatrix.h:114
Orientation
An enumeration of orientations.
Definition: CRNMath.h:152
Matrix MakeCovariance() const
Definition: CRNMatrix.h:624
Matrix(const std::vector< T > &vect, Orientation ori=Orientation::VERTICAL)
Constructor from std::vector 1D vector.
Definition: CRNMatrix.h:88
Matrix & operator/=(double d)
Scaling.
Definition: CRNMatrix.h:313
const T * operator[](size_t r) const
Returns a row.
Definition: CRNMatrix.h:655
T * operator[](size_t r)
Returns a row.
Definition: CRNMatrix.h:648
virtual Matrix & Transpose()
Inplace transposition.
Definition: CRNMatrix.h:472
size_t CountNullCells() const
Definition: CRNMatrix.h:585
Matrix MakeRow(size_t r) const
Extract a row from current matrix.
Definition: CRNMatrix.h:501
size_t rows
Definition: CRNMatrix.h:694
Matrix MakeColumn(size_t k) const
Extracts one column from matrix.
Definition: CRNMatrix.h:513
bool operator!=(const Matrix &m) const
Definition: CRNMatrix.h:298
size_t CountNullCellsInColumn(size_t c) const
Counts null cells in column.
Definition: CRNMatrix.h:573
Matrix & operator*=(const Matrix &m)
Definition: CRNMatrix.h:279
size_t CountNullCellsInRow(size_t r) const
Counts null cells in row.
Definition: CRNMatrix.h:561
Matrix & operator*=(double d)
Scaling.
Definition: CRNMatrix.h:306
void ReduceColumns(bool centered=true)
Reduces all columns by dividing by the standard deviation of each column.
Definition: CRNMatrix.h:358
T & At(size_t r, size_t c) noexcept
Definition: CRNMatrix.h:168
A UTF32 character string class.
Definition: CRNString.h:61
IMG ToImage() const
Converts to image.
Definition: CRNMatrix.h:663
const T & At(size_t r, size_t c) const noexcept
Definition: CRNMatrix.h:167
bool areValidIndexes(size_t r, size_t c) const
checks the validity of indexes
Definition: CRNMatrix.h:690
void NormalizeForConvolution()
Normalizes the matrix to be used for convolution.
Definition: CRNMatrix.h:409
A generic domain error.
Definition: CRNException.h:83
T GetMin() const
Definition: CRNMatrix.h:432
Matrix MakeTranspose()
Create transposition.
Definition: CRNMatrix.h:460
BoolNotBoolDummy operator*(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:125
void MultRow(size_t r, double v)
Scale one row from matrix.
Definition: CRNMatrix.h:320
Base matrix class.
Definition: CRNMatrix.h:43
decltype(std::declval< T >()+std::declval< T >()) SumType
A safe type to compute a sum (e.g.: a larger integer type)
Definition: CRNType.h:179
BoolNotBoolDummy operator+(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:123
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
Definition: CRNMath.h:61
datatype data
Definition: CRNMatrix.h:693
void SetAll(const T &v)
Set all elements.
Definition: CRNMatrix.h:173
void CenterColumns()
Centers all columns by subtracting the mean for each column.
Definition: CRNMatrix.h:342
A dimension error.
Definition: CRNException.h:119
void IncreaseColumn(size_t c, const T &v)
Increase one column from matrix.
Definition: CRNMatrix.h:255
std::vector< T > datatype
Definition: CRNMatrix.h:692
Matrix MakeColumnMeans() const
Definition: CRNMatrix.h:591
Matrix & operator+=(const Matrix &m)
Definition: CRNMatrix.h:263
void Abs(Image< T > &img, typename std::enable_if< std::is_arithmetic< T >::value >::type *dummy=nullptr) noexcept
Replaces each pixel by its absolute value.
Definition: CRNImageGray.h:47
Matrix & operator=(const Matrix &)=default
const T & At(size_t pos) const noexcept
Definition: CRNMatrix.h:165
Matrix MakeRowAsColumn(size_t r) const
Creates a column matrix from a row.
Definition: CRNMatrix.h:489
std::pair< size_t, size_t > Argmin() const
Definition: CRNMatrix.h:444
const std::vector< T > & Std() const &noexcept
Definition: CRNMatrix.h:658
void MultColumn(size_t c, double v)
Scale one column from matrix.
Definition: CRNMatrix.h:333
BoolNotBoolDummy operator/(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:126
bool operator==(const Matrix &m) const
Definition: CRNMatrix.h:292
size_t ArgmaxInColumn(size_t c) const
Returns the row index of the maximal element in a column.
Definition: CRNMatrix.h:538
void SetColum(size_t c, const std::vector< T > &col)
Sets the values of a column.
Definition: CRNMatrix.h:213
virtual ~Matrix() override=default
Destructor.
T GetMax() const
Definition: CRNMatrix.h:438
size_t ArgmaxInRow(size_t r) const
Returns the row index of the maximal element in a row.
Definition: CRNMatrix.h:527
std::pair< size_t, size_t > Argmax() const
Definition: CRNMatrix.h:451
TypeInfo< T >::SumType CumulateCells() const
Definition: CRNMatrix.h:422
void SetRow(size_t r, const std::vector< T > &row)
Sets the values of a row.
Definition: CRNMatrix.h:196
Matrix MakeColumnDeviations(const Matrix &means) const
Definition: CRNMatrix.h:602
size_t cols
Definition: CRNMatrix.h:694
A class containing informations on a type.
Definition: CRNType.h:176
Matrix MakeColumnDeviations(bool zero_means=false) const
standard deviation of patterns stored as the rows of a data matrix
Definition: CRNMatrix.h:615
void IncreaseElement(size_t r, size_t c, const T &delta)
Increases the value of an element.
Definition: CRNMatrix.h:230
Matrix(const Matrix< Y > &m)
Definition: CRNMatrix.h:137
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107
Matrix & operator-=(const Matrix &m)
Definition: CRNMatrix.h:271
Matrix(size_t nrow, size_t ncol, const T &value=T(0))
Constructor.
Definition: CRNMatrix.h:52
Matrix(const std::vector< std::vector< T >> &m)
Constructor from std::vector matrix.
Definition: CRNMatrix.h:64
void SwapRows(size_t r1, size_t r2)
Swap two rows in matrix.
Definition: CRNMatrix.h:385